Core types
| Type | Role |
|---|---|
rag.Document | A text chunk with ID, Content, Source, and Metadata |
rag.Retriever | Interface: Retrieve(ctx, query, topK) ([]Document, error) |
rag.DocumentStorer | Interface: Store(ctx, sessionID, docs) error |
rag.Ingestor | Splits documents and stores them via a DocumentStorer |
rag.QdrantRetriever | Retriever backed by Qdrant semantic search |
rag.QdrantIngestor | Storer that writes chunks into Qdrant |
rag.PGVectorStore | Retriever + storer backed by PostgreSQL + pgvector |
rag.NewRetrieverTool | Wraps a Retriever as a core.Tool for LLM-driven retrieval |
Loading documents
Chunking / Splitting
Two built-in splitters:splitter.Splitter to use a custom strategy with rag.WithSplitter(s).
Ingesting into Qdrant
sessionID (here "kb") is the namespace. Use the same ID when retrieving.
Pattern 1 — Auto-retrieval (WithRetriever)
The agent automatically retrieves context before every LLM call and appends it to the system prompt. Best for Q&A chatbots where every question benefits from background knowledge.
Relevant context:\n[1] Source: ...\n... and appended to the system prompt before the first iteration.
Pattern 2 — Tool-based retrieval (RetrieverTool)
The LLM decides when to retrieve. Best for agents with multiple tools where retrieval is conditional.
retriever_tool with {"query": "..."} to fetch context on-demand.
Ingesting into PostgreSQL + pgvector
PGVectorStore implements both rag.Retriever and rag.DocumentStorer, so a single instance handles ingestion and retrieval. It automatically creates the vector extension and the chunk table on first use.
Session scoping
Store and RetrieveBySession both accept a sessionID. Use different IDs to maintain isolated knowledge bases in the same table:
HNSW index
For collections over 100k rows, enable the HNSW approximate nearest neighbor index. It trades a ~10–30s extra migration step for significantly faster ANN search:vector_cosine_ops, matching the <=> cosine distance operator used in all queries.
Prerequisites
- PostgreSQL 14+ with the pgvector extension installed
- Environment:
PGVECTOR_DSN=postgres://user:pass@host:5432/db
Custom Retriever
Implementrag.Retriever to connect any vector database:
core.Embedder interface
All embedders satisfy core.Embedder:
embedders.OpenAI(apiKey), embedders.Ollama(host, model, dims).
See also
- Vector Memory — using Qdrant as a conversation memory store
- Tools — adding the RetrieverTool alongside other tools