Skip to content

feat(providers): gemini + ollama backends, fakes, and live smoke script (T04) - #5

Merged
prateekchaplot merged 2 commits into
mainfrom
arch/caliper/t04
Nov 15, 2026
Merged

prateekchaplot merged 2 commits into
mainfrom
arch/caliper/t04

Conversation

@prateekchaplot

Copy link
Copy Markdown
Owner

Task T04 from docs/ARCHITECTURE.md §14.2 (spec §7.2, §9.8, §15).

What

  • src/caliper/providers/: base.py (protocols, ProviderError, Usage/ChatResult, estimate_cost + prices.json with as_of, l2_normalize, ≤100-text batching, bounded exponential backoff on 429 — max 5 retries then quota_exhausted), gemini.py (google-genai; RETRIEVAL_DOCUMENT/QUERY; client-side L2 normalization of all 768-dim vectors), ollama.py (httpx; qwen2.5:7b-instruct / nomic-embed-text), factories get_chat(cfg) / get_embeddings(cfg) reading CALIPER_PROVIDER / GEMINI_API_KEY / OLLAMA_BASE_URL from env (§11.2). Model IDs/temperature/seed always come from config — never hardcoded.
  • tests/fakes.py: the suite's only mocks per §15 — scripted FakeChat (paraphrased-quote + refusal cases) and deterministic hash-based FakeEmbeddings (768-dim, L2-normalized). Foundation for T09–T14.
  • scripts/smoke.py: manual live smoke (§15 — human-run, never in CI): one chat + one 768-dim normalized embed per backend, cost estimate, 429 backoff demo, §9 envelope exit codes.
  • tests/conftest.py: puts tests/ on sys.path so fakes.py is importable from every test dir (smallest mechanism; avoids touching T01-owned pyproject.toml). Recorded in docs/TASKS.md Notes.

Notable fix found by the live smoke

89e5423: google-genai's Models holds only a weak reference to its Client — dropping the client fails every request ("client has been closed"). GeminiSession now owns the genai.Client for both factories and the smoke script. Without this the production ask path (T11/T12) would have broken on the first GC.

Verification

  • Offline: uv run pytest 97 passed (30 new; no network — retry/backoff via injectable sleep/transport, no patch() anywhere); ruff check, ruff format --check, mypy strict all clean.
  • Live smoke 2026-11-16 (Gemini leg): chat OK on gemini-3.5-flash-lite, embed OK 768-dim norm 1.000000, cost estimated, 429 backoff demo logged. Ollama leg deferred (Ollama not installed; user decision recorded in docs/TASKS.md) — re-run uv run python scripts/smoke.py after ollama pull qwen2.5:7b-instruct + nomic-embed-text.
  • OQ1 still open: confirm gemini-3.5-flash-lite free-tier RPD in AI Studio; log in §18.

Dependent tasks (per §14.4)

T05–T08 (ingest) consume get_embeddings/iter_batches; T09–T12 consume get_chat and tests/fakes.py; T14's cost metrics consume estimate_cost/prices.json. Dependents: re-run tests after this merges.

…pt (T04)

- ChatBackend/EmbeddingBackend protocols, ChatResult/Usage, typed
  ProviderError (quota_exhausted | provider_unavailable), bounded 429
  exponential backoff (max 5 retries, 1s..60s cap), L2 normalization,
  <=100-text batching, and prices.json (as_of 2026-11-16, per 9.8)
- Gemini via google-genai (config-pinned answer/embedding models,
  RETRIEVAL_DOCUMENT/RETRIEVAL_QUERY task types); Ollama via httpx
  (qwen2.5:7b-instruct + nomic-embed-text), injectable transport
- get_chat/get_embeddings factories read CALIPER_PROVIDER, GEMINI_API_KEY,
  OLLAMA_BASE_URL from env (11.2); 768-dim enforcement before any call
- tests/fakes.py: scripted FakeChat (paraphrased-quote + refusal cases)
  and deterministic hash-based FakeEmbeddings (768-dim, correlated)
- scripts/smoke.py: manual live smoke per backend + 429 backoff demo,
  exits nonzero with the 9 error envelope on failure
- 30 new unit tests; pytest/ruff/mypy strict all green
google-genai's Models object holds only a weak reference to its Client: dropping the Client makes every request fail with 'Cannot send a request, as the client has been closed'. get_chat/get_embeddings and scripts/smoke.py now build a GeminiSession that owns the Client, and the Gemini backends store it (keepalive slot). Found by the live smoke run; proven by the gemini leg passing (chat + 768-dim normalized embed + cost + backoff demo).
@prateekchaplot
prateekchaplot merged commit a051f3b into main Nov 15, 2026
1 check passed
@prateekchaplot
prateekchaplot deleted the arch/caliper/t04 branch November 15, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant