MongoDB for Workflow

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

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.