Async MySQL for Agent
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 uuid34from kern.agent import Agent5from kern.db.base import SessionType6from kern.db.mysql import AsyncMySQLDb7from kern.tools.hackernews import HackerNewsTools89db_url = "mysql+asyncmy://ai:ai@localhost:3306/ai"10db = AsyncMySQLDb(db_url=db_url)1112agent = Agent(13 db=db,14 tools=[HackerNewsTools()],15 add_history_to_context=True,16 add_datetime_to_context=True,17)1819async def main():20 """Run the agent queries in the same event loop"""21 session_id = str(uuid.uuid4())22 await agent.aprint_response(23 "How many people live in Canada?", session_id=session_id24 )25 await agent.aprint_response(26 "What is their national anthem called?", session_id=session_id27 )28 session_data = await db.get_session(29 session_id=session_id, session_type=SessionType.AGENT30 )31 print("\n=== SESSION DATA ===")32 print(session_data.to_dict())3334if __name__ == "__main__":35 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. |