Agent with Knowledge Base
Code
1from kern.agent import Agent2from kern.knowledge.knowledge import Knowledge3from kern.models.cerebras import Cerebras4from kern.vectordb.pgvector import PgVector56db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"78knowledge = Knowledge(9 vector_db=PgVector(table_name="recipes", db_url=db_url),10)11# Add content to the knowledge12knowledge.insert(13 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"14)1516agent = 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.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateSet your API key
1export CEREBRAS_API_KEY=xxxInstall dependencies
1uv pip install -U kern-ai sqlalchemy pgvector pypdf cerebras_cloud_sdkStart 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.py1python cookbook/11_models/cerebras/basic_knowledge.pySubsequent Runs
After the first run, comment out or remove knowledge_base.load(recreate=True) to avoid reloading the PDF each time.