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 redis
1from kern.agent import Agent
2from kern.db.base import SessionType
3from kern.db.redis import RedisDb
4from kern.tools.hackernews import HackerNewsTools
5
6# Initialize Redis db (use the right db_url for your setup)
7db = RedisDb(db_url="redis://localhost:6379")
8
9# Create agent with Redis db
10agent = Agent(
11 db=db,
12 tools=[HackerNewsTools()],
13 add_history_to_context=True,
14)
15
16agent.print_response("How many people live in Canada?")
17agent.print_response("What is their national anthem called?")
18
19# Verify db contents
20print("\nVerifying db contents...")
21all_sessions = db.get_sessions(session_type=SessionType.AGENT)
22print(f"Total sessions in Redis: {len(all_sessions)}")
23
24if all_sessions:
25 print("\nSession details:")
26 session = all_sessions[0]
27 print(f"The stored session: {session}")

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
redis_clientOptional[Redis]-Redis client instance to use. If not provided a new client will be created.
db_urlOptional[str]-Redis connection URL (e.g., "redis://localhost:6379/0" or "rediss://user:pass@host:port/db")
db_prefixstr"kern"Prefix for all Redis keys.
expireOptional[int]-TTL for Redis keys in seconds.
session_tableOptional[str]-Name of the table to store 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.
knowledge_tableOptional[str]-Name of the table to store knowledge documents.
traces_tableOptional[str]-Name of the table to store traces.
spans_tableOptional[str]-Name of the table to store spans.