Skip to content

feat: improve RAG context budgeting (bounty #7)#12

Open
jing11223344 wants to merge 4 commits into
aietal:masterfrom
jing11223344:improve-rag-context-budgeting
Open

feat: improve RAG context budgeting (bounty #7)#12
jing11223344 wants to merge 4 commits into
aietal:masterfrom
jing11223344:improve-rag-context-budgeting

Conversation

@jing11223344
Copy link
Copy Markdown

Summary

Improves RAG context budgeting as described in issue #7 (Algora bounty clq18zr98000ejs0gt0nv7gwu).

Changes

New file: ui/utils/app/rag-context.ts
Shared RAG context preparation helper:

  • parseBoundedInt — safe integer parsing with range clamping (env override support)
  • formatSource — consistent source string formatting
  • prepareRagContext — deduplication by content fingerprint, configurable character budget (default 8K), returns context + metadata
  • defaultNResults — Chroma query count with bounds (default 8, max 20)

Updated: ui/pages/api/fetch-documents.ts

  • Accept optional nResults parameter (default 8, cap 20)
  • Return _prepared context alongside raw Chroma results
  • Use CHROMA_PATH env var instead of hardcoded chroma-server:8000

Updated: ui/pages/api/rag-chat.ts

  • Build fetch URL from request origin instead of hardcoded http://localhost:3000 (fixes hosted/Docker/proxy deployments)
  • Consume _prepared context from fetch-documents
  • Graceful fallback when document fetch fails

New file: ui/__tests__/utils/app/rag-context.test.ts
25 comprehensive tests covering:

  • parseBoundedInt edge cases (null, NaN, min, max, float, string)
  • formatSource with/without page number
  • prepareRagContext dedup, budget truncation, empty/null docs, blank content, env override
  • defaultNResults bounds checking

Testing

All 25 tests pass:

✓ __tests__/utils/app/rag-context.test.ts (25 tests)

Closes #7

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