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 mongo1from kern.agent import Agent2from kern.db.mongodb import MongoDBDb3from kern.models.openai import OpenAIResponses4from kern.team import Team5from kern.tools.hackernews import HackerNewsTools6from kern.tools.hackernews import HackerNewsTools7from kern.workflow.step import Step8from kern.workflow.workflow import Workflow910db_url = "mongodb://localhost:27017"1112db = MongoDb(db_url=db_url)1314# Define agents15hackernews_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)2728# Define research team for complex analysis29research_team = Team(30 name="Research Team",31 members=[hackernews_agent, web_agent],32 instructions="Research tech topics from Hackernews and the web",33)3435content_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)4344# Define steps45research_step = Step(46 name="Research Step",47 team=research_team,48)4950content_planning_step = Step(51 name="Content Planning Step",52 agent=content_planner,53)5455# Create and use workflow56if __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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
db_client | Optional[MongoClient] | - | The MongoDB client to use. |
db_name | Optional[str] | - | The name of the database to use. |
db_url | Optional[str] | - | The database URL to connect to. |
session_collection | Optional[str] | - | Name of the collection to store sessions. |
memory_collection | Optional[str] | - | Name of the collection to store memories. |
metrics_collection | Optional[str] | - | Name of the collection to store metrics. |
eval_collection | Optional[str] | - | Name of the collection to store evaluation runs. |
knowledge_collection | Optional[str] | - | Name of the collection to store knowledge documents. |
traces_collection | Optional[str] | - | Name of the collection to store traces. |
spans_collection | Optional[str] | - | Name of the collection to store spans. |