This document explains what the app does when you run "RAG Training" and what gets stored or cached.
RAG training builds a searchable knowledge base that helps the AI answer SQL questions by combining:
- Schema context (tables/columns/keys)
- Query examples (natural language + SQL pairs)
- Documentation (business terms and descriptions)
The output is stored in Azure AI Search (preferred) or in-memory embeddings when Azure Search is disabled.
RAG is only one of three learning paths:
- RAG training (this doc):
- Semantic retrieval of schema/query/doc embeddings.
- Best for broad context and examples.
- Chat memory (JDBC message window):
- Recent turn-by-turn conversation continuity.
- Not intended for deterministic SQL rule enforcement.
- Business rule memory (feedback guardrails):
- Connection-scoped SQL constraints learned from corrections/teachings.
- Enforced before SQL execution and during SQL repair loops.
Use feedback/guardrails when users give exact join/filter/table rules that must always be applied.
When you click Train for a connection:
- Scan schema: The app reads tables, columns, and metadata from the target DB.
- Build table summaries: For each table it generates a DDL-like text block.
- Create embeddings: Each table summary is turned into a vector embedding.
- Index:
- If Azure AI Search is enabled, documents are upserted with deterministic IDs.
- Otherwise, embeddings are cached in memory for that connection.
- Evict RAG cache: Clears the per-connection retrieval cache so future queries use the newest schema.
When a query is successful, the app can store the natural language prompt + SQL:
- Save a
QueryExamplerow in Postgres (connection-scoped). - Create an embedding.
- Index to Azure Search (or cache in memory if Azure Search is off).
- Evict the per-connection RAG cache.
When a user adds documentation (table/column/business term):
- Save
SchemaDocumentationin Postgres. - Create an embedding and index/cache.
- Evict the per-connection RAG cache.
- Primary store: Azure AI Search index (vector + keyword search).
- Fallback: In-memory embeddings per connection when Azure Search is disabled.
- Caching:
ragRetrievalcache with per-connection keys; eviction happens after training.
Training jobs are asynchronous and tracked per connection:
- Statuses:
PENDING,RUNNING,COMPLETED,FAILED,CANCELLED,REJECTED. - Progress updates include current table and percent complete.
- History is persisted in
training_job_history. - Retention defaults to keeping the last 50 jobs per connection.
If you cancel while the job is PENDING or RUNNING:
- The job is marked
CANCELLED. - Any queued work is discarded.
- History is updated immediately.
POST /api/training/schema/{connectionId}— start schema trainingPOST /api/training/schema/cancel/{connectionId}— cancel schema trainingGET /api/training/schema/status/{connectionId}— current statusGET /api/training/schema/stream/{connectionId}— SSE progress updatesGET /api/training/history/{connectionId}— recent historyGET /api/training/queue/metrics— queue depth + concurrency
- Schema training is idempotent when Azure Search is enabled (stable document IDs per table).
- Training results are isolated per connection; each connection maintains its own RAG data.