> ## Documentation Index
> Fetch the complete documentation index at: https://docs.definable.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Knowledge Overview

> Build RAG pipelines to give your agents access to custom data.

The Knowledge module provides a complete **Retrieval-Augmented Generation (RAG)** pipeline. Ingest documents from any source, chunk them into manageable pieces, generate embeddings, store them in a vector database, and retrieve the most relevant context at query time.

## The RAG Pipeline

**Ingestion (one-time)**

```mermaid theme={null}
flowchart LR
  Source --> Reader --> Chunker --> Embedder --> VectorDB
```

| Step     | Options                                                            |
| -------- | ------------------------------------------------------------------ |
| Source   | file, URL, string                                                  |
| Reader   | PDF, text, URL                                                     |
| Chunker  | text, recursive                                                    |
| Embedder | OpenAI, Voyage                                                     |
| VectorDB | InMemory, PgVector, Qdrant, ChromaDb, MongoDb, RedisDB, PineconeDb |

**Retrieval (per query)**

```mermaid theme={null}
flowchart LR
  Query --> Embedder --> VectorDB --> HybridMerge["Hybrid Merge"] --> Reranker --> TemporalDecay["Temporal Decay"] --> MMR["MMR Diversity"] --> Results
```

<Note>
  Hybrid merge, temporal decay, and MMR are optional. Without them, the pipeline is simply Vector Search → Reranker → Results. See [Hybrid Search & Scoring](/knowledge/hybrid-search) for details.
</Note>

## Quick Start

```python theme={null}
from definable.embedder import OpenAIEmbedder
from definable.knowledge import Knowledge
from definable.vectordb import InMemoryVectorDB

# Create a knowledge base
knowledge = Knowledge(
    vector_db=InMemoryVectorDB(),
    embedder=OpenAIEmbedder(),
)

# Add content
knowledge.add("Python is a programming language created by Guido van Rossum.")
knowledge.add("JavaScript is the language of the web.")

# Search
results = knowledge.search("Who created Python?")
for doc in results:
    print(f"[{doc.reranking_score or 'n/a'}] {doc.content}")
```

## Path Shorthand

For the quickest setup, pass a directory path directly to the Agent:

```python theme={null}
from definable.agent import Agent

agent = Agent(model="gpt-4o-mini", knowledge="./docs/")
```

This auto-configures InMemoryVectorDB + OpenAIEmbedder + RecursiveChunker and recursively loads all supported files. See [Agent Integration](/knowledge/agent-integration) for details.

## Adding Documents

The `add()` method accepts strings, file paths, or URLs. The appropriate reader is selected automatically:

```python theme={null}
# Plain text
knowledge.add("Definable is an AI agent framework.")

# From a text file
knowledge.add("/path/to/document.txt")

# From a PDF
knowledge.add("/path/to/report.pdf")

# From a URL
knowledge.add("https://example.com/article")
```

## Searching

```python theme={null}
results = knowledge.search(
    query="What is Definable?",
    top_k=5,         # Number of results
    rerank=True,     # Apply reranking (if reranker is configured)
)
```

Returns a list of `Document` objects sorted by relevance.

## Async Support

Every method has an async variant:

```python theme={null}
await knowledge.aadd("New content to index.")
results = await knowledge.asearch("Search query")
```

## Components

Each step in the pipeline is pluggable:

<CardGroup cols={2}>
  <Card title="Documents" icon="file-lines" href="/knowledge/documents">
    The core data unit — text content with metadata and embeddings.
  </Card>

  <Card title="Readers" icon="file-import" href="/knowledge/pipeline/readers">
    Read text, PDF, and web content into documents.
  </Card>

  <Card title="Chunkers" icon="scissors" href="/knowledge/pipeline/chunkers">
    Split large documents into smaller, overlapping chunks.
  </Card>

  <Card title="Embedders" icon="vector-square" href="/knowledge/pipeline/embedders">
    Generate vector embeddings with OpenAI, Voyage AI, Google, or Mistral.
  </Card>

  <Card title="Rerankers" icon="ranking-star" href="/knowledge/pipeline/rerankers">
    Rerank search results for higher relevance with Cohere or SentenceTransformer.
  </Card>

  <Card title="Vector Databases" icon="database" href="/knowledge/pipeline/vector-databases">
    Store and search embeddings with seven backends: InMemory, PgVector, Qdrant, ChromaDb, MongoDb, RedisDB, and PineconeDb.
  </Card>

  <Card title="Hybrid Search" icon="magnifying-glass-chart" href="/knowledge/hybrid-search">
    Combine vector + full-text search, MMR diversity, and temporal decay.
  </Card>

  <Card title="Agent Integration" icon="robot" href="/knowledge/agent-integration">
    Connect knowledge to agents via middleware or toolkits.
  </Card>
</CardGroup>

## Knowledge Parameters

<ParamField path="vector_db" type="VectorDB">
  Vector database for storing and searching embeddings. Defaults to `InMemoryVectorDB`.
</ParamField>

<ParamField path="embedder" type="Embedder">
  Embedding provider for converting text to vectors.
</ParamField>

<ParamField path="reranker" type="Reranker">
  Optional reranker for improving search result relevance.
</ParamField>

<ParamField path="chunker" type="Chunker">
  Text chunker for splitting documents. When `None` (default), documents are not chunked automatically. Pass a `RecursiveChunker` or `TextChunker` instance to enable chunking during `add()`.
</ParamField>

<ParamField path="readers" type="List[Reader]">
  Document readers. Defaults include `TextReader`, `PDFReader`, `URLReader`.
</ParamField>

<ParamField path="auto_detect_reader" type="bool" default="true">
  Automatically detect the correct reader based on the source.
</ParamField>

<ParamField path="fts_index" type="FTSIndex">
  Full-text search index for hybrid vector + keyword search. See [Hybrid Search](/knowledge/hybrid-search).
</ParamField>

<ParamField path="hybrid_config" type="HybridSearchConfig">
  Configuration for merging vector and full-text search results (weights, merge strategy).
</ParamField>

<ParamField path="temporal_decay" type="TemporalDecay">
  Exponential score decay based on document age. See [Hybrid Search](/knowledge/hybrid-search#temporal-decay).
</ParamField>

<ParamField path="mmr" type="MMRConfig">
  Maximal Marginal Relevance for diversity reranking. See [Hybrid Search](/knowledge/hybrid-search#mmr-maximal-marginal-relevance).
</ParamField>

## Managing Documents

```python theme={null}
# Add and get document IDs
doc_ids = knowledge.add("Some content")

# Remove specific documents
knowledge.remove(doc_ids)

# Clear everything
knowledge.clear()

# Count documents
print(len(knowledge))
```
