Skip to content

RAG Integration (KnowledgeBase) ​

agent-orca provides native Retrieval-Augmented Generation through the KnowledgeBase CRD. When an Agent references a KnowledgeBase, the model-router automatically injects _rag_search and _rag_ingest as built-in tools, giving the LLM direct access to vector search and runtime document ingestion.


Architecture ​

flowchart TD
    subgraph Operator
        KBC[KnowledgeBase Controller]
        API[Operator API :8082]
    end

    subgraph "Per Namespace"
        QD[(Qdrant StatefulSet<br/>gRPC :6334 / HTTP :6333)]
        CM[ConfigMap Sources]
        ING[mcp-ingester Job<br/>detect + extract + chunk + embed]
    end

    subgraph "Agent Pod"
        AC[Agent Container]
        MR[model-router sidecar]
    end

    subgraph "Embedding Provider"
        EP[OpenAI / Ollama / etc.]
    end

    KBC -->|1. auto-deploy| QD
    KBC -->|2. create collection| QD
    KBC -->|3. launch ingestion Job| ING
    ING -->|4. read sources| CM
    ING -->|5. embed| EP
    ING -->|6. upsert vectors| QD

    AC -->|chat completions| MR
    MR -->|_rag_search| API
    MR -->|_rag_ingest| API
    API -->|embed query| EP
    API -->|vector search| QD
    API -->|chunk + embed + upsert| QD

KnowledgeBase CRD ​

yaml
apiVersion: agentorca.agentorca.io/v1alpha1
kind: KnowledgeBase
metadata:
  name: my-kb
spec:
  description: "Project documentation"
  vectorStore:
    provider: qdrant          # only supported provider
    collectionName: my-kb     # defaults to CR name
    storageSize: "10Gi"       # PVC size for Qdrant
  embedding:
    modelSelectorRef: local-embeddings   # ModelSelector for /v1/embeddings calls
    chunkSize: 512                       # tokens per chunk
    chunkOverlap: 64                     # overlap tokens between chunks
  ingestion:
    configMapRefs:
      - name: my-docs                    # ConfigMap keys → documents
    urls:
      - https://example.com/docs.txt     # fetched at reconcile time
    syncIntervalSeconds: 3600            # 0 = one-shot, >0 = periodic re-sync

Status ​

yaml
status:
  ready: true
  vectorStoreURL: "kb-qdrant-my-kb.default.svc.cluster.local:6334"
  collectionName: "my-kb"
  documentCount: 3
  chunkCount: 12
  lastSyncTime: "2026-03-22T10:00:00Z"

Connecting an Agent to a KnowledgeBase ​

Add the KnowledgeBase name to the Agent's knowledgeBases field:

yaml
apiVersion: agentorca.agentorca.io/v1alpha1
kind: Agent
metadata:
  name: research-agent
spec:
  modelSelectorRef: default
  knowledgeBases:
    - my-kb
  systemPrompt: |
    You are a research assistant. Use _rag_search to find relevant
    information before answering questions.
  runtime:
    ociRef: ghcr.io/myorg/research-agent:latest

When knowledgeBases is non-empty, the operator automatically injects two built-in tools into the router config:

ToolDescription
_rag_searchEmbed a query and return the top-K most relevant chunks
_rag_ingestIngest new documents at runtime (chunk → embed → upsert)

Built-in Tools ​

json
{
  "knowledgeBase": "my-kb",
  "query": "How does the model-router handle authentication?",
  "topK": 5
}

Returns an array of results with score, text, doc_id, chunk_index, and any metadata.

_rag_ingest ​

json
{
  "knowledgeBase": "my-kb",
  "documents": [
    {
      "id": "meeting-notes-2026-03-22",
      "content": "Discussed new RAG integration...",
      "metadata": {"source": "meeting", "date": "2026-03-22"}
    }
  ]
}

Returns {"ingested": 1, "chunks": 3}.


Local Development Setup ​

This guide walks through testing RAG on a local kind cluster using Ollama for embeddings.

Prerequisites ​

  • kind (or any local Kubernetes cluster)
  • Ollama installed on macOS
  • Docker Desktop

Step 1: Create a kind cluster ​

bash
kind create cluster

Step 2: Install and configure Ollama ​

bash
brew install ollama
ollama pull nomic-embed-text

Ollama runs natively on your Mac (Apple Silicon optimized) and serves /v1/embeddings on localhost:11434.

Step 3: Bridge Ollama into the cluster ​

Since Ollama runs on the host (not inside kind), create a Service + Endpoints bridge:

bash
kubectl apply -f config/samples/ollama-embedding-bridge.yaml

This creates a ollama Service in the default namespace that forwards traffic to your host's Ollama instance. You may need to update the IP in the Endpoints resource:

bash
# Find your Docker gateway IP
docker run --rm alpine nslookup host.docker.internal
# Update the IP in ollama-embedding-bridge.yaml if it differs from 192.168.65.254

Step 4: Install CRDs ​

bash
make install

Step 5: Apply the demo KnowledgeBase ​

bash
kubectl apply -f config/samples/knowledgebase-demo.yaml

This creates:

  • A Secret with a dummy API key (Ollama doesn't need auth)
  • A ModelProvider (ollama-embed) pointing at nomic-embed-text with baseURL: http://ollama.default.svc:11434
  • A ModelSelector (local-embeddings) routing to the Ollama provider
  • A ConfigMap (demo-knowledge) with 3 sample documents
  • A KnowledgeBase (demo-kb) with ConfigMap ingestion (dimensions auto-discovered from the model)

Step 6: Run the operator ​

bash
make run

The KnowledgeBase controller will:

  1. Create a dedicated kb-qdrant-demo-kb StatefulSet + headless Service
  2. Probe the embedding model, discover its output dimension, create the demo-kb collection
  3. Read the demo-knowledge ConfigMap
  4. Chunk the 3 documents, embed via Ollama, upsert into Qdrant
  5. Update status with ready: true, document/chunk counts

Step 7: Verify ​

bash
# Check KnowledgeBase status
kubectl get knowledgebase demo-kb -o yaml

# Check Qdrant is running (each KB gets its own StatefulSet)
kubectl get statefulset kb-qdrant-demo-kb
kubectl get svc kb-qdrant-demo-kb

# Check Qdrant has data (port-forward to HTTP API)
kubectl port-forward svc/kb-qdrant-demo-kb 6333:6333
curl http://localhost:6333/collections/demo-kb

Access Control ​

Each KnowledgeBase manages its own access list via spec.allowedAgents. Access is denied by default — an agent can only use a KnowledgeBase if its name appears in the list, regardless of what the agent's spec.knowledgeBases field declares.

yaml
apiVersion: agentorca.agentorca.io/v1alpha1
kind: KnowledgeBase
metadata:
  name: my-kb
spec:
  allowedAgents:
    - rag-assistant
    - search-agent
  embedding:
    modelSelectorRef: embeddings

The operator enforces this at two points:

  1. AgentRun/AgentDeployment creation time — the run is rejected before the pod is scheduled if the agent is not in allowedAgents.
  2. Runtime — the operator API verifies the calling agent's SA identity via TokenReview and returns 403 Forbidden for unauthorised requests.

Migration from Shared Qdrant ​

Prior to this change, all KnowledgeBases in a namespace shared a single agentorca-qdrant StatefulSet. The legacy StatefulSet is not deleted by the operator — existing clusters keep their data intact. New KnowledgeBases (and re-creates of existing ones) will receive their own dedicated kb-qdrant-<name> instance.

To migrate existing data, port-forward the old instance, export collections via the Qdrant snapshot API, and import them into the new per-KB instance.


ModelProvider baseURL Field ​

For self-hosted models (Ollama, vLLM, etc.), set baseURL on the ModelProvider to override the default endpoint derived from the LiteLLM model prefix:

yaml
apiVersion: agentorca.agentorca.io/v1alpha1
kind: ModelProvider
metadata:
  name: ollama-embed
spec:
  litellmModel: "ollama/nomic-embed-text"
  baseURL: "http://ollama.default.svc:11434"   # overrides default localhost:11434
  credentialsRef:
    name: ollama-credentials
    key: api-key

Without baseURL, the operator maps prefixes to default endpoints:

PrefixDefault Endpoint
openai/https://api.openai.com
ollama/http://localhost:11434
gemini/https://generativelanguage.googleapis.com/v1beta/openai

Setting baseURL is required when the model runs in-cluster rather than on localhost.


Embedding Models ​

Any model that serves an OpenAI-compatible /v1/embeddings endpoint works. Common options:

ModelProviderDimensionsNotes
nomic-embed-textOllama768Free, runs locally on Apple Silicon. Requires queryPrompt / docPrompt (see below).
text-embedding-3-smallOpenAI1536Low cost, good quality. No prompt needed.
text-embedding-3-largeOpenAI3072Higher quality, higher cost. No prompt needed.

The dimensions field no longer exists in the KnowledgeBase spec. The controller probes the embedding model on first use and stores the discovered dimension in status.embeddingDimensions.


Prompt Prefixes for Open-Source Embedding Models ​

Some embedding models — notably nomic-embed-text via Ollama — require task-specific prefixes to produce retrieval-quality vectors. These models were trained with an instruction prefix prepended to the input text; without it, the embedding space is shifted and similarity search quality degrades significantly (or the model may return degenerate vectors).

agent-orca handles this via two optional fields on the ModelProvider CRD:

FieldApplied toExample
queryPromptQuery text before _rag_search"search_query: "
docPromptDocument chunks before _rag_ingest"search_document: "

When set, the EmbeddingClient prepends the relevant prefix to each input string before sending it to the model's /v1/embeddings endpoint. Both ingestion and search use the same ModelProvider, so the correct prefix is applied in each context automatically.

Configuration ​

yaml
apiVersion: agentorca.agentorca.io/v1alpha1
kind: ModelProvider
metadata:
  name: ollama-embed
spec:
  litellmModel: "ollama/nomic-embed-text"
  baseURL: "http://ollama-embed.agent-orca-system.svc:11434"
  credentialsRef:
    name: ollama-embed-credentials
    key: api-key
  capabilities:
    - embeddings
  queryPrompt: "search_query: "
  docPrompt: "search_document: "

When queryPrompt and docPrompt are omitted, raw text is sent as-is — this is suitable for hosted models like OpenAI's text-embedding-3-* which handle instructions internally and do not accept a prompt parameter in the OpenAI-compatible API.

Ollama nomic-embed-text Gotchas ​

  1. Task prefixes are required. Documents must be prefixed with search_document: and queries with search_query: . Set these as docPrompt and queryPrompt on your ModelProvider respectively. Skipping the prefixes degrades retrieval quality.

  2. Context window size. The model card lists 2,048 tokens, but the native context window is actually 8,192 tokens. Ollama's OpenAI-compatible /v1/embeddings endpoint does not accept a num_ctx parameter per request, so set it in your Ollama Modelfile instead:

    dockerfile
    FROM nomic-embed-text
    PARAMETER num_ctx 8192

    The bundled ollama-nomic-embed-text ModelProvider in charts/model-providers/values.yaml already includes the correct queryPrompt and docPrompt.

  3. No prompt field in the OpenAI-compatible embeddings API. The EmbeddingClient sends {"model": "...", "input": ["<prefix><text>"]} — the prefix is prepended to the input text itself, not sent as a separate request parameter. This works for both native Ollama OpenAI-compatible mode and hosted providers.


How It Works Internally ​

Ingestion Pipeline ​

Source Documents → Detect type → Extract text → Chunk (sentence-boundary, token-based) → Embed (batch /v1/embeddings) → Upsert (Qdrant gRPC)

All source ingestion (ConfigMaps, URLs, S3, MCP) runs in mcp-ingester Kubernetes Jobs, not in the operator — the controller only orchestrates Job lifecycle and queries Qdrant for counts. The pipeline:

  1. Type detection & extraction: internal/rag/extract detects the document type from magic bytes (with Content-Type/extension fallbacks) and extracts plain text. Supported: PDF (text layer), DOCX, PPTX, XLSX, HTML, and plain-text formats (markdown, JSON, YAML, CSV, XML). Base64-encoded binaries returned by MCP tools are decoded and retried. Documents that fail extraction are skipped with a warning.
  2. Chunking: Splits on sentence boundaries, respecting chunkSize tokens with chunkOverlap overlap. Uses word count as a token approximation (1 token ~ 1.33 words).
  3. Embedding: Batches up to 100 texts per /v1/embeddings call. If the ModelProvider has a docPrompt, it is prepended to each chunk text before embedding.
  4. Upsert: Each chunk gets a deterministic ID (sha256(docID:chunkIndex)), enabling idempotent re-ingestion. Points include payload: text, doc_id, chunk_index, plus any user metadata (including docType).

The controller ensures an egress NetworkPolicy for ingestion Job pods (agentorca-kb-ingest-<kb>) allowing DNS, the Kubernetes API, same-namespace services (Qdrant, MCP servers, in-cluster embedding providers), and HTTP/HTTPS. Add extra ports via ingestion.networkEgress. The policy is additive, so it is safe in namespaces without default-deny policies and required in namespaces that have them.

Search Flow ​

Query → Embed (with queryPrompt if set) → Qdrant nearest-neighbor (cosine) → Top-K chunks with scores

The query text is embedded via the same EmbeddingClient, using queryPrompt (if configured) instead of docPrompt. This ensures the query and document vectors share the same prompt-space alignment required by models like nomic-embed-text.

Key Files ​

FileRole
api/v1alpha1/knowledgebase_types.goKnowledgeBase CRD types
internal/rag/qdrant.goQdrant gRPC client wrapper
internal/rag/embedding.goOpenAI-compatible embedding client
internal/rag/chunker.goSentence-boundary document chunker
internal/rag/extract/Document type detection + text extraction (PDF, OOXML, HTML, text)
internal/rag/pipeline.goShared ingest pipeline (chunk → embed → upsert)
internal/controller/knowledgebase_controller.goReconciler: Qdrant deploy, collection create, ingestion Job orchestration
internal/controller/kb_mcp_ingest.goIngestion Job builders (MCP + static sources), ingester RBAC, NetworkPolicy
cmd/mcp-ingester/Ingestion Job binary: fetch sources, detect/extract, chunk, embed, upsert
internal/apiserver/apiserver.go/knowledgebase/.../search and /knowledgebase/.../ingest handlers
internal/controller/agentrun_controller.goResolves KB configs + injects RAG tools in buildRouterConfig

Integration Test ​

A standalone integration test exercises the full pipeline without Kubernetes:

bash
# Prerequisites: Qdrant on localhost:6334, Ollama with nomic-embed-text
docker run -d --name qdrant -p 6333:6333 -p 6334:6334 qdrant/qdrant:v1.17.1
ollama pull nomic-embed-text

# Run the test
go test -tags integration -v ./internal/rag/ -run TestRAGEndToEnd

The test ingests 3 documents, runs semantic queries, and verifies the top results match expected topics.

Released under the Apache 2.0 License.