Async MySQL for Team
Kern supports using MySQL asynchronously, with the AsyncMySQLDb class.
Usage
Run MySQL
Install docker desktop and run MySQL on port 3306 using:
1docker run -d \2 --name mysql \3 -e MYSQL_ROOT_PASSWORD=ai \4 -e MYSQL_DATABASE=ai \5 -e MYSQL_USER=ai \6 -e MYSQL_PASSWORD=ai \7 -p 3306:3306 \8 -d mysql:81import asyncio2import uuid3from typing import List45from kern.agent import Agent6from kern.db.base import SessionType7from kern.db.mysql import AsyncMySQLDb8from kern.team import Team9from kern.tools.hackernews import HackerNewsTools10from kern.tools.hackernews import HackerNewsTools11from pydantic import BaseModel1213db_url = "mysql+asyncmy://ai:ai@localhost:3306/ai"14db = AsyncMySQLDb(db_url=db_url)1516class Article(BaseModel):17 title: str18 summary: str19 reference_links: List[str]2021hn_researcher = Agent(22 name="HackerNews Researcher",23 role="Gets top stories from hackernews.",24 tools=[HackerNewsTools()],25)2627web_searcher = Agent(28 name="Web Searcher",29 role="Searches the web for information on a topic",30 tools=[HackerNewsTools()],31 add_datetime_to_context=True,32)3334hn_team = Team(35 name="HackerNews Team",36 members=[hn_researcher, web_searcher],37 db=db,38 instructions=[39 "First, search hackernews for what the user is asking about.",40 "Then, ask the web searcher to search for each story to get more information.",41 "Finally, provide a thoughtful and engaging summary.",42 ],43 output_schema=Article,44 markdown=True,45 show_members_responses=True,46)4748async def main():49 """Run the agent queries in the same event loop"""50 session_id = str(uuid.uuid4())51 await hn_team.aprint_response(52 "Write an article about the top 2 stories on hackernews", session_id=session_id53 )54 session_data = await db.get_session(55 session_id=session_id, session_type=SessionType.TEAM56 )57 print("\n=== SESSION DATA ===")58 print(session_data.to_dict())5960if __name__ == "__main__":61 asyncio.run(main())Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
db_url | Optional[str] | - | The database URL to connect to. |
db_engine | Optional[AsyncEngine] | - | The SQLAlchemy async database engine to use. |
db_schema | Optional[str] | - | The database schema to use. |
session_table | Optional[str] | - | Name of the table to store Agent, Team and Workflow sessions. |
memory_table | Optional[str] | - | Name of the table to store memories. |
metrics_table | Optional[str] | - | Name of the table to store metrics. |
eval_table | Optional[str] | - | Name of the table to store evaluation runs data. |
knowledge_table | Optional[str] | - | Name of the table to store knowledge content. |