Postgres for Workflows

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

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
db_urlOptional[str]-The database URL to connect to.
db_engineOptional[Engine]-The SQLAlchemy database 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.