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 mongo1"""2Run `uv pip install kern-ai openai motor pymongo` to install dependencies.3"""4from kern.agent import Agent5from kern.db.mongo import AsyncMongoDb6from kern.models.openai import OpenAIResponses7from kern.team import Team8from kern.tools.hackernews import HackerNewsTools9from kern.tools.hackernews import HackerNewsTools10from kern.workflow.step import Step11from kern.workflow.workflow import Workflow1213db_url = "mongodb://localhost:27017"1415db = AsyncMongoDb(db_url=db_url)1617# Define agents18hackernews_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)3031# Define research team for complex analysis32research_team = Team(33 name="Research Team",34 members=[hackernews_agent, web_agent],35 instructions="Research tech topics from Hackernews and the web",36)3738content_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)4647# Define steps48research_step = Step(49 name="Research Step",50 team=research_team,51)5253content_planning_step = Step(54 name="Content Planning Step",55 agent=content_planner,56)5758# Create and use workflow59if __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
| 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. |