Async MongoDB for Team
Kern supports using MongoDB asynchronously as a storage backend for Teams, with the AsyncMongoDb class.
Usage
You need to provide either db_url or client. The following example uses db_url.
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 openai pymongo motor` to install dependencies3"""4from typing import List56from kern.agent import Agent7from kern.db.mongo import AsyncMongoDb8from kern.models.openai import OpenAIResponses9from kern.team import Team10from kern.tools.hackernews import HackerNewsTools11from kern.tools.hackernews import HackerNewsTools12from pydantic import BaseModel1314# MongoDB connection settings15db_url = "mongodb://localhost:27017"16db = AsyncMongoDb(db_url=db_url)1718class Article(BaseModel):19 title: str20 summary: str21 reference_links: List[str]2223hn_researcher = Agent(24 name="HackerNews Researcher",25 model=OpenAIResponses(id="gpt-5.2"),26 role="Gets top stories from hackernews.",27 tools=[HackerNewsTools()],28)2930web_searcher = Agent(31 name="Web Searcher",32 model=OpenAIResponses(id="gpt-5.2"),33 role="Searches the web for information on a topic",34 tools=[HackerNewsTools()],35 add_datetime_to_context=True,36)3738hn_team = Team(39 name="HackerNews Team",40 model=OpenAIResponses(id="gpt-5.2"),41 members=[hn_researcher, web_searcher],42 db=db,43 instructions=[44 "First, search hackernews for what the user is asking about.",45 "Then, ask the web searcher to search for each story to get more information.",46 "Finally, provide a thoughtful and engaging summary.",47 ],48 output_schema=Article,49 markdown=True,50 debug_mode=True,51 show_members_responses=True,52)5354hn_team.print_response("Write an article about the top 2 stories on hackernews")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. |