Async Postgres for Workflows

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
2
3from kern.agent import Agent
4from kern.db.postgres import AsyncPostgresDb
5from kern.models.openai import OpenAIResponses
6from kern.team import Team
7from kern.tools.hackernews import HackerNewsTools
8from kern.tools.hackernews import HackerNewsTools
9from kern.workflow.step import Step
10from kern.workflow.workflow import Workflow
11
12db_url = "postgresql+psycopg_async://ai:ai@localhost:5532/ai"
13db = AsyncPostgresDb(db_url=db_url)
14
15# Define agents
16hackernews_agent = Agent(
17 name="Hackernews Agent",
18 model=OpenAIResponses(id="gpt-5.2"),
19 tools=[HackerNewsTools()],
20 role="Extract key insights and content from Hackernews posts",
21)
22web_agent = Agent(
23 name="Web Agent",
24 model=OpenAIResponses(id="gpt-5.2"),
25 tools=[HackerNewsTools()],
26 role="Search the web for the latest news and trends",
27)
28
29# Define research team for complex analysis
30research_team = Team(
31 name="Research Team",
32 members=[hackernews_agent, web_agent],
33 instructions="Research tech topics from Hackernews and the web",
34)
35
36content_planner = Agent(
37 name="Content Planner",
38 model=OpenAIResponses(id="gpt-5.2"),
39 instructions=[
40 "Plan a content schedule over 4 weeks for the provided topic and research content",
41 "Ensure that I have posts for 3 posts per week",
42 ],
43)
44
45# Define steps
46research_step = Step(
47 name="Research Step",
48 team=research_team,
49)
50
51content_planning_step = Step(
52 name="Content Planning Step",
53 agent=content_planner,
54)
55
56# Create and use workflow
57if __name__ == "__main__":
58 content_creation_workflow = Workflow(
59 name="Content Creation Workflow",
60 description="Automated content creation from blog posts to social media",
61 db=db,
62 steps=[research_step, content_planning_step],
63 )
64 asyncio.run(
65 content_creation_workflow.aprint_response(
66 input="AI trends in 2024",
67 markdown=True,
68 )
69 )

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.