ChunkingStrategy and implement chunk() to return a list of Document objects.
1
Create a Python file
custom_chunking.py
from pathlib import Path
from typing import List
from agno.agent import Agent
from agno.knowledge.chunking.strategy import ChunkingStrategy
from agno.knowledge.document import Document
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.text_reader import TextReader
from agno.vectordb.pgvector import PgVector
class CustomChunking(ChunkingStrategy):
def __init__(self, separator: str = "---"):
self.separator = separator
def chunk(self, document: Document) -> List[Document]:
result = []
for chunk_content in document.content.split(self.separator):
chunk_content = self.clean_text(chunk_content).strip()
if not chunk_content:
continue
chunk_number = len(result) + 1
meta_data = document.meta_data.copy()
meta_data["chunk"] = chunk_number
meta_data["chunk_size"] = len(chunk_content)
result.append(
Document(
id=self._generate_chunk_id(
document,
chunk_number,
chunk_content,
),
name=document.name,
meta_data=meta_data,
content=chunk_content,
)
)
return result
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
knowledge = Knowledge(
vector_db=PgVector(table_name="recipes_custom_chunking", db_url=db_url),
)
content_path = Path("tmp/recipes.txt")
content_path.parent.mkdir(parents=True, exist_ok=True)
content_path.write_text(
"Tom kha gai uses coconut milk.\n---\nMassaman curry uses warm spices.",
encoding="utf-8",
)
knowledge.insert(
path=str(content_path),
reader=TextReader(
name="Custom Chunking Reader",
chunking_strategy=CustomChunking(separator="---"),
),
)
agent = Agent(
knowledge=knowledge,
search_knowledge=True,
)
agent.print_response("Which recipe uses coconut milk?", markdown=True)
2
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
3
Install dependencies
uv pip install -U agno openai pgvector psycopg sqlalchemy
4
Export your OpenAI API key
Set OpenAI Key
Set yourOPENAI_API_KEY as an environment variable. You can get one from OpenAI.export OPENAI_API_KEY=sk-***
setx OPENAI_API_KEY sk-***
5
Run PgVector
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql \
-v pgvolume:/var/lib/postgresql \
-p 5532:5432 \
--name pgvector \
agnohq/pgvector:18
docker run -d `
-e POSTGRES_DB=ai `
-e POSTGRES_USER=ai `
-e POSTGRES_PASSWORD=ai `
-e PGDATA=/var/lib/postgresql `
-v pgvolume:/var/lib/postgresql `
-p 5532:5432 `
--name pgvector `
agnohq/pgvector:18
6
Run the script
python custom_chunking.py
Custom Chunking Params
| Parameter | Type | Default | Description |
|---|---|---|---|
separator | str | "---" | The string used to split the document content into chunks. |