Knowledge

Code

1from kern.agent import Agent
2from kern.knowledge.embedder.ollama import OllamaEmbedder
3from kern.knowledge.knowledge import Knowledge
4from kern.models.ollama import Ollama
5from kern.vectordb.pgvector import PgVector
6
7db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
8
9knowledge = Knowledge(
10 vector_db=PgVector(
11 table_name="recipes",
12 db_url=db_url,
13 embedder=OllamaEmbedder(id="llama3.2", dimensions=3072),
14 ),
15)
16# Add content to the knowledge
17knowledge.insert(
18 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
19)
20
21agent = Agent(model=Ollama(id="llama3.2"), knowledge=knowledge)
22agent.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

Install Ollama

Follow the Ollama installation guide and run:

1ollama pull llama3.2

Install dependencies

1uv pip install -U kern-ai sqlalchemy pgvector pypdf openai ollama

Run Agent

1python cookbook/11_models/ollama/knowledge.py