autogen_ext.memory.redis#
- pydantic model RedisMemoryConfig[source]#
Bases:
BaseModel
Configuration for Redis-based vector memory.
This class defines the configuration options for using Redis as a vector memory store, supporting semantic memory. It allows customization of the Redis connection, index settings, similarity search parameters, and embedding model.
Show JSON schema
{ "title": "RedisMemoryConfig", "description": "Configuration for Redis-based vector memory.\n\nThis class defines the configuration options for using Redis as a vector memory store,\nsupporting semantic memory. It allows customization of the Redis connection, index settings,\nsimilarity search parameters, and embedding model.", "type": "object", "properties": { "redis_url": { "default": "redis://localhost:6379", "description": "url of the Redis instance", "title": "Redis Url", "type": "string" }, "index_name": { "default": "chat_history", "description": "Name of the Redis collection", "title": "Index Name", "type": "string" }, "prefix": { "default": "memory", "description": "prefix of the Redis collection", "title": "Prefix", "type": "string" }, "distance_metric": { "default": "cosine", "enum": [ "cosine", "ip", "l2" ], "title": "Distance Metric", "type": "string" }, "algorithm": { "default": "flat", "enum": [ "flat", "hnsw" ], "title": "Algorithm", "type": "string" }, "top_k": { "default": 10, "description": "Number of results to return in queries", "title": "Top K", "type": "integer" }, "datatype": { "default": "float32", "enum": [ "uint8", "int8", "float16", "float32", "float64", "bfloat16" ], "title": "Datatype", "type": "string" }, "distance_threshold": { "default": 0.7, "description": "Minimum similarity score threshold", "title": "Distance Threshold", "type": "number" }, "model_name": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": "sentence-transformers/all-mpnet-base-v2", "description": "Embedding model name", "title": "Model Name" } } }
- Fields:
algorithm (Literal['flat', 'hnsw'])
datatype (Literal['uint8', 'int8', 'float16', 'float32', 'float64', 'bfloat16'])
distance_metric (Literal['cosine', 'ip', 'l2'])
distance_threshold (float)
index_name (str)
model_name (str | None)
prefix (str)
redis_url (str)
top_k (int)
- class RedisMemory(config: RedisMemoryConfig | None = None)[source]#
Bases:
Memory
,Component
[RedisMemoryConfig
]Store and retrieve memory using vector similarity search powered by RedisVL.
RedisMemory provides a vector-based memory implementation that uses RedisVL for storing and retrieving content based on semantic similarity. It enhances agents with the ability to recall contextually relevant information during conversations by leveraging vector embeddings to find similar content.
This implementation requires the RedisVL extra to be installed. Install with:
pip install "autogen-ext[redisvl]"
Additionally, you will need access to a Redis instance. To run a local instance of redis in docker:
docker run -d --name redis -p 6379:6379 redis:8
To download and run Redis locally:
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list sudo apt-get update > /dev/null 2>&1 sudo apt-get install redis-server > /dev/null 2>&1 redis-server --daemonize yes
- Parameters:
config (RedisMemoryConfig | None) – Configuration for the Redis memory. If None, defaults to a RedisMemoryConfig with recommended settings.
Example
from logging import WARNING, getLogger import asyncio from autogen_agentchat.agents import AssistantAgent from autogen_agentchat.ui import Console from autogen_core.memory import MemoryContent, MemoryMimeType from autogen_ext.memory.redis import RedisMemory, RedisMemoryConfig from autogen_ext.models.openai import OpenAIChatCompletionClient logger = getLogger() logger.setLevel(WARNING) # Define tool to use async def get_weather(city: str, units: str = "imperial") -> str: if units == "imperial": return f"The weather in {city} is 73 °F and Sunny." elif units == "metric": return f"The weather in {city} is 23 °C and Sunny." else: return f"Sorry, I don't know the weather in {city}." async def main(): # Initailize Redis memory redis_memory = RedisMemory( config=RedisMemoryConfig( redis_url="redis://localhost:6379", index_name="chat_history", prefix="memory", ) ) # Add user preferences to memory await redis_memory.add( MemoryContent( content="The weather should be in metric units", mime_type=MemoryMimeType.TEXT, metadata={"category": "preferences", "type": "units"}, ) ) await redis_memory.add( MemoryContent( content="Meal recipe must be vegan", mime_type=MemoryMimeType.TEXT, metadata={"category": "preferences", "type": "dietary"}, ) ) model_client = OpenAIChatCompletionClient( model="gpt-4o", ) # Create assistant agent with ChromaDB memory assistant_agent = AssistantAgent( name="assistant_agent", model_client=model_client, tools=[get_weather], memory=[redis_memory], ) stream = assistant_agent.run_stream(task="What is the weather in New York?") await Console(stream) await model_client.close() await redis_memory.close() asyncio.run(main())
Output:
---------- TextMessage (user) ---------- What is the weather in New York? ---------- MemoryQueryEvent (assistant_agent) ---------- [MemoryContent(content='The weather should be in metric units', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata={'category': 'preferences', 'type': 'units'})] ---------- ToolCallRequestEvent (assistant_agent) ---------- [FunctionCall(id='call_tyCPvPPAV4SHWhtfpM6UMemr', arguments='{"city":"New York","units":"metric"}', name='get_weather')] ---------- ToolCallExecutionEvent (assistant_agent) ---------- [FunctionExecutionResult(content='The weather in New York is 23 °C and Sunny.', name='get_weather', call_id='call_tyCPvPPAV4SHWhtfpM6UMemr', is_error=False)] ---------- ToolCallSummaryMessage (assistant_agent) ---------- The weather in New York is 23 °C and Sunny.
- component_config_schema#
alias of
RedisMemoryConfig
- component_provider_override: ClassVar[str | None] = 'autogen_ext.memory.redis_memory.RedisMemory'#
Override the provider string for the component. This should be used to prevent internal module names being a part of the module name.
- async update_context(model_context: ChatCompletionContext) UpdateContextResult [source]#
Update the model context with relevant memory content.
This method retrieves memory content relevant to the last message in the context and adds it as a system message. This implementation uses the last message in the context as a query to find semantically similar memories and adds them all to the context as a single system message.
- Parameters:
model_context (ChatCompletionContext) – The model context to update with relevant memories.
- Returns:
UpdateContextResult – Object containing the memories that were used to update the context.
- async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None [source]#
Add a memory content object to Redis.
Note
To perform semantic search over stored memories RedisMemory creates a vector embedding from the content field of a MemoryContent object. This content is assumed to be text, JSON, or Markdown, and is passed to the vector embedding model specified in RedisMemoryConfig.
- Parameters:
content (MemoryContent) – The memory content to store within Redis.
cancellation_token (CancellationToken) – Token passed to cease operation. Not used.
- async query(query: str | MemoryContent, cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult [source]#
Query memory content based on semantic vector similarity.
Note
RedisMemory.query() supports additional keyword arguments to improve query performance. top_k (int): The maximum number of relevant memories to include. Defaults to 10. distance_threshold (float): The maximum distance in vector space to consider a memory semantically similar when performining cosine similarity search. Defaults to 0.7.
- Parameters:
query (str | MemoryContent) – query to perform vector similarity search with. If a string is passed, a vector embedding is created from it with the model specified in the RedisMemoryConfig. If a MemoryContent object is passed, the content field of this object is extracted and a vector embedding is created from it with the model specified in the RedisMemoryConfig.
cancellation_token (CancellationToken) – Token passed to cease operation. Not used.
- Returns:
memoryQueryResult – Object containing memories relevant to the provided query.