Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0055

Gemini only, model fixed by env; the admin page keeps the kill switch and sampling

Trạng thái
accepted
Ngày
2026-11-23
Người quyết định
SmartLearn team (#184)

Context

The admin AI page offered a provider picker (Gemini, OpenAI, Fake), a fallback provider, a chat model and a Thinking Budget. None of it matched what runs: the project only has Gemini keys, the worker ignored the admin config entirely (its model and thinking come from env), the Thinking Budget field reached no request, and the kill switch stopped the api but not the worker. The probe and the "key ready" badge only looked at GEMINI_API_KEY, not the GEMINI_API_KEYS pool. Toggling the switch re-saved every unsaved field of the form.

Decision

  • Gemini only, the model fixed by env. GEMINI_MODEL (api calls), AI_QUESTION_GENERATION_MODEL (quiz worker, else GEMINI_MODEL), EMBEDDING_MODEL and the thinking settings are read from env; resolveAiModels(env) in packages/ai is the one resolution both createAiClient and the admin page use, so the page shows what is called. Changing a model is an env change and a restart.
  • ai.config keeps three fields: aiEnabled, temperature, maxTokens. Older rows are read field by field (resolveAiConfig): provider/model/fallback/thinking fields are ignored, and one invalid field never resets the others, so a stored aiEnabled: false stays off.
  • The kill switch is an AiClient gate. AiClientOptions.gate runs before every call (before the cache and the budget, no event). The api and the worker each pass one that reads ai.config; refusal is AiError('disabled') (never retried). The api maps it to a Vietnamese 503 AI_DISABLED; worker processors treat it as non-transient (grading falls back to its deterministic feedback). The worker re-reads the switch at most every 5 s, the api every 2 s (and at once after an admin change in the same process).
  • Separate endpoints: PUT /admin/ai/config saves the sampling defaults only, PUT /admin/ai/enabled the switch only; each writes the whole row and audits the changed fields with their old and new values. The probe pings the env model with the api's key pool, without retries, and works while AI is off.
  • The OpenAI and Fake adapters stay in packages/ai: tests and laptops without a key use LLM_PROVIDER=fake. Nothing in the admin UI or API selects a provider.

Consequences

  • The admin page shows the real model, key status and the daily budget (limit and today's global bucket), and can no longer point production at a model nobody priced or tested.
  • Switching models needs a deploy; acceptable for a capstone with one provider.