What it does
TRagStore chunks each document, embeds the chunks into a vector index, retrieves the passages most
relevant to a question, and asks the model to answer using only that context. It reuses
AI4Delphi Connect's chat and embedding clients, so it inherits their resilience,
telemetry and provider neutrality for free.
AddDocument(id, text) — chunking with configurable ChunkSize and ChunkOverlap. Chunk boundaries snap back to a paragraph, sentence or word break rather than cutting mid-word, and the whole document is embedded in a single batched request.
Search(query, topK) — the top-K most relevant chunks by cosine similarity, as text.
Ask(query, topK) — retrieve, then answer under a "use only this context, cite the [docId], say you don't know otherwise" system prompt.
- Thread-safe index — indexing and querying may overlap.
- Per-client endpoints — required for Azure, and the route to fully offline retrieval.
Fully offline RAG
Point both the embedding and the chat endpoints at a local OpenAI-compatible server and no document ever
leaves the machine. For confidential or regulated content that is usually the deciding factor — and it is
something no other Delphi product offers.
LocalRag.dpr
Store := TRagStore.Create(
'openai', '', 'nomic-embed-text', CN_AI_OLLAMA_DEFAULT_ENDPOINT, // embeddings
'openai', '', 'llama3.1', CN_AI_OLLAMA_DEFAULT_ENDPOINT); // generation
Notes and limits
- The v1 index is in-memory — ideal for moderate corpora. External vector stores (Azure AI Search, Qdrant, pgvector, Redis, Pinecone) are on the roadmap behind the same Delphi surface, so swapping the store will not change your code.
- Embedding every chunk costs tokens. Connect's
UsageReported makes that visible, and indexing batches a document into one request so the round-trip cost is far lower than the token cost. Offline retrieval removes the per-token cost entirely.
- Retrieval quality depends on chunking and the embedding model; both are exposed so you can tune them.
- Requires Core: .Net 8+ or Framework: .NET Framework 4.5 or later, a dotNetCore4Delphi licence (Core edition) or a .Net Runtime Library for Delphi licence (Framework edition), and includes the Connect chat client it builds on.