haiku.rag Documentation

repository·main·Indexed 20 days ago

https://github.com/ggozad/haiku.rag

An agentic Retrieval-Augmented Generation (RAG) system powered by LanceDB, Pydantic AI, and Docling. It supports hybrid search, multimodal vision capabilities, and complex analytical tasks via sandboxed Python code execution. The system includes a chat application, a continuous document ingester, and a CLI for benchmarking RAG retrieval and QA performance across various datasets.

Tokens
59.6K
Snippets
196
Records
256
Agent score
66%

What's inside haiku.rag

  1. Core capabilities of Haiku RAG

    main

    Haiku RAG provides an agentic RAG pipeline with the following core features:

    • Ingestion: Supports PDFs, DOCX, HTML, images, and 40+ formats via Docling. You can add files, URLs, or directories using haiku-rag add-src, or use the haiku-ingester service for continuous ingestion from filesystem, HTTP, S3, or WebDAV.
    • Search: Features hybrid retrieval (vector + full-text with reciprocal rank fusion), optional cross-encoder reranking, and structure-aware context expansion. It supports cross-modal retrieval (image-as-query) if configured with a multimodal embedder.
    • Answering: Provides RAG with citations (page numbers, section headings, visual grounding). Includes a vision capability where models receive figure bytes, and an Analysis capability using a sandboxed Python interpreter for computation across documents.
    • Integration: Accessible via Python API, CLI, MCP server, or as composable Pydantic AI capabilities.
    • Operation: Uses embedded LanceDB by default (no servers required), but can run on S3, GCS, Azure, or LanceDB Cloud. Supports time-travel queries via LanceDB versioning.
  2. Understand evaluation metrics: Retrieval, QA, and Citations

    main

    Haiku RAG evaluations use three primary metrics:

    1. Retrieval Metrics (MAP): Uses Mean Average Precision to score how well retrieved documents match the gold expected_uris. A score of 1.0 means relevant documents are ranked highest.
    2. QA Accuracy: The fraction of questions correctly answered, as determined by an LLM judge (default: ollama:qwen3.6).
    3. Citation Retrieval (cited_map): Measures whether the capability grounded its answer by using the cite tool to register the correct URIs. This uses the same MAP math as raw retrieval but specifically tracks the URIs provided in the answer.
  3. Use environment variables in configuration

    main

    You can use environment variables within your YAML configuration to manage secrets or deployment-specific settings. Substitution happens after YAML parsing, ensuring special characters like :, @, or # are treated as literal parts of the string and do not alter the YAML structure.

    Syntax:

    • ${VAR}: Replaces with the value of VAR. If VAR is unset, loading fails.
    • ${VAR:-default}: Uses default if VAR is unset or empty.
    • $$: Produces a literal $ character.
    ingester:
      queue:
        dburi: postgresql+asyncpg://haiku:${POSTGRES_PASSWORD}@db:5432/haiku_rag
  4. Expand search context for better RAG results

    main

    To improve retrieval quality, use expand_context(search_results) to include surrounding content from the documents found in a search.

    How it works

    • Structured Documents: Expansion includes the entire section containing the match. If a section is too large or small, it grows outward item-by-item, skipping noise like footnotes or headers.
    • Unstructured Documents: Grows outward item-by-item from the match.
    • Smart Merging: If expanded results overlap within the same document, they are merged into a single continuous result with the highest relevance score.
    • Exemptions: Picture and table matches return their enclosing section as-is and do not cross boundaries. Custom chunks without doc_item_refs are not expanded.

    Configuration: Control the maximum expansion size via search.max_context_chars (default: 5000).

    # Get initial search results
    search_results = await client.search("machine learning", limit=3)
    
    # Expand with section-bounded context
    expanded_results = await client.expand_context(search_results)
    
    for result in expanded_results:
        print(f"Expanded content: {result.content}")
  5. Understand Document Processing in Haiku RAG

    main

    Haiku RAG processes documents through two main stages: conversion (turning raw files into text/structured data) and chunking (splitting text into manageable pieces for retrieval).

    Note that document processing is distinct from document ingestion. While processing handles the transformation of content, the ingester service is responsible for continuous ingestion tasks such as watching directories or polling HTTP, S3, or WebDAV sources.

  6. Manage database state with Tags

    main

    A tag is a logical snapshot of the database state, composed of a single LanceDB tag on each of the five tables. Tags allow you to name specific database states (e.g., release-1) for deployment or after ingestion runs.

    Important considerations:

    • Completeness: A tag is considered 'complete' only if it exists on every table. If it is missing from some tables, haiku-rag tag list will mark it as a 'partial' tag. Partial tags can be listed or deleted but cannot be restored.
    • Concurrency: Tag creation coordinates writers within a single process only. To avoid capturing a 'mixed state' (where a writer in another process commits between table snapshots), stop all other writers before creating a tag.
    • Cleanup/Vacuum: Tagged versions are protected from vacuum. The vacuum process retains the oldest tagged version and every version newer than it. To allow the database to clean up old versions, delete tags that are no longer needed.
    • Storage: Tagged versions survive vacuum operations.
    # Tag the current state
    haiku-rag tag create release-1
    
    # List tags (partial tags will be marked)
    haiku-rag tag list
    
    # Delete a tag to allow vacuum to clean up old versions
    haiku-rag tag delete release-1
  7. Understand haiku.rag Chat App capabilities

    main

    The chat interface provides the following RAG capabilities:

    • Search: Hybrid vector + full-text search across your documents.
    • Answer questions: Generates answers with citations from your knowledge base.
    • Filter by document: Allows asking questions about specific files.
    • Visual grounding: Shows visual grounding for PDF or image sources.
  8. Configure Multimodal Embeddings

    main

    To enable cross-modal retrieval (where text and pictures share a single vector space), set embeddings.model.multimodal: true.

    Important Notes:

    • Capability is determined by this flag, not the provider name.
    • Setting this on a provider that doesn't support it (anything other than vllm, voyageai, or cohere) will raise an error at startup.
    • If you change this setting, you must rebuild or re-ingest your data to add or drop picture chunks.

    Supported Multimodal Providers:

    vLLM

    Use provider: vllm for multimodal models. This allows text inputs via the standard input field and image inputs via messages-with-image_url.

    embeddings:
      model:
        provider: vllm
        name: Qwen/Qwen3-VL-Embedding-8B
        vector_dim: 4096
        base_url: http://localhost:8000/v1
        multimodal: true

    VoyageAI

    Requires VOYAGE_API_KEY.

    embeddings:
      model:
        provider: voyageai
        name: voyage-multimodal-3
        vector_dim: 1024
        multimodal: true

    Cohere

    Requires CO_API_KEY.

    embeddings:
      model:
        provider: cohere
        name: embed-v4.0
        vector_dim: 1536
        multimodal: true
  9. Understand RAG request limits and tool lifecycle

    main

    To manage context window usage, the capability implements a request limit (defaulting to 20 requests per question):

    1. When the request_limit is reached, the rag_search tool is removed.
    2. The rag_cite tool remains available for up to two additional requests that call a RAG tool. This allows the model to register citations for evidence it has already gathered before finalizing its answer.
    3. Requests used for other capabilities do not count against this limit.
    4. Each new agent run starts a fresh limit; multi-turn chat does not share a single budget across turns.
  10. Benchmark analysis capability vs rag capability

    main

    By default, evaluations run benchmarks the rag-capability. To benchmark the analysis capability (which tests complex analytical tasks) against the same datasets, use the --target analysis-capability flag. You can also specify a specific model for the capability using --capability-model.

    When running these benchmarks, a citation retrieval metric (cited_map) is computed alongside QA accuracy based on the URIs registered via the cite tool.

    # Benchmark analysis capability using a specific model
    evaluations run wix --target analysis-capability --capability-model ollama:gpt-oss
  11. Tuning the Haiku RAG Retrieval Pipeline

    main

    The retrieval pipeline follows this flow: chunking → embedding → hybrid search (vector + FTS) → reranking → context expansion → LLM generation.

    Retrieval tuning (from chunking through reranking) is the highest-leverage stage. If the LLM does not receive the correct chunks, changes to prompts or models will not improve performance.

    Key Tuning Levers

    1. Chunking

    • chunk_size: Controls granularity. Smaller chunks improve precision but reduce context; larger chunks provide more context but may dilute relevance.
    • chunker_type: Choose between hybrid (default) and hierarchical. hierarchical chunking preserves heading structures and is better for deeply nested or structured content.

    2. Embedding Model

    • Larger models improve retrieval quality but increase indexing time and storage costs. This setting has a significant impact on retrieval quality.

    3. Reranking

    • When configured, a cross-encoder reranker re-scores 10x the requested candidates. This improves precision at the cost of increased latency.

    4. Search Settings

    • limit: Controls how many results reach the LLM. Increasing this improves recall but increases token usage.
    • Context Expansion: Automatically expands search results to include surrounding content. For structured documents, expansion stays within section boundaries and filters noise (footnotes, headers). For unstructured documents, it grows outward until max_context_chars is reached.
  12. Understand Haiku RAG benchmark methodologies and metrics

    main

    Haiku RAG uses several metrics to evaluate performance across different datasets. When reviewing benchmark results, understand the following key metrics:

    • MAP (Mean Average Precision): Used for evaluating Retrieval performance. It measures how well the system surfaces relevant documents in the search results.
    • QA Accuracy: Measures the correctness of the answer generated by the capability model. Note that for financial datasets like T²-RAGBench (FinQA), this is a deterministic numeric match (using NumberMatchEvaluator with a relative tolerance of 0.01) rather than an LLM-judged score.
    • Mean cited_map: Measures Citation Retrieval performance. It evaluates how effectively the model uses the cite tool to register URIs that match the gold expected_uris.

    Benchmarks are categorized by the type of embedding approach used:

    1. Multimodal embedder: Uses models like Qwen/Qwen3-VL-Embedding-8B where picture bytes and text share a vector space. No VLM is run during ingestion.
    2. Text embedder + VLM picture descriptions: Uses a text embedder (e.g., qwen3-embedding:4b) and a VLM (e.g., ollama/ministral-3) to describe images at ingestion. These descriptions are then woven into the text chunks for retrieval.