Redis Integration for Microsoft Agent Framework
agent-framework-redis provides robust Redis integration for the Microsoft Agent Framework, offering persistent storage for conversational history and a flexible context provider for long-term memory. It leverages Redis for efficient, thread-safe chat message storage and advanced context management, including optional vector search capabilities. The library is part of the broader Microsoft Agent Framework ecosystem and is actively developed, with its release cadence tied to the main framework.
Warnings
- gotcha The current version `1.0.0b260409` indicates a beta release. While the core `agent-framework` is stable (v1.0.0), specific integration packages like `agent-framework-redis` might still undergo API changes or have unfinalized features before a stable release. Always refer to the latest official documentation.
- breaking The broader Microsoft Agent Framework (which `agent-framework-redis` is a part of) underwent significant breaking changes leading up to its 1.0.0 stable release, particularly in `agent-framework-core` and `agent-framework-openai`. Although `agent-framework-redis` itself wasn't explicitly listed with all breaking changes, updates to the parent framework could implicitly affect its usage or require dependency version bumps.
- gotcha When initializing `RedisChatMessageStore` or `RedisProvider`, you must provide either `redis_url` or a combination of `credential_provider` (for Azure AD auth) and `host`. Providing both `redis_url` and `credential_provider`, or neither, will raise a `ValueError`.
- gotcha The library offers `RedisChatMessageStore` for managing conversational chat history and `RedisProvider` for general context management and long-term memory, potentially with vector search. Confusing these or using the wrong class for a specific memory pattern is a common footgun.
Install
-
pip install agent-framework-redis
Imports
- RedisChatMessageStore
from agent_framework.redis import RedisChatMessageStore
- RedisProvider
from agent_framework.redis import RedisProvider
Quickstart
import asyncio
import os
from agent_framework.redis import RedisChatMessageStore
from agent_framework.core.message import Message, Role
async def main():
# Connect to Redis. Replace with your Redis URL or environment variable.
# For local Redis, use redis://localhost:6379
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379')
session_id = 'my-unique-conversation-123'
# Initialize the Redis chat message store
# max_messages can be set to limit history, None for unlimited
store = RedisChatMessageStore(
redis_url=redis_url,
thread_id=session_id,
max_messages=10 # Example: retain last 10 messages
)
print(f"Storing messages for session: {session_id}")
# Add messages to the store
await store.add_messages([
Message(role=Role.USER, content='Hello, agent!', author_name='User'),
Message(role=Role.ASSISTANT, content='Hi there! How can I help you?', author_name='Agent')
])
# Retrieve messages from the store
retrieved_messages = await store.get_messages()
print("\nRetrieved messages:")
for msg in retrieved_messages:
print(f"[{msg.author_name} ({msg.role.value})]: {msg.content}")
# Add more messages to test trimming
for i in range(1, 15):
await store.add_messages([
Message(role=Role.USER, content=f'Message {i}', author_name=f'User{i}')
])
print(f"\nRetrieved messages after adding more (max_messages={store.max_messages}):")
trimmed_messages = await store.get_messages()
for msg in trimmed_messages:
print(f"[{msg.author_name} ({msg.role.value})]: {msg.content}")
# Clear the session history
await store.clear()
print("\nMessages cleared. Current messages:")
print(await store.get_messages())
if __name__ == '__main__':
asyncio.run(main())