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:161import asyncio23from kern.agent import Agent4from kern.db.postgres import AsyncPostgresDb5from kern.models.openai import OpenAIResponses6from kern.team import Team7from kern.tools.hackernews import HackerNewsTools8from kern.tools.hackernews import HackerNewsTools9from kern.workflow.step import Step10from kern.workflow.workflow import Workflow1112db_url = "postgresql+psycopg_async://ai:ai@localhost:5532/ai"13db = AsyncPostgresDb(db_url=db_url)1415# Define agents16hackernews_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)2829# Define research team for complex analysis30research_team = Team(31 name="Research Team",32 members=[hackernews_agent, web_agent],33 instructions="Research tech topics from Hackernews and the web",34)3536content_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)4445# Define steps46research_step = Step(47 name="Research Step",48 team=research_team,49)5051content_planning_step = Step(52 name="Content Planning Step",53 agent=content_planner,54)5556# Create and use workflow57if __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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
db_url | Optional[str] | - | The database URL to connect to. |
db_engine | Optional[AsyncEngine] | - | The SQLAlchemy asyncdatabase engine to use. |
db_schema | Optional[str] | - | The database schema to use. |
session_table | Optional[str] | - | Name of the table to store Agent, Team and Workflow sessions. |
memory_table | Optional[str] | - | Name of the table to store memories. |
metrics_table | Optional[str] | - | Name of the table to store metrics. |
eval_table | Optional[str] | - | Name of the table to store evaluation runs data. |
knowledge_table | Optional[str] | - | Name of the table to store knowledge content. |
traces_table | Optional[str] | - | Name of the table to store traces. |
spans_table | Optional[str] | - | Name of the table to store spans. |