MongoDB Atlas 促进了跨行业 AI 代理的快速开发。
产品: MongoDB Atlas、MongoDB Atlas 数据库、MongoDB Atlas Vector Search
合作伙伴: AWS Bedrock、Cohere、LangChain
解决方案概述
世界各地的公司都在将AI代理纳入其业务工作流程。AI代理最常见的用途是协助研究分析或编写代码。LangChain 最近的调查 对多个行业的超过 1、000 专业人士进行的调查显示,超过 51% 已经在生产环境中部署了代理。
AI代理在扩展到基本任务之外时面临三个关键挑战:
复杂的数据集成和处理: AI代理依赖于不同的数据源,例如结构化日志、非结构化文本和传感器流。这使得数据统一难以进行实时决策。将所有相关数据存储在一个数据库中可加快开发速度。
高并发和延迟:代理必须处理大量请求并快速响应。这可能会使缺乏高吞吐量的数据库不堪重负。虽然 LLM 推断增加了延迟,但随着代理的扩展,数据库性能仍然很重要。生产代理并行运行,调用多个工具,并根据当前数据做出决策。慢速数据库会产生瓶颈,从而增加响应时间并降低实时功能。
数据管理和安全: AI代理必须安全地存储和访问权限数据,同时保持合规。MongoDB Atlas提供内置安全控制,例如客户端字段级加密、可查询加密和审核功能。这些功能可确保代理仅访问权限授权数据并保持可追溯性。
该解决方案提供了一个代理框架,为加速AI驱动的工作流程的开发提供了灵活的基础。该框架不是提供一设立固定的工具或功能,而是为构建针对特定用例的代理提供了一个点。
使用此解决方案,可使用MongoDB Atlas和 LangGraph 创建具有线性工作流程的代理。对于复杂的使用案例,您可以通过添加新工具、节点、条件或工作流程来扩展框架。
图 1 显示了代理的主要组件,其中包括:
从用户或自动触发接收任务
使用 LLM 生成回复或遵循工作流程
使用各种工具和模型在MongoDB Atlas中存储和检索数据
图 1. AI 代理的基本组成部分
参考架构
该代理框架使用 LangGraph 执行多步骤诊断工作流程,并生成可行的见解。该框架执行以下操作:
从逗号分隔值(CSV)文件或MongoDB Atlas读取时间序列数据
生成文本嵌入
执行向量搜索以识别过去的类似查询
保留会话和运行数据
生成诊断建议
MongoDB Atlas存储代理配置文件、历史建议、时间序列数据和会话日志。这确保了完全的可追溯性,并实现了对过去见解的高效查询和可重用性。
图2。Agentic AI 参考架构
MongoDB为 Agentic AI带来的优势
MongoDB通过其灵活性、性能和可扩展性支持代理AI 。文档模型原生存储结构化、半结构化和非结构化数据,并处理各种数据集。MongoDB使AI代理能够对新信息做出反应,实时维护内部状态并持续学习。
灵活数据模型: MongoDB以统一格式存储时间序列日志、代理配置文件和推荐输出等数据。其灵活的模式无需在数据需求发生变化时重新设计数据库。AI代理可以使用嵌套文档存储省/市/自治区,以适应不断变化的特征和上下文。MongoDB支持版本控制和跟踪代理的演变,并具备暂停和恢复代理上下文的能力。
向量搜索: MongoDB Atlas支持原生向量搜索,用于向量嵌入的相似性搜索。此功能将当前查询与历史数据进行匹配,提高诊断准确性,并提供相关建议。向量搜索支持模式和上下文检索,从而减少 LLM 幻觉。MongoDB处理AI代理工作流程的语义匹配、上下文搜索和多维数据分析。
可扩展性和性能:借助MongoDB,AI代理可以横向扩展,以处理大量实时数据、分配存储和计算负载,并通过MongoDB副本集保持高可用性。
时间序列集合: MongoDB时间序列集合可高效地摄取大量数据。这些集合启用AI代理能够追踪一段时间内的顺序交互、学习模式和省/市/自治区变化。此功能可维护上下文并实现自适应决策。时间序列优化包括自动数据压缩、提高的存储效率以及基于时间的快速查询。借助MongoDB,AI代理可以维护当前和历史记录,而不会影响性能或数据完整性。
集成: MongoDB通过类似JSON的文档、动态模式和索引功能与LangGraph等代理框架集成。这种集成使AI代理能够维护数据和内存结构,追踪多步骤推理过程,并实现跨会话持续的状态管理。
构建解决方案
使用以下步骤使用MongoDB Atlas和LangGraph设立代理框架。有关步骤的更详细说明,请参阅GitHub 存储库。
包含对您的用例有意义的 CSV 数据
数据必须与您的使用案例相关,并遵循以下准则:
在逗号分隔值(CSV)文件中,添加包含列名称的标题行。
将第一列命名为
timestamp,并采用YYYY-MM-DDTHH:MM:SSZ格式设置时间戳。示例,2025-02-19T13:00:00Z。在剩余列中填写与您的使用案例相关的数据。
对于框架测试,请保持数据量尽可能小。
示例数据:
timestamp,gdp,interest_rate,unemployment_rate,vix 2025-02-19T13:00:00Z,2.5,1.75,3.8,15 2025-02-19T13:05:00Z,2.7,1.80,3.7,18 2025-02-19T13:10:00Z,2.6,1.85,3.9,22 2025-02-19T13:15:00Z,2.4,1.70,4.0,10 2025-02-19T13:20:00Z,2.3,1.65,4.1,20 在同一文件夹中,添加一个查询文件。此文件包含用于展示向量搜索功能的查询,作为代理工作流程的一部分。在您的文件中,请遵循以下准则:
在逗号分隔值(CSV)文件中,添加包含列名称的标题行。
将第一列命名为
query并用查询填充它。将第二列命名为
recommendation并用预期推荐填充它。对于框架测试,请保持数据量尽可能小。
示例数据:
query,recommendation GDP growth slowing,Consider increasing bond assets to mitigate risks from potential economic slowdown. GDP showing strong growth,Increase equity assets to capitalize on favorable investment conditions. Interest rates rising,Shift focus to bond assets as higher rates may impact borrowing-sensitive sectors. Interest rates falling,Increase real estate assets to take advantage of lower borrowing costs. Unemployment rate increasing,Reduce equity assets to account for potential economic weakness and reduced consumer spending. Unemployment rate decreasing,Increase equity assets to benefit from improved economic conditions and corporate profits. VIX above 20,Reduce equity assets to manage risks associated with high market volatility. VIX below 12,Increase equity assets to capitalize on low market volatility and investor confidence. VIX within normal range (12-20),Maintain current asset allocation as market conditions are stable. Combination of rising interest rates and high VIX,Focus on bond assets to hedge against market volatility and borrowing cost impacts.
通过JSON文件配置代理工作流程
Go配置文件夹并创建或更新JSON config.json文件。该文件必须包含以下结构:
{ "CSV_DATA": "data/csv/<YOUR_FILE_NAME>.csv", "MDB_DATABASE_NAME": "<YOUR_MONGODB_DATABASE_NAME>", "MDB_TIMESERIES_COLLECTION": "<YOUR_MONGODB_TIMESERIES_COLLECTION_NAME>", "DEFAULT_TIMESERIES_DATA": [ { "timestamp": "<DEFAULT_TIMESTAMP_IN_YYYY-MM-DDTHH:MM:SSZ>" // Your default data here, check config_example.json for better understanding } ], "CRITICAL_CONDITIONS": { // Below is an example of a critical condition for GDP growth "gdp": {"threshold": 2.5, "condition": "<", "message": "GDP growth slowing: {value}%"} // Other critical conditions for your use case here, check config_example.json for better understanding }, "MDB_TIMESERIES_TIMEFIELD": "<YOUR_TIMESTAMP_FIELD_NAME>", "MDB_TIMESERIES_GRANULARITY": "<YOUR_TIMESERIES_GRANULARITY>", "MDB_EMBEDDINGS_COLLECTION": "queries", // Using "queries" collection name for storing queries "MDB_EMBEDDINGS_COLLECTION_VS_FIELD": "query_embedding", // Using "query_embedding" field for storing embeddings "MDB_VS_INDEX": "<YOUR_MONGODB_DATABASE_NAME>_queries_vs_idx", // Replace <YOUR_MONGODB_DATABASE_NAME> with your MongoDB database name "MDB_HISTORICAL_RECOMMENDATIONS_COLLECTION": "historical_recommendations", // Using "historical_recommendations" collection name for storing recommendations "SIMILAR_QUERIES": [ // Below is an example of default similar queries for GDP growth { "query": "GDP growth slowing", "recommendation": "Consider increasing bond assets to mitigate risks from potential economic slowdown." } // Other similar queries for your use case here, check config_example.json for better understanding // This ones are going to be used for the vector search tool in case something is not found in the queries collection ], "MDB_CHAT_HISTORY_COLLECTION": "chat_history", // Using "chat_history" collection name for storing chat history "MDB_CHECKPOINTER_COLLECTION": "checkpoints", // Using "checkpoints" collection name for storing checkpoints "MDB_LOGS_COLLECTION": "logs", // Using "logs" collection name for storing logs "MDB_AGENT_PROFILES_COLLECTION": "agent_profiles", // Using "agent_profiles" collection name for storing agent profiles "MDB_AGENT_SESSIONS_COLLECTION": "agent_sessions", // Using "agent_sessions" collection name for storing agent sessions "AGENT_PROFILE_CHOSEN_ID": "<YOUR_AGENT_PROFILE_ID>", // Replace <YOUR_AGENT_PROFILE_ID> with the agent profile ID you want to use, check config_example.json for better understanding // Below is an example default agent profile for Portfolio Advisor "DEFAULT_AGENT_PROFILE": { "agent_id": "DEFAULT", "profile": "Default Agent Profile", "role": "Expert Advisor", "kind_of_data": "Specific Data", "motive": "diagnose the query and provide recommendations", "instructions": "Follow procedures meticulously.", "rules": "Document all steps.", "goals": "Provide actionable recommendations." }, "EMBEDDINGS_MODEL_NAME": "Cohere Embed English V3 Model (within AWS Bedrock)", // Describing the embeddings model used for creating the chain of thought "EMBEDDINGS_MODEL_ID": "cohere.embed-english-v3", // Model ID for the embeddings model "CHATCOMPLETIONS_MODEL_NAME": "Anthropic Claude 3 Haiku (within AWS Bedrock)", // Describing the chat completions model used for generating responses "CHATCOMPLETIONS_MODEL_ID": "anthropic.claude-3-haiku-20240307-v1:0", // Model ID for the chat completions model // Below is a sample agent workflow graph that uses the tools defined in the agent_tools.py file // PLEASE BE CAREFUL WHEN MODIFYING THIS GRAPH, CONSIDER THAT THE TOOLS DEFINED IN THE AGENT TOOLS FILE ARE USED HERE AS WELL AS THE IMPORTS "AGENT_WORKFLOW_GRAPH": { "nodes": [ {"id": "reasoning_node", "tool": "agent_tools.generate_chain_of_thought_tool"}, {"id": "data_from_csv", "tool": "agent_tools.get_data_from_csv_tool"}, {"id": "process_data", "tool": "agent_tools.process_data_tool"}, {"id": "embedding_node", "tool": "agent_tools.get_query_embedding_tool"}, {"id": "vector_search", "tool": "agent_tools.vector_search_tool"}, {"id": "process_vector_search", "tool": "agent_tools.process_vector_search_tool"}, {"id": "persistence_node", "tool": "agent_tools.persist_data_tool"}, {"id": "recommendation_node", "tool": "agent_tools.get_llm_recommendation_tool"} ], "edges": [ {"from": "reasoning_node", "to": "data_from_csv"}, {"from": "data_from_csv", "to": "process_data"}, {"from": "process_data", "to": "embedding_node"}, {"from": "embedding_node", "to": "vector_search"}, {"from": "vector_search", "to": "process_vector_search"}, {"from": "process_vector_search", "to": "persistence_node"}, {"from": "persistence_node", "to": "recommendation_node"}, {"from": "recommendation_node", "to": "END"} ], "entry_point": "reasoning_node" } }
更新配置文件后:
配置环境变量。
创建向量嵌入。
创建向量搜索索引。
图 3. 逻辑架构
关键要点
Agentic AI引入了以不同于传统应用程序的方式处理数据的软件。要在业务应用程序中利用代理,软件交付团队必须了解:
代理如何接收预定工作流程的指令
代理如何访问权限API 和数据库并与之交互
代理如何持久化和管理省/市/自治区
这些活动构成了软件交付团队部署代理的基线设置,而无论使用案例、工作流程或行业背景如何。
框架抽象了常见任务和组件,以加快开发和部署,同时降低维护复杂性。该能动框架可帮助软件交付团队使用通用的可配置组件构建和维护能动解决方案,同时教团队开发可重复的模式和组件,以实现长期可扩展性和可维护性。
作者
Julian Boronat,MongoDB
Peyman Parsi, MongoDB
Jeff Needham,MongoDB
Luca Napoli,MongoDB
Humza Akthar, MongoDB