RAG (Retrieval-Augmented Generation) is the technique that makes AI actually useful for your data. Instead of hallucinating answers, the AI searches your documents first, then generates answers based on real information. Hazina makes this ridiculously easy.
What RAG Does
- Index – Your documents are split into chunks and converted to vector embeddings
- Retrieve – When a user asks a question, relevant chunks are found via similarity search
- Generate – The AI answers using the retrieved context
Quick Setup
// Configure storage
builder.Services.AddHazina(config => {
config.Storage.UseSqlite("hazina-rag.db");
config.AI.SetupOpenAI(apiKey);
});
Indexing Documents
var ragEngine = serviceProvider.GetRequiredService<IRagEngine>();
// Index a single document
await ragEngine.IndexDocumentAsync(new Document {
Title = "Company Policy",
Content = File.ReadAllText("policy.txt"),
Metadata = new { Department = "HR", Year = 2026 }
});
// Index a directory
await ragEngine.IndexDirectoryAsync("./documents",
pattern: "*.txt",
recursive: true);
Querying with Context
// Simple query
var answer = await ragEngine.AskWithContextAsync(
"What is our vacation policy?"
);
Console.WriteLine(answer.Response);
Console.WriteLine($"Sources: {string.Join(", ", answer.Sources)}");
// With metadata filter
var answer = await ragEngine.AskWithContextAsync(
"What changed in 2026?",
filter: new { Year = 2026 }
);
How Chunking Works
Hazina automatically splits documents into optimal chunks for retrieval:
- Default chunk size: 500 tokens with 50-token overlap
- Respects paragraph boundaries
- Preserves code blocks and structured content
- Configurable:
config.RAG.ChunkSize = 1000;
Storage Backends
Vector embeddings can be stored in:
- SQLite – Zero setup, great for development and small datasets
- PostgreSQL – Production-ready with pgvector extension
- Supabase – Managed PostgreSQL with vector support
In the next post, we’ll look at the Document Store – the persistence layer behind RAG and more.
Frequently Asked Questions
The Hazina RAG engine utilizes Retrieval-Augmented Generation (RAG) to enhance AI responses by searching relevant documents before generating answers. This technique minimizes inaccuracies by relying on actual data from indexed documents.
To index documents in Hazina RAG, you can use the `IndexDocumentAsync` method for single documents or `IndexDirectoryAsync` for multiple files in a directory. The engine automatically splits the content into optimal chunks for efficient retrieval.
Hazina RAG supports various storage backends, including SQLite for easy setup, PostgreSQL for production environments, and Supabase for managed PostgreSQL with vector support. Each option helps store vector embeddings effectively based on your use case.
Hazina RAG automatically splits documents into chunks of a default size of 500 tokens with a 50-token overlap, ensuring that paragraph boundaries are respected. This helps preserve the structure of the content and enhances retrieval accuracy.