本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将 Valkey 设置 ElastiCache 为代理记忆的矢量存储
以下演练展示了如何使用 Mem0 和 for Valkey 作为矢量存储来构建支持内存 ElastiCache的 AI 代理。
步骤 1:创建不带内存的基本代理
首先,安装 Strands Agents 并创建一个基本代理:
pip install strands-agents strands-agents-tools strands-agents-builder
使用用于浏览网页的 HTTP 工具初始化基本代理:
from strands import Agent from strands.tools import http_request # Initialize agent with access to the tool to browse the web agent = Agent(tools=[http_request]) # Format messages as expected by Strands formatted_messages = [ { "role": "user", "content": [{"text": "What is the URL for the project mem0 and its most important metrics?"}] } ] result = agent(formatted_messages)
如果没有内存,代理会为每个请求重复执行相同的研究任务。在测试中,代理调用三次工具来回复请求,使用大约 70,000 个代币,完成时间超过 9 秒。
第 2 步:为 Valkey 配置 Mem0 ElastiCache
使用 Valkey 矢量存储连接器安装 Mem0 库:
pip install mem0ai "mem0ai[vector_stores]"
将 Valkey 配置为矢量存储。 ElastiCache for Valkey 从 8.2 版开始支持矢量搜索功能:
from mem0 import Memory # Configure Mem0 with ElastiCache for Valkey config = { "vector_store": { "provider": "valkey", "config": { "valkey_url": "your-elasticache-cluster.cache.amazonaws.com:6379", "index_name": "agent_memory", "embedding_model_dims": 1024, "index_type": "flat" } } } m = Memory.from_config(config)
your-elasticache-cluster.cache.amazonaws.com替换为ElastiCache 集群的终端节点。有关查找集群终端节点的说明,请参阅访问您的 ElastiCache 集群。
步骤 3:向代理添加内存工具
创建代理可以用来存储和检索信息的内存工具。@tool装饰器将常规 Python 函数转换为代理可以调用的工具:
from strands import Agent, tool from strands.tools import http_request @tool def store_memory_tool(information: str, user_id: str = "user") -> str: """Store important information in long-term memory.""" memory_message = [{"role": "user", "content": information}] # Create new memories using Mem0 and store them in Valkey m.add(memory_message, user_id=user_id) return f"Stored: {information}" @tool def search_memory_tool(query: str, user_id: str = "user") -> str: """Search stored memories for relevant information.""" # Search memories using Mem0 stored in Valkey results = m.search(query, user_id=user_id) if results['results']: return "\n".join([r['memory'] for r in results['results']]) return "No memories found" # Initialize Strands agent with memory tools agent = Agent(tools=[http_request, store_memory_tool, search_memory_tool])
步骤 4:测试启用内存的代理
启用内存后,代理会存储来自其交互的信息,并在后续请求中检索这些信息:
# First request - agent searches the web and stores results in memory formatted_messages = [ { "role": "user", "content": [{"text": "What is the URL for the project mem0 and its most important metrics?"}] } ] result = agent(formatted_messages) # Second request (same question) - agent retrieves from memory result = agent(formatted_messages)
在第二个请求中,代理会从内存中检索信息,而不是调用 Web 工具。在测试中,这使代币使用量从大约 70,000 减少到 6,300(减少了 12 倍),并将响应时间从 9.25 秒缩短到 2 秒(快了 3 倍多)。
它在引擎盖下是如何工作的
下表显示了 Mem0 在内部用来实现代理内存的 Valkey 命令。 ElastiCacheMem0 通过其 API 将这些命令抽象出来——具体的架构和密钥命名可能因 Mem0 版本和配置而异:
| 操作 | Valkey 命令 | 说明 |
|---|---|---|
| 创建向量索引 | FT.CREATE agent_memory SCHEMA embedding VECTOR HNSW 6 TYPE FLOAT32 DIM 1024 DISTANCE_METRIC COSINE |
为语义内存搜索创建向量索引 |
| 存储内存 | HSET mem:{id} memory "..." embedding [bytes] user_id "user_123" created_at "..." |
存储带有向量嵌入的内存 |
| 搜索回忆 | FT.SEARCH agent_memory "*=>[KNN 5 @embedding $query_vec]" PARAMS 2 query_vec [bytes] DIALECT 2 |
找到语义上最相似的记忆 |
| 设置到期时间 | EXPIRE mem:{id} 86400 |
为内存条目设置 TTL |