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:8
1import asyncio
2import uuid
3
4from kern.agent import Agent
5from kern.db.base import SessionType
6from kern.db.mysql import AsyncMySQLDb
7from kern.tools.hackernews import HackerNewsTools
8
9db_url = "mysql+asyncmy://ai:ai@localhost:3306/ai"
10db = AsyncMySQLDb(db_url=db_url)
11
12agent = Agent(
13 db=db,
14 tools=[HackerNewsTools()],
15 add_history_to_context=True,
16 add_datetime_to_context=True,
17)
18
19async 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_id
24 )
25 await agent.aprint_response(
26 "What is their national anthem called?", session_id=session_id
27 )
28 session_data = await db.get_session(
29 session_id=session_id, session_type=SessionType.AGENT
30 )
31 print("\n=== SESSION DATA ===")
32 print(session_data.to_dict())
33
34if __name__ == "__main__":
35 asyncio.run(main())

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
db_urlOptional[str]-The database URL to connect to.
db_engineOptional[AsyncEngine]-The SQLAlchemy async database engine to use.
db_schemaOptional[str]-The database schema to use.
session_tableOptional[str]-Name of the table to store Agent, Team and Workflow sessions.
memory_tableOptional[str]-Name of the table to store memories.
metrics_tableOptional[str]-Name of the table to store metrics.
eval_tableOptional[str]-Name of the table to store evaluation runs data.
knowledge_tableOptional[str]-Name of the table to store knowledge content.