Async MongoDB for Workflow

Kern supports using MongoDB asynchronously as a storage backend for Workflows, with the AsyncMongoDb class.

Usage

Run MongoDB

Install docker desktop and run MongoDB on port 27017 using:

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

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
db_clientOptional[MongoClient]-The MongoDB client to use.
db_nameOptional[str]-The name of the database to use.
db_urlOptional[str]-The database URL to connect to.
session_collectionOptional[str]-Name of the collection to store sessions.
memory_collectionOptional[str]-Name of the collection to store memories.
metrics_collectionOptional[str]-Name of the collection to store metrics.
eval_collectionOptional[str]-Name of the collection to store evaluation runs.
knowledge_collectionOptional[str]-Name of the collection to store knowledge documents.
traces_collectionOptional[str]-Name of the collection to store traces.
spans_collectionOptional[str]-Name of the collection to store spans.