Agentic Chunking with Custom Prompt

Use custom prompts to determine personalized chunk breakpoints.

1from kern.agent import Agent
2from kern.knowledge.chunking.agentic import AgenticChunking
3from kern.knowledge.knowledge import Knowledge
4from kern.knowledge.reader.pdf_reader import PDFReader
5from kern.vectordb.pgvector import PgVector
6
7db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
8
9knowledge = Knowledge(
10 vector_db=PgVector(table_name="recipes_custom_agentic_chunking", db_url=db_url),
11)
12
13knowledge.insert(
14 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
15 reader=PDFReader(
16 name="Custom Agentic Chunking Reader",
17 chunking_strategy=AgenticChunking(
18 custom_prompt="""
19 Split this recipe document at natural recipe boundaries.
20 Keep each complete recipe (ingredients and instructions) in one chunk.
21 Break between different recipes, not within a single recipe.
22 """,
23 max_chunk_size=3000,
24 ),
25 ),
26)
27
28agent = Agent(
29 knowledge=knowledge,
30 search_knowledge=True,
31)
32
33agent.print_response("How to make Thai curry?", markdown=True)

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/07_knowledge/chunking
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9# Optional: Run PgVector (needs docker)
10./cookbook/scripts/run_pgvector.sh
11
12python agentic_chunking_custom_prompt.py