Db

Code

1"""Run `uv pip install sqlalchemy openai` to install dependencies."""
2
3from kern.agent import Agent
4from kern.db.postgres import PostgresDb
5from kern.models.openai import OpenAIResponses
6from kern.tools.hackernews import HackerNewsTools
7
8# Setup the database
9db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
10db = PostgresDb(db_url=db_url)
11
12agent = Agent(
13 model=OpenAIResponses(id="gpt-5-mini"),
14 db=db,
15 tools=[HackerNewsTools()],
16 add_history_to_context=True,
17)
18agent.print_response("How many people live in Canada?")
19agent.print_response("What is their national anthem called?")

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

1python cookbook/11_models/openai/responses/db.py
1python cookbook\models\openai\responses\db.py

Install dependencies

1uv pip install -U openai kern-ai

Run Agent

1python cookbook/11_models/openai/responses/db.py