feat: add embeddings endpoint and config for OpenAI-compatible text embeddings via gateway

Add POST /openai/v1/embeddings route in openai_gateway router, new config fields for embedding upstream URL/model/key/enabled toggle with defaults pointing to OpenRouter Qwen3 8B, INTERNAL_EMBED_MODEL constant in config.py, documentation in docs_api.py and README.md describing the molodetz~embed model mapping, and embed-call tracking in gateway metrics alongside existing chat/vision counters.
This commit is contained in:
2026-06-14 01:06:18 +00:00
parent 1076696dec
commit c7770ee21a
17 changed files with 662 additions and 62 deletions
@@ -130,6 +130,38 @@ class GatewayService(BaseService):
help="Image-description LRU cache entries (0 disables caching).",
group="Vision",
),
ConfigField(
"gateway_embed_enabled",
"Embeddings",
type="bool",
default=True,
help="Expose the embeddings model at /openai/v1/embeddings.",
group="Embeddings",
),
ConfigField(
"gateway_embed_url",
"Embeddings URL",
type="url",
default=config.EMBED_URL_DEFAULT,
help="OpenAI-compatible embeddings endpoint requests are forwarded to.",
group="Embeddings",
),
ConfigField(
"gateway_embed_model",
"Embeddings model",
type="str",
default=config.EMBED_MODEL_DEFAULT,
help="Embedding model sent upstream. Clients request it as molodetz~embed.",
group="Embeddings",
),
ConfigField(
"gateway_embed_key",
"Embeddings API key",
type="str",
default="",
help="The key currently in use; falls back to the vision/OPENROUTER key on boot (embeddings default to OpenRouter). Editable.",
group="Embeddings",
),
ConfigField(
"gateway_require_auth",
"Require authentication",
@@ -216,6 +248,15 @@ class GatewayService(BaseService):
minimum=0,
group="Pricing",
),
ConfigField(
"gateway_embed_price_input_per_m",
"Embeddings price input / 1M ($)",
type="float",
default=config.EMBED_PRICE_INPUT_PER_M_DEFAULT,
minimum=0,
help="Fallback only; used when the embeddings upstream returns no native cost.",
group="Pricing",
),
ConfigField(
"gateway_max_retries",
"Max retries",
@@ -290,6 +331,11 @@ class GatewayService(BaseService):
cfg["gateway_vision_key"] = cfg["gateway_vision_key"] or os.environ.get(
"OPENROUTER_API_KEY", ""
)
cfg["gateway_embed_key"] = (
cfg["gateway_embed_key"]
or cfg["gateway_vision_key"]
or os.environ.get("OPENROUTER_API_KEY", "")
)
return cfg
def authorize(self, request: Request) -> bool:
@@ -352,6 +398,18 @@ class GatewayService(BaseService):
self.log("Rejected chat request: JSON body was not an object")
raise HTTPException(status_code=400, detail="Invalid JSON body")
return await runtime.handle_chat(body, cfg, owner, user_agent, self.log)
if subpath == "embeddings" and request.method == "POST":
try:
body = await request.json()
except Exception:
self.log("Rejected embeddings request: invalid JSON body")
raise HTTPException(status_code=400, detail="Invalid JSON body")
if not isinstance(body, dict):
self.log("Rejected embeddings request: JSON body was not an object")
raise HTTPException(status_code=400, detail="Invalid JSON body")
return await runtime.handle_embeddings(
body, cfg, owner, user_agent, self.log
)
body = await request.body()
content_type = request.headers.get("content-type", "")
return await runtime.handle_passthrough(
@@ -395,6 +453,7 @@ class GatewayService(BaseService):
"in_flight": 0,
"peak_in_flight": 0,
"vision_calls": 0,
"embed_calls": 0,
"last_status": 0,
"last_latency_ms": 0,
"pool": 0,
@@ -406,11 +465,13 @@ class GatewayService(BaseService):
{"label": "Requests (lifetime)", "value": m["requests"]},
{"label": "In flight", "value": m["in_flight"]},
{"label": "Vision calls", "value": m["vision_calls"]},
{"label": "Embed calls", "value": m["embed_calls"]},
{"label": "Last status", "value": m["last_status"] or "-"},
{"label": "Last latency", "value": f"{m['last_latency_ms']} ms"},
{"label": "Pool size", "value": m["pool"]},
{"label": "Circuit", "value": "open" if m["circuit_open"] else "closed"},
{"label": "Model", "value": cfg["gateway_model"]},
{"label": "Embed model", "value": cfg["gateway_embed_model"]},
{"label": "Requests 24h", "value": s["requests"]},
{"label": "Success 24h", "value": f"{s['success_pct']}%"},
{"label": "Error rate 24h", "value": f"{s['error_pct']}%"},