Agentic Chunking with Custom Prompt
Use custom prompts to determine personalized chunk breakpoints.
1from kern.agent import Agent2from kern.knowledge.chunking.agentic import AgenticChunking3from kern.knowledge.knowledge import Knowledge4from kern.knowledge.reader.pdf_reader import PDFReader5from kern.vectordb.pgvector import PgVector67db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"89knowledge = Knowledge(10 vector_db=PgVector(table_name="recipes_custom_agentic_chunking", db_url=db_url),11)1213knowledge.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)2728agent = Agent(29 knowledge=knowledge,30 search_knowledge=True,31)3233agent.print_response("How to make Thai curry?", markdown=True)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/07_knowledge/chunking45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89# Optional: Run PgVector (needs docker)10./cookbook/scripts/run_pgvector.sh1112python agentic_chunking_custom_prompt.py