Async Postgres for Team

Kern supports using PostgreSQL asynchronously, with the AsyncPostgresDb class.

Usage

Run PgVector

Install docker desktop and run PgVector on port 5532 using:

1docker run -d \
2 -e POSTGRES_DB=ai \
3 -e POSTGRES_USER=ai \
4 -e POSTGRES_PASSWORD=ai \
5 -e PGDATA=/var/lib/postgresql/data/pgdata \
6 -v pgvolume:/var/lib/postgresql/data \
7 -p 5532:5432 \
8 --name pgvector \
9 agnohq/pgvector:16
1import asyncio
2from typing import List
3
4from kern.agent import Agent
5from kern.db.postgres import AsyncPostgresDb
6from kern.models.openai import OpenAIResponses
7from kern.team import Team
8from kern.tools.hackernews import HackerNewsTools
9from kern.tools.hackernews import HackerNewsTools
10from pydantic import BaseModel
11
12db_url = "postgresql+psycopg_async://ai:ai@localhost:5532/ai"
13db = AsyncPostgresDb(db_url=db_url)
14
15class Article(BaseModel):
16 title: str
17 summary: str
18 reference_links: List[str]
19
20hn_researcher = Agent(
21 name="HackerNews Researcher",
22 model=OpenAIResponses(id="gpt-5.2"),
23 role="Gets top stories from hackernews.",
24 tools=[HackerNewsTools()],
25)
26
27web_searcher = Agent(
28 name="Web Searcher",
29 model=OpenAIResponses(id="gpt-5.2"),
30 role="Searches the web for information on a topic",
31 tools=[HackerNewsTools()],
32 add_datetime_to_context=True,
33)
34
35hn_team = Team(
36 name="HackerNews Team",
37 model=OpenAIResponses(id="gpt-5.2"),
38 members=[hn_researcher, web_searcher],
39 db=db,
40 instructions=[
41 "First, search hackernews for what the user is asking about.",
42 "Then, ask the web searcher to search for each story to get more information.",
43 "Finally, provide a thoughtful and engaging summary.",
44 ],
45 output_schema=Article,
46 markdown=True,
47 show_members_responses=True,
48)
49
50asyncio.run(
51 hn_team.aprint_response("Write an article about the top 2 stories on hackernews")
52)

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
db_urlOptional[str]-The database URL to connect to.
db_engineOptional[AsyncEngine]-The SQLAlchemy asyncdatabase engine to use.
db_schemaOptional[str]-The database schema to use.
session_tableOptional[str]-Name of the table to store Agent, Team and Workflow sessions.
memory_tableOptional[str]-Name of the table to store memories.
metrics_tableOptional[str]-Name of the table to store metrics.
eval_tableOptional[str]-Name of the table to store evaluation runs data.
knowledge_tableOptional[str]-Name of the table to store knowledge content.
traces_tableOptional[str]-Name of the table to store traces.
spans_tableOptional[str]-Name of the table to store spans.