feat: add RAG (Retrieval-Augmented Generation) for AI Doctor

New files:
- tools/rag_build_index.sh: build-time ingestion pipeline
  Chunks docs/knowledge-base/*.md into 2000-char chunks with 200-char overlap
  Produces rag_index.json (39 chunks, ~74KB) for runtime embedding
- tachyon/files/usr/lib/diagnostics/rag.uc: runtime RAG module
  Embeds chunks via same LLM provider API (OpenAI-compatible /v1/embeddings)
  Cosine similarity search, top-3 retrieval, 24h cache in /tmp
  Graceful fallback if embedding API unavailable
- tachyon/files/usr/lib/tachyon/rag_index.json: pre-built chunk index

Modified files:
- runtime.uc: integrated RAG into ai_doctor() function
  Retrieves relevant docs based on user query or diagnostic context
  Injects 'Relevant Documentation' section into LLM prompt
- renderAdvancedSettings.ts: added 'Enable RAG' toggle in AI Doctor settings
- tachyon config: added enable_rag option (default '1')
- build.sh: added rag_build_index.sh step

Architecture:
- Provider-agnostic: uses same API key/provider as LLM for embeddings
- Embedding model: text-embedding-3-small (OpenAI), nomic-embed-text (Ollama),
  local-model (LMStudio), deepseek-embedding (DeepSeek)
- Cache: embeddings cached in /tmp/tachyon_rag_embeddings.json (24h TTL)
- Fallback: if embedding fails, AI Doctor works without RAG (as before)
- 39 chunks from 14 docs, ~66K chars, cosine search <1ms

623 tests pass (101 backend + 522 frontend)
This commit is contained in:
Dushnilin
2026-08-20 10:27:56 +03:00
parent 1137e96b0d
commit 421a4fd796
7 changed files with 425 additions and 0 deletions
+3
View File
@@ -1070,6 +1070,9 @@ main() {
[[ -x "$ipkg_build_bin" ]] || { echo "ipkg-build not found at $ipkg_build_bin" >&2; exit 1; }
[[ -x "$apk_bin" ]] || { echo "apk host tool not found at $apk_bin" >&2; exit 1; }
echo "Building RAG index..." >&2
bash "$ROOT_DIR/tools/rag_build_index.sh" "$ROOT_DIR/docs/knowledge-base" "$ROOT_DIR/tachyon/files/usr/lib/tachyon" || true
build_backend_root "$backend_root"
build_app_root "$app_root"
build_i18n_root "$i18n_root" "$po2lmo_bin"