Repository navigation
feat(graphile-llm): embed and complete through the metered gateway - #1961
Conversation
…ugh the metered gateway
With AGENTIC_SERVER_URL set, graphile-llm embeds VectorNearbyInput.text,
{column}Text companions, ragQuery and embedText through the gateway's
/v1/embeddings (and ragQuery's answer through /v1/chat/completions) under
the identity the request's jwt.claims carry, so every model call is
recorded once with its database and actor. The gateway reports Ollama's
prompt_eval_count as embedding usage.
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. 🟡 2 medium 💬 Inline comments (1)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Gateway chat drops // agentic/agentic-server/src/transforms.ts
72 if (provider.type === 'ollama') {
73 return {
74 model: model || 'llama3',
75 messages: flattenContentParts(body.messages),
76 stream: false,
77 ...(body.temperature !== undefined && {
78 options: { temperature: body.temperature }
79 })
80 };
81 }The new gateway chat completer sends 🧹 Nitpicks (1) — 🟢 1 low
This PR routes embed and chat completions through the agentic-server LLM gateway with usage metering, adds gateway-aware env resolution in
Three issues were flagged: RAG plugin scopes omit the per-database model overrides, the Ollama transform drops Reviewed commit: 037c1ed |
There was a problem hiding this comment.
Integrates an LLM gateway metering path across graphile-llm, agentic-server, llm-env, and the server-test suite, with per-tenant llm_module model overrides and Ollama/OpenAI request transforms.
Key findings
- 🟡 RAG scopes drop
llm_modulemodel overrides — rag-plugin.ts:260 - 🟡 Gateway chat drops
max_tokensfor Ollama — transforms.ts:72
| return lambda($combined, (input: any) => llmConfigStore.run( | ||
| { identity: identityFromClaims(input.pgSettings) }, | ||
| async () => { |
There was a problem hiding this comment.
🟡 bug · medium
RAG scopes drop llm_module model overrides
The new llmConfigStore.run scopes in ragQuery and embedText carry only identity (graphile/graphile-llm/src/plugins/rag-plugin.ts:260-262), while the gateway embedder and chat completer read embeddingModel/chatModel overrides from the store (graphile/graphile-llm/src/embedder.ts:149, graphile/graphile-llm/src/chat.ts:98). Those per-database llm_module overrides are only resolved on the embed-inputs path via resolveConfigOverrides (graphile/graphile-llm/src/embed-inputs.ts:54-73), and the RAG plugin receives the raw, unwrapped embedder. In a multi-tenant deployment where a database overrides its model, ragQuery embeds its prompt and generates its answer with the env-default model while the chunk-search embeddings on the same request use the tenant's model — mismatched embedding dimensions can silently break the vector search and the metered calls record the wrong model.
📋 Prompt for AI Agents
In graphile/graphile-llm/src/plugins/rag-plugin.ts, the ragQuery lambda (lines 260-262) and the embedText lambda (lines 380-382) call llmConfigStore.run({ identity: identityFromClaims(...) }, ...) with only an identity, while the gateway embedder/chat completer also read embeddingModel/chatModel/baseUrl from the store (graphile/graphile-llm/src/embedder.ts:149, graphile/graphile-llm/src/chat.ts:98). Export resolveConfigOverrides from graphile/graphile-llm/src/embed-inputs.ts:54 and, inside both RAG lambdas, await it with the request's grafast context and spread the result into the scope: llmConfigStore.run({ ...(await resolveConfigOverrides(ctx)), identity: identityFromClaims(input.pgSettings) }, ...), so ragQuery/embedText honor the database's llm_module model configuration like the embed-inputs path does.
…gateway forwards max_tokens to Ollama
…ect provider route
Summary
graphile-llm's server-side model calls —
VectorNearbyInput.text,unifiedSearch, the{column}Textcompanions,ragQueryandembedText— called the model provider directly (EMBEDDER_BASE_URL/CHAT_BASE_URL), so none of them passed through theagentic-servergateway that records inference. They now go through the gateway atAGENTIC_SERVER_URL, once per model call, under the identity the request carries. There is no direct-provider route any more: configuring an embedder or chat completer withoutAGENTIC_SERVER_URLfails when the schema is built.@constructive-io/llm-env:AGENTIC_SERVER_URL→gateway.url(no default).graphile-llm:buildEmbedder/buildChatCompleter(presetdefaultEmbedder/defaultChatCompleter) and the...FromEnvvariants return a gateway client (POST /v1/embeddings,POST /v1/chat/completions,X-LLM-Provider= the configured provider).EmbedderConfig/ChatConfiglosebaseUrl;buildEmbedderFromModule/buildChatCompleterFromModuleand the@agentic-kit/ollamadependency are gone. With a gateway and nothing else configured the default models are used; with a provider or model configured and no gateway,requireGatewayUrlthrows; with neither, text fields and RAG stay disabled.identityFromClaims(pgSettings)readsjwt.claims.database_id/entity_id/user_idfrom the request's own pgSettings;resolveRequestScopeputs it (plus the database'sllm_modulemodel overrides) in the per-requestllmConfigStorefor the plan-time embedding step and the RAG plugin; the gateway client turns it intoX-Database-Id/X-Entity-Id/X-Actor-Idvia@agentic-kit/metering'sbuildIdentityHeaders. A request with nodatabase_idclaim is refused before it reaches the gateway.agentic-server: Ollama's/api/embedprompt_eval_countis reported asusage.prompt_tokens/total_tokens(was always 0);max_tokensis forwarded to Ollama asoptions.num_predict.Tests
graphql/server-test/__tests__/llm-gateway-metering.integration.test.tsruns the routed server over HTTP against a realagentic-server(in-memoryInferenceSink) in front of a local Ollama stand-in, and asserts the recorded entries: one per embedding with the request's database and actor (Bearer) or database as entity (anonymous); none when the client sends a vector;ragQuery= one embed + one chat;embedText= one; an upstream failure is recorded asstatus: 'error'and fails the request.On
main(same test, pre-fix sources): 7 of 9 fail — the embedder trieslocalhost:11434and nothing is recorded; only the two "records nothing" controls pass. On this branch: 9 of 9 pass.The existing suites that embedded through a local Ollama now run through the same gateway (
graphile-llm/test-utils/gateway.ts,server-test/__tests__/llm-gateway.shared.ts):graphile-llm98/98 (unit cases for the gateway/no-gateway/nothing-configured outcomes ofbuildEmbedder*/buildChatCompleter*, and a refused unattributed call),vector-text-search+vector-text-mutations14/14,packages/llm-env14/14,agentic/agentic-server22/22.Link to Devin session: https://app.devin.ai/sessions/698c523a027547f2be44eb9cbbae8d8e
Open in Devin Desktop: https://app.devin.ai/desktop/session/698c523a027547f2be44eb9cbbae8d8e?variant=devin
Requested by: @pyramation