Redis for Agent
Kern supports using Redis as a storage backend for Agents using the RedisDb class.
Usage
Run Redis
Install docker desktop and run Redis on port 6379 using:
1docker run -d \2 --name my-redis \3 -p 6379:6379 \4 redis1from kern.agent import Agent2from kern.db.base import SessionType3from kern.db.redis import RedisDb4from kern.tools.hackernews import HackerNewsTools56# Initialize Redis db (use the right db_url for your setup)7db = RedisDb(db_url="redis://localhost:6379")89# Create agent with Redis db10agent = Agent(11 db=db,12 tools=[HackerNewsTools()],13 add_history_to_context=True,14)1516agent.print_response("How many people live in Canada?")17agent.print_response("What is their national anthem called?")1819# Verify db contents20print("\nVerifying db contents...")21all_sessions = db.get_sessions(session_type=SessionType.AGENT)22print(f"Total sessions in Redis: {len(all_sessions)}")2324if all_sessions:25 print("\nSession details:")26 session = all_sessions[0]27 print(f"The stored session: {session}")Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
redis_client | Optional[Redis] | - | Redis client instance to use. If not provided a new client will be created. |
db_url | Optional[str] | - | Redis connection URL (e.g., "redis://localhost:6379/0" or "rediss://user:pass@host:port/db") |
db_prefix | str | "kern" | Prefix for all Redis keys. |
expire | Optional[int] | - | TTL for Redis keys in seconds. |
session_table | Optional[str] | - | Name of the table to store 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. |
knowledge_table | Optional[str] | - | Name of the table to store knowledge documents. |
traces_table | Optional[str] | - | Name of the table to store traces. |
spans_table | Optional[str] | - | Name of the table to store spans. |