Agent with Knowledge Base

Code

1from kern.agent import Agent
2from kern.knowledge.knowledge import Knowledge
3from kern.models.cerebras import Cerebras
4from kern.vectordb.pgvector import PgVector
5
6db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
7
8knowledge = Knowledge(
9 vector_db=PgVector(table_name="recipes", db_url=db_url),
10)
11# Add content to the knowledge
12knowledge.insert(
13 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
14)
15
16agent = Agent(model=Cerebras(id="llama-4-scout-17b-16e-instruct"), knowledge=knowledge)
17agent.print_response("How to make Thai curry?", markdown=True)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export CEREBRAS_API_KEY=xxx

Install dependencies

1uv pip install -U kern-ai sqlalchemy pgvector pypdf cerebras_cloud_sdk

Start your Postgres server

Ensure your Postgres server is running and accessible at the connection string used in db_url.

Run Agent (first time)

The first run will load and index the PDF. This may take a while.

1python cookbook/11_models/cerebras/basic_knowledge.py
1python cookbook/11_models/cerebras/basic_knowledge.py

Subsequent Runs

After the first run, comment out or remove knowledge_base.load(recreate=True) to avoid reloading the PDF each time.