对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

多代理零售顾问的内存数据层

构建一个零售顾问,利用 MongoDB Atlas 实现跨合作代理的产品发现、客户背景和记忆管理。

使用案例: 人工智能,个性化

行业: 零售

产品和工具: MongoDB 搜索、向量搜索、 Voyage AI

合作伙伴: 人为因素,谷歌云

借助现代大型语言模型 (LLM) 和编排框架,构建 AI 代理看似简单,但生产环境中的代理系统却常常因复杂的数据管理而失败。团队通常需要将向量搜索、会话历史记录、内存和审计日志等多个独立数据库拼接在一起。这种碎片化的数据库架构会增加延迟、运营成本和维护复杂性。

在零售行业,顾客需要持续、个性化的互动,并有其独特的需求:

  • Carol searches for road-marathon shoes in a size 8 with maximum cushioning.

  • John searches for trail-running shoes in a size 11 with waterproof material and minimal cushioning.

为了支持这些场景,您的系统必须跨所有渠道保留短期会话历史记录、客户偏好和长期记忆。孤立的后端数据系统阻碍了零售商构建统一架构,而这种架构能够存储和共享支持生产就绪型 AI 助手所需的上下文信息。

MongoDB Atlas 将分散的数据存储替换为单一的统一数据库平台,该平台可用作您的代理数据平面。

部署 MongoDB Atlas 以原生方式处理智能体 AI 的所有五个数据需求:

  • 知识:将产品目录和矢量嵌入存储在一起。

  • 用户背景:维护灵活的客户档案。

  • 会话历史记录:保留跨回合的对话上下文。

  • 长期记忆:随着时间的推移学习和更新客户偏好。

  • 可观测性:记录工具执行路径和延迟。

多代理零售顾问 (MARA) 正是这种统一数据平台的体现。MARA 帮助客户找到最符合其特定需求的产品。

该解决方案融合了三大关键架构支柱:

  • Google Agent 开发工具包 (ADK) 协调多代理工作流程并管理代理交互。

  • Agent-to-Agent (A2A) protocol: Standardizes network-based communication and dynamic discovery across agent boundaries.

  • MongoDB Atlas 代理数据平面:将知识、用户上下文、会话历史、内存和可观测性整合到一个单一平台中。

MARA’s agents communicate over the A2A protocol and use MongoDB Atlas as a shared data layer for product discovery, customer memory, and traceability.

MongoDB Atlas holds MARA’s product catalog and powers product discovery. Each product document contains a search_text field with searchable catalog details. Atlas automatically generates vector embeddings for this text using voyage-4-large. When a customer submits a query, the application embeds the request with the lighter voyage-4-lite model. Because Voyage 4 models share an embedding space, queries embedded by the lightweight model can directly search representations generated by the larger index model. This asymmetric retrieval approach combines high-quality indexed data with lower query-embedding cost and latency, balancing accuracy and compute efficiency.

The Product agent searches products with vector search and structured catalog filters. When native reranking is enabled, it reorders candidates before returning matches. Otherwise, the agent returns the vector search results directly.

The Planner agent coordinates product recommendations. Through Agent-to-Agent (A2A) connections, it queries the Profile agent for customer context and the Product agent for matching catalog items. The Profile agent reads attributes from users and retrieves saved preferences from user_memory. When the interaction reveals a durable preference, the Profile agent can save it to user_memory. The Planner agent brings the specialists’ results together in its recommendation. ADK supports the agent workflow; MongoDB Atlas supplies the data beneath it.

内存标签描述了该数据的不同角色。

  • sessions记录对话信息。

  • agent_state记录工作流程状态,交互的短期记录。

  • user_memory:包含着超越一次对话而持续存在的已习得事实。

  • “共享内存”:指的是代理根据其角色访问的公共 Atlas 上下文层,而不是单独的数据库集合。

A session identifier tracks all agent calls. Specialist tools record their agent names, tool names, inputs, outputs, and execution times to tool_invocations under that identifier. Combined with the session and workflow records, these traces make the observable sequence of an interaction reconstructible and comparable across runs. They support investigations of recommendations or failures, but they do not capture every internal model decision or guarantee identical wording across repeated requests.

将用于向量搜索、会话状态和日志记录的独立数据库拼接在一起会增加成本。MongoDB Atlas 将所有代理数据整合到六个专用集合中。

Collection
用途

产品

存储带有自动矢量嵌入的目录项。

用户

保持静态的客户偏好、喜爱的品牌和尺码。

用户内存

保留在对话中不断积累的、持久的、已习得的偏好。

会话

按时间顺序保存每个客户对话的聊天互动历史记录。

代理状态

跟踪每个代理工作流程的活跃执行阶段。

工具调用

记录输入、输出和执行延迟,以便进行调试。

现代人工智能需要在一个灵活的模型中管理不断演变的上下文的整个生命周期,例如数据、内存、嵌入、检索和实时应用程序状态。

这种灵活的文档模型能够处理不断演变的模式,而无需重新设计表或协调破坏性的迁移。通过将客户资料、互动历史、偏好和权限等相关数据嵌入到文档中,可以将常用的上下文信息集中在一个文档中。这减少了连接操作,简化了查询,并提高了响应速度。

AI 数据天然呈文档形式。对话历史、代理记忆、工具调用、元数据和检索到的上下文自然地以嵌套的类 JSON 结构表示。MongoDB 文档与应用程序对象紧密对应,从而减少了转换和对象关系映射 (ORM) 的开销。

The users collection stores static customer attributes, such as shoe sizes and brand preferences. Because customer attributes are stored together in one document, the Profile agent retrieves a complete preference profile with a single, high-speed read query. This avoids the latency overhead of multi-table joins.

{ "name": "Carol Kim", "email": "carol@example.com", "preferences": { "brands": [ "Patagonia", "Darn Tough", "Arc'teryx" ], "price_range": { "min": 50, "max": 350 }, "categories": [ "rain_jackets", "base_layers", "accessories" ], "sizes": { "shoes": "8", "clothing": "S" } }, "profile": { "experience": "advanced", "activities": [ "mountaineering", "backpacking", "trail running" ], "location": "Vermont" }}

The user_memory collection stores dynamic, learned facts that agents extract during conversations.

{ "user_id": "user003", "facts": [ "Prefers polyester base layers because they dry faster than cotton or other materials" ], "updated_at": "2026-08-12T23:08:44.886000"}

The user.preferences.shoe_size field filters product catalog searches directly. The fact text field captures evolving insights, such as material preferences.

静态模式和非结构化动态记忆均以灵活的 JSON 格式存储。Profile 代理会查询这两个集合,以构建完整的客户状态。

The sessions collection holds conversational history per customer conversation. Each document retains a chronologically ordered array of message objects.

{ "session_id": "6415c1c1-bbf5-4986-818c-f390c489cab5", "user_id": "user003", "messages": [ { "role": "user", "content": "What base layers work best for mountaineering in cold, wet conditions?", "timestamp": "2026-08-12T23:03:22.654000" }, { "role": "assistant", "content": "Hi Carol! Great to hear from you.\n\nBased on your preferences and mountaineering needs, here are my top recommendations:\n\n## **1. Icebreaker Merino 260 Tech Long Sleeve Crewe** — $129\n…", "timestamp": "2026-08-12T23:03:57.044000" }, { "role": "user", "content": "i like base layers to be Polyester because they dry faster than cotton or other materials", "timestamp": "2026-08-12T23:08:33.023000" }, { "role": "assistant", "content": "Hi Carol! I've saved your preference for polyester base layers.\n\nBased on what you're looking for, here are my top recommendations:\n\n**1. Arc'teryx Phase SL Crew Long Sleeve - $89*...", "timestamp": "2026-08-12T23:09:09.013000" } ], "created_at": "2026-08-12T23:03:22.635000", "updated_at": "2026-08-12T23:09:09.013000"}

messages 数组捕获每回合的角色和文本内容。

关系型数据库需要一个辅助消息表,并且需要使用速度较慢的排序查询来重建聊天记录。将聊天记录以嵌套数组的形式存储在单个文档中,可以保证记录的顺序。这使得 Planner 代理能够通过一次读取即可获取完整的按时间顺序排列的会话历史记录。

The products collection integrates transactional fields with vector embedding arrays.

{ "name": "Patagonia Torrentshell 3L Jacket", "brand": "Patagonia", "category": "rain_jackets", "description": "The Torrentshell 3L is Patagonia's workhorse waterproof jacket built with H2No Performance Standard 3-layer shell. Fully seam-sealed and Fair Trade Certified, it packs into its own chest pocket. Ideal for unpredictable weather on trail or in town.", "price": 149, "size_options": ["XS",...], "attributes": { "waterproof": true, "eco_friendly": true, "insulated": false, "gender": "unisex" }, "colors": ["black"...], "rating": 4.7, "reviews_count": 2841, "search_text": "Patagonia Torrentshell 3L Jacket. Patagonia. Category: rain_jackets. The Torrentshell 3L is Patagonia's workhorse waterproof jacket built with H2No Performance Standard 3-layer shell. Fully seam-sealed and Fair Trade Certified, it packs into its own chest pocket. Ideal for unpredictable weather on trail or in town."}

The price and productId fields handle standard retail transaction lookups. The search_text field combines the product name, brand, category, and raw description into a single string. The Atlas Vector Search index then targets this concatenated string using an automatic embedding configuration.

You do not need to store or manage vector arrays inside your documents. The Auto-Embedding feature automatically vectorizes the search_text field in the background.

Because Voyage AI models share the same embedding space, you can pre-embed the catalog with voyage-4-large. You can then run user queries with low-latency voyage-4-lite. This asymmetric approach speeds up search and reduces execution costs.

The tool_invocations collection records every tool call and execution detail.

[ { "session_id": "6415c1c1-bbf5-4986-818c-f390c489cab5", "agent_name": "ProfileAgent", "tool_name": "get_user_profile", "input": { "user_id": "user003" }, "output": { "name": "Carol Kim", "email": "carol@example.com", "preferences": { "brands": [ "Patagonia", "Darn Tough", "Arc'teryx" ], "price_range": { "min": 50, "max": 350 }, "categories": [ "rain_jackets", "base_layers", "accessories" ], "sizes": { "shoes": "8", "clothing": "S" } }, "profile": { "experience": "advanced", "activities": [ "mountaineering", "backpacking", "trail running" ], "location": "Vermont" } }, "latency_ms": 15, "timestamp": "2026-08-12T23:08:36.663000" }, { "session_id": "6415c1c1-bbf5-4986-818c-f390c489cab5", "agent_name": "ProfileAgent", "tool_name": "get_user_memory", "input": { "user_id": "user003" }, "output": { "user_id": "user003", "facts": ["prefers polyester material over cotton"], "note": "No memory stored for this user yet" }, "latency_ms": 15, "timestamp": "2026-08-12T23:08:36.698000" }, { "session_id": "6415c1c1-bbf5-4986-818c-f390c489cab5", "agent_name": "ProfileAgent", "tool_name": "update_user_memory", "input": { "user_id": "user003", "new_facts": [ "Prefers polyester base layers because they dry faster than cotton or other materials" ] }, "output": { "status": "ok", "user_id": "user003", "facts_added": 1 }, "latency_ms": 21, "timestamp": "2026-08-12T23:08:44.907000" }, { "session_id": "6415c1c1-bbf5-4986-818c-f390c489cab5", "agent_name": "ProductAgent", "tool_name": "search_products", "input": { "query": "Base layers Patagonia Arc'teryx mountaineering backpacking trail running advanced performance", "max_price": 350, "size": "S", "waterproof": null, "eco_friendly": null, "category": "base_layers" }, "output": [ { "name": "Patagonia Capilene Cool Daily Long Sleeve Shirt", "brand": "Patagonia", "category": "base_layers", "description": "The Capilene Cool Daily is made from recycled polyester …", "price": 65.0, "size_options": [ "XS", "S"], "attributes": { "waterproof": false, "eco_friendly": true, "insulated": false, "gender": "unisex" }, "colors": [ "black", "light bog green"], "rating": 4.5, "reviews_count": 2543, "vectorScore": 0.7558120489120483 }, … ], "latency_ms": 193, "timestamp": "2026-08-12T23:08:45.707000" }]

使用MARA 存储库探索解决方案。设置指南提供以下阶段的详细配置和命令:

1

Use an Atlas M10-or-higher cluster for automated embeddings. Configure the Atlas connection and LLM endpoint in backend/.env by following the setup guide. Native reranking is optional and requires the MongoDB version and preview setting specified in the guide.

2

Run the repository’s seed procedure to load sample users and 135 outdoor products, and to create the vector and text search indexes. Atlas embeds the product search_text field in the background. Wait for indexing to complete before evaluating semantic product retrieval.

3

Start the Docker Compose stack to run the frontend, REST orchestrator, and separate Planner, Profile, and Product agent services. Each agent service publishes an A2A AgentCard for discovery and communication.

4

Request a product recommendation in the chat interface, then inspect the Agents, Trace, and MongoDB views. State a durable preference and start another conversation as the same sample user to check that the Profile agent retrieves the saved fact from user_memory. Inspect the session-linked tool records to see which specialists ran. This verifies learned-preference persistence, not automatic replay of the complete chat history to the Planner.

  • 统一代理数据:在 MongoDB Atlas 中保留产品目录、客户资料、学习到的偏好、会话记录和工具跟踪。

  • Balance retrieval quality and query cost: Configure Atlas to embed catalog text with voyage-4-large and use the compatible voyage-4-lite model for live customer queries. Evaluate retrieval quality for the application rather than assuming identical results.

  • Separate agent responsibilities: Use the Planner agent to coordinate the Profile and Product agents over A2A, while each specialist accesses the Atlas data relevant to its role.

  • Persist learned preferences: Save durable customer facts in user_memory so the Profile agent can retrieve them in later conversations. Stored session messages are not automatically replayed to the Planner agent.

  • 追踪可观察的代理活动:通过会话 ID 连接工作流状态和工具记录,以调查建议和失败,而无需声称捕获每个内部模型决策。

  • Alberto Camarena, MongoDB

  • Angie Guemes, MongoDB

  • Rodrigo Leal, MongoDB

  • 罗纳恩 康伦,MongoDB

  • Daniel Jamir, MongoDB

文档