GIVENALITY injects Tanzanian statute context into every chat turn via a fail-soft RAG helper

Two commits add a generic RAG injection layer -- env-gated, gracefully degrading -- that fetches Tanzanian legal context from GIVENALITY's Leksa corpus and prepends it to the LLM system prompt. The helper is portable; the system-prompt rewrites and seeded workflow templates are not.

contract-reviewsearch

The first commit (0c2eb990) puts a fetchLegalContext() call directly in projectChat.ts. On each turn it POSTs the latest user message to ${LEKSA_RAG_URL}/rag/retrieve, formats the returned chunks as a ## Tanzanian Legal Context markdown block, and appends it to the system prompt. The 5-second AbortSignal.timeout and a try/catch around the whole thing mean the chat proceeds normally if the RAG service is down or LEKSA_RAG_URL is unset.

The second commit (d5b14f75) lifts that function into backend/src/lib/leksaRag.ts and extends coverage to tabular.ts. The full-review SSE path fetches context once up-front from all column prompts concatenated, with topK = min(columns*3, 20), before SSE headers go out. The single-cell path calls fetchLegalContext per column prompt, which adds a 5-second timeout HTTP round-trip per cell for large reviews. The same commit also rewrites extraction system prompts to reference Tanzanian law and reframes flag values from "standard/favorable" to "compliant/non-compliant." A seedWorkflows.ts script adds four is_system=true tabular workflow templates (Employment Contract, SPA, Loan Agreement, Court Pleading) whose column prompts cite the Employment and Labour Relations Act 2004, the Limitation Act, BRELA, FCC, and Bank of Tanzania rate guidance.

Two import risks are worth flagging. The system-prompt rewrites -- "specialising in Tanzanian law," compliance/non-compliance flag framing -- are unconditional. Any deployment without LEKSA_RAG_URL set ends up with an analyst claiming Tanzanian expertise against a corpus it can't see. The per-cell fetchLegalContext call in queryTabularCell also adds latency at scale; a 30-column review would trigger 30 sequential RAG fetches at up to 5 seconds each.

The leksaRag.ts module itself is clean. To adapt it to a different corpus: set LEKSA_RAG_URL to your endpoint and confirm the response shape {chunks, sources, intent, language} matches.

So what Worth a look if you need an env-gated, fail-soft RAG injection layer for any jurisdiction-specific corpus -- `leksaRag.ts` is the part to cherry-pick. Skip the system-prompt rewrites and seeded workflows, which are Tanzania-specific and apply unconditionally regardless of whether the RAG service is connected.

View this fork on GitHub →

Spotted something wrong? Or know the PR text has fresher detail than the writeup above?

Commits in this thread

2 commits from GIVENALITY/mike, oldest first. Source extracted verbatim from the harvested git log.

SHA Subject Author Date
0c2eb990 Integrate Leksa RAG for Tanzanian legal context in project chat GIVENALITY 2026-05-14 ↗ GitHub
commit body
Adds fetchLegalContext() to projectChat.ts - on each project chat turn,
queries the Leksa /rag/retrieve endpoint (hybrid pgvector + FTS search
over Tanzanian statutes and case law) and injects the top-k results into
the system prompt. Degrades silently if LEKSA_RAG_URL is unset or the
service is unreachable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
d5b14f75 Add Leksa RAG legal context injection into tabular review extraction GIVENALITY 2026-05-14 ↗ GitHub
commit body
- Extract fetchLegalContext into shared lib/leksaRag.ts (used by both
  projectChat and tabular routes)
- Inject Tanzanian legal context into queryTabularAllColumns and
  queryTabularCell so the LLM can flag clauses as compliant/non-compliant
  against actual Tanzanian law (not just extract text)
- Extend inline citation requirement to all column formats (was only
  yes_no/date/tag; now text and bulleted_list also require citations)
- Add seedWorkflows.ts script: seeds 4 system tabular review workflows
  for Employment, SPA, Loan Agreement, and Court Pleading review

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Capture this thread into my fork

Download a single Markdown prompt that tells Claude how to port every commit above into your working tree — adapting paths and structure to match your repo. Run it via claude -p < capture-thread-432.md from inside the repo you want the changes in.

⬇ Download capture-thread-432.md