Skip to content

Implement Assignment 1 Live Translate backend (AI service + gateway)#62

Open
iamSarang wants to merge 4 commits into
hamzafarooq:mainfrom
iamSarang:fde-live-translate-backend-implementation
Open

Implement Assignment 1 Live Translate backend (AI service + gateway)#62
iamSarang wants to merge 4 commits into
hamzafarooq:mainfrom
iamSarang:fde-live-translate-backend-implementation

Conversation

@iamSarang

Copy link
Copy Markdown

Summary

  • Implements the 5 TODOs in FDE/Assignment_1_Live_Translate: lib/llm.py (Mexican-Spanish prompt + Anthropic call), lib/cache.py (SQLite schema + two-tier get/set), app.py (cache → LLM → cache flow with latency timing), and the Node gateway's request-logging middleware + AI-service proxy call.
  • Bumps anthropic from 0.39.00.69.0 in requirements.txt — the old pin crashed on import due to an httpx compatibility break, unrelated to the assignment TODOs.
  • Fixes a lazy-init bug in lib/llm.py: the Anthropic client was built at module-import time, before app.py's load_dotenv() ran, so ANTHROPIC_API_KEY was never picked up.

Test plan

  • Cache unit-tested directly (miss → set → memory hit → fresh-instance DB hit → correct stats)
  • AI service /health, /stats, /translate, /translate/batch exercised via curl with a live Anthropic key
  • Gateway /health, /translate (200 and 502 on AI-service failure), /translate/batch, /widget.js, request-logging output all verified
  • Full end-to-end browser test on a demo page: cache miss (13 chunks, ~22s, real LLM calls, prices/model codes preserved) → Restore → cache hit (13/13 cache hits, instant)

🤖 Generated with Claude Code

sarang and others added 4 commits July 15, 2026 15:35
Fills in the assignment's TODOs so the translation widget works end to end:
- lib/llm.py: Mexican-Spanish system prompt and Anthropic call, lazy-init the
  client so it picks up ANTHROPIC_API_KEY after load_dotenv() runs
- lib/cache.py: SQLite schema init, two-tier get/set (memory + disk) with hit
  stats
- app.py: wire cache -> LLM -> cache flow with server-side latency timing
- gateway-node/server.js: request logging middleware and the AI-service proxy
  call

Also bumps anthropic 0.39.0 -> 0.69.0 in requirements.txt; the old pin crashed
on import due to an httpx compatibility break.

Verified end-to-end in a browser: cache miss (13 chunks, ~22s, real LLM
calls) -> cache hit (13/13 cache hits, instant) on a live demo page through
the Node gateway.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Gateway generates a UUID (or reuses an inbound X-Request-Id), returns it
on the response, forwards it as a header to the AI service, and logs it
in a structured JSON line. AI service reads the header (falling back to
its own UUID for direct calls with no header) and includes it in its
translate/translate_batch log lines, so one request is greppable by a
single ID across both services' logs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
- ai-service-python: Dockerfile (binds uvicorn to :: for IPv6/6PN reachability),
  fly.toml with a persistent 1GB volume for translations.db (survives restarts)
  and an internal-only [[services]] block (no public ports) so the AI service
  and its API key never face the public internet
- gateway-node: Dockerfile, fly.toml (public http_service on :8787). Bundles
  its own copy of translation-widget.js (same pattern the extension/ folder
  already uses) since Fly's build context is scoped to the app directory and
  can't reach ../../widget/
- Secrets (ANTHROPIC_API_KEY, AI_SERVICE_URL) set via `fly secrets`, not committed

Live: https://sarang-livetranslate-gw.fly.dev — verified end-to-end (translate,
cache hit, request-ID tracing all confirmed against the deployed gateway); AI
service confirmed unreachable from the public internet, only via Fly's private
network.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
- eval/report.json + eval/REPORT.md: automated rubric run, 70/70 auto score,
  fresh-cache SLA gate pass, deploy health check against the live Fly URL
- benchmark/_bench.json: corroborating fresh-cache benchmark run
- PRODUCT_EVAL.md (+ PDF): live-website test against a real, uncontrolled
  homedepot.com product page (212 chunks) via the deployed gateway — prices,
  model codes and SKUs preserved, natural es-MX, 98x cache speedup on
  re-translate (199,564ms -> 2,035ms, 99% hit rate). Also documents a real
  finding: a 653-chunk search-listing page timed out on a single giant batch
  call, a genuine scaling gap worth fixing before production.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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