Skip to content

Service Configuration

This document details the configuration files and environment variables that connect the Cloudflare Worker and the Labeeb API.


1. Cloudflare Worker (wrangler.jsonc)

The cloudflare/wrangler.jsonc file is the primary configuration for the worker. It defines environments, bindings to other Cloudflare services, and environment variables.

{
  "name": "cloudflare",
  "main": "src/index.ts",
  "compatibility_date": "2025-08-31",
  "observability": { "enabled": true },

  // Default environment (used by `wrangler dev`)
  "ai": { "binding": "AI" },
  "vectorize": [{ "binding": "VECTORIZE", "index_name": "labeeb-articles-dev" }],
  "vars": { "CF_RAG_TOP_K": 10, "CF_RERANK_TOP_N": 20 },

  // Environment-specific overrides
  "env": {
    "stage": {
      "ai": { "binding": "AI" },
      "vectorize": [{ "binding": "VECTORIZE", "index_name": "labeeb-articles-stage" }],
      "vars": { "CF_RAG_TOP_K": 10, "CF_RERANK_TOP_N": 20 }
    },
    "prod": {
      "ai": { "binding": "AI" },
      "vectorize": [{ "binding": "VECTORIZE", "index_name": "labeeb-articles" }],
      "vars": { "CF_RAG_TOP_K": 10, "CF_RERANK_TOP_N": 20 }
    }
  }
}
  • ai binding: Connects the worker to Cloudflare's Workers AI models.
  • vectorize binding: Connects the worker to the specified Vectorize index.
  • vars: Environment variables passed to the worker, used to control RAG behavior.

2. Labeeb API (Laravel)

The API service requires configuration to communicate with both the AI Gateway and the custom worker.

Config File (config/ai.php)

This file centralizes all AI-related configuration, pulling values from environment variables.

// config/ai.php
return [
  // For AI Gateway (OpenAI-compatible)
  'gateway_enabled'       => env('CF_AI_GATEWAY_ENABLED', false),
  'gateway_base'          => env('CF_AI_GATEWAY_BASE', ''),
  'provider_base'         => env('OPENAI_API_BASE', 'https://api.openai.com/v1'),
  'provider_key'          => env('OPENAI_API_KEY', ''),
  'provider_org'          => env('OPENAI_ORG_ID'),
  'timeout'               => (int) env('AI_HTTP_TIMEOUT', 60),
  'health_embedding_model'=> env('AI_HEALTH_EMBEDDING_MODEL', 'text-embedding-3-small'),

  // For our custom Edge Worker (RAG, Translate, etc.)
  'edge_worker_base'      => env('CF_WORKER_BASE', ''),
  'edge_worker_key'       => env('CF_WORKER_API_KEY', ''),
];

Environment Variables (.env)

These variables must be set in the API's .env file for each environment.

AI Gateway (OpenAI-Compatible)

Variable Example Value Description
CF_AI_GATEWAY_ENABLED true Enables or disables the AI Gateway client.
CF_AI_GATEWAY_BASE https://gateway.ai.cloudflare.com/v1/... The base URL from the Cloudflare dashboard.
OPENAI_API_KEY sk-... Your OpenAI API key.
AI_HTTP_TIMEOUT 60 HTTP client timeout in seconds.

Custom Worker (RAG & LLM)

Variable Example Value Description
CF_WORKER_BASE https://cloudflare-stage...workers.dev The URL of your deployed worker.
CF_WORKER_API_KEY <secret_token> The shared secret created during provisioning.

Service Clients & Health Checks

  • Clients:
    • App\Services\AiGatewayClient: Interacts with the AI Gateway (OpenAI-compatible).
    • App\Services\EdgeLlmClient: Interacts with our custom worker's /llm/* endpoints.
  • Health Controllers:
    • /api/health/ai-gateway: Checks the status of the AI Gateway.
    • /api/health/edge-llm: Checks the status of our custom worker via its /llm/health endpoint.