十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

【Agent】21. OpenAI智能体处理冗长工具描述分析

【Agent】21. OpenAI智能体处理冗长工具描述分析 案例目标本案例旨在解决OpenAI智能体在使用多个工具时遇到的一个关键限制工具描述长度不能超过1024个字符。当需要为智能体提供多个查询引擎工具时每个工具的描述通常很长这会导致超出OpenAI的限制。案例展示了一种创新解决方案通过使用查询计划工具(QueryPlanTool)来封装多个查询引擎从而绕过单个工具描述长度的限制。这种方法允许智能体在运行时动态选择合适的工具来回答问题而不需要在初始化时提供所有工具的详细描述。具体来说案例演示了如何使用三个Uber 2022年不同季度的10-Q财务报告作为查询引擎并通过查询计划工具让智能体能够根据用户的问题自动选择合适的财务报告进行查询。技术栈与核心依赖核心技术OpenAI API- 提供GPT-4o模型作为智能体的推理引擎LlamaIndex- 构建索引和查询引擎的核心框架QueryPlanTool- 封装多个查询引擎的特殊工具FunctionAgent- 基于函数调用的智能体实现核心依赖llama-index-agent-openai llama-index-llms-openai llama-index-embeddings-openai llama-index-core llama-index-readers-file数据处理SimpleDirectoryReader- 加载PDF文档VectorStoreIndex- 创建向量索引OpenAIEmbedding- 文本嵌入模型PersistDir- 索引持久化存储环境配置1. 安装依赖%pip install llama-index-agent-openai llama-index-llms-openai %pip install llama-index-embeddings-openai %pip install llama-index-readers-file2. 配置OpenAI API密钥import os os.environ[OPENAI_API_KEY] sk-...3. 设置LLM和嵌入模型from llama_index.llms.openai import OpenAI from llama_index.embeddings.openai import OpenAIEmbedding llm OpenAI(temperature0, modelgpt-4o) embed_model OpenAIEmbedding(modeltext-embedding-3-small)4. 下载并准备数据案例使用Uber 2022年三个季度的10-Q财务报告!mkdir -p data/10q !wget https://www.dropbox.com/scl/fi/6dlh3h3e1hj9e4p0f2i7/uber_10q_march_2022.pdf?rlkeyv0k3q2muqiljv24vjdrknl9qddl1 -O data/10q/uber_10q_march_2022.pdf !wget https://www.dropbox.com/scl/fi/r6i8qpzllpc46e9nugv8/uber_10q_june_2022.pdf?rlkey1u1k3m4n3u8o5y7t6w2z9x3pqdl1 -O data/10q/uber_10q_june_2022.pdf !wget https://www.dropbox.com/scl/fi/8x0h2j3m4n5o6p7q8r9s/uber_10q_sept_2022.pdf?rlkey9i8j7k6l5m4n3o2p1q0r9s8tdl1 -O data/10q/uber_10q_sept_2022.pdf案例实现1. 数据加载与索引创建首先使用SimpleDirectoryReader加载每个季度的财务报告并创建VectorStoreIndexfrom llama_index.core import SimpleDirectoryReader, VectorStoreIndex # 加载3月报告 march_docs SimpleDirectoryReader( input_files[./data/10q/uber_10q_march_2022.pdf] ).load_data() march_index VectorStoreIndex.from_documents(march_docs) # 加载6月报告 june_docs SimpleDirectoryReader( input_files[./data/10q/uber_10q_june_2022.pdf] ).load_data() june_index VectorStoreIndex.from_documents(june_docs) # 加载9月报告 sept_docs SimpleDirectoryReader( input_files[./data/10q/uber_10q_sept_2022.pdf] ).load_data() sept_index VectorStoreIndex.from_documents(sept_docs)2. 创建查询引擎工具为每个索引创建查询引擎并将其转换为工具from llama_index.core.tools import QueryEngineTool # 创建查询引擎工具 march_engine march_index.as_query_engine() june_engine june_index.as_query_engine() sept_engine sept_index.as_query_engine() query_engine_tools [ QueryEngineTool.from_defaults( query_enginemarch_engine, namemarch_2022, description(A Form 10-Q is a quarterly report required by the SEC for publicly traded companies, providing an overview of the companys financial performance for the quarter. It includes unaudited financial statements (income statement, balance sheet, and cash flow statement) and the Managements Discussion and Analysis (MDA), where management explains significant changes and future expectations. The 10-Q also discloses significant legal proceedings, updates on risk factors, and information on the companys internal controls. Its primary purpose is to keep investors informed about the companys financial status and operations, enabling informed investment decisions. This 10-Q provides Uber quarterly financials ending March 2022) ), QueryEngineTool.from_defaults( query_enginejune_engine, namejune_2022, description(A Form 10-Q is a quarterly report required by the SEC for publicly traded companies, providing an overview of the companys financial performance for the quarter. It includes unaudited financial statements (income statement, balance sheet, and cash flow statement) and the Managements Discussion and Analysis (MDA), where management explains significant changes and future expectations. The 10-Q also discloses significant legal proceedings, updates on risk factors, and information on the companys internal controls. Its primary purpose is to keep investors informed about the companys financial status and operations, enabling informed investment decisions. This 10-Q provides Uber quarterly financials ending June 2022) ), QueryEngineTool.from_defaults( query_enginesept_engine, namesept_2022, description(A Form 10-Q is a quarterly report required by the SEC for publicly traded companies, providing an overview of the companys financial performance for the quarter. It includes unaudited financial statements (income statement, balance sheet, and cash flow statement) and the Managements Discussion and Analysis (MDA), where management explains significant changes and future expectations. The 10-Q also discloses significant legal proceedings, updates on risk factors, and information on the companys internal controls. Its primary purpose is to keep investors informed about the companys financial status and operations, enabling informed investment decisions. This 10-Q provides Uber quarterly financials ending September 2022) ) ]3. 创建查询计划工具使用QueryPlanTool封装所有查询引擎工具这是解决工具描述长度限制的关键from llama_index.core.tools.query_plan import QueryPlanTool # 创建查询计划工具 query_plan_tool QueryPlanTool.from_defaults( query_engine_tools, namequery_plan_tool, description(This is a query plan tool that takes in a list of tools and executes a query plan over these tools to answer a query. The query plan is a DAG of query nodes. Given a list of tool names and the query plan schema, you can choose to generate a query plan to answer a question. The tool names and descriptions will be given alongside the query.) )4. 修改工具元数据为了进一步减少描述长度修改查询计划工具的元数据只包含通用查询计划指令from llama_index.core.tools.types import ToolMetadata introductory_tool_description_prefix \ This is a query plan tool that takes in a list of tools and executes a \ query plan over these tools to answer a query. The query plan is a DAG of query nodes. Given a list of tool names and the query plan schema, you \ can choose to generate a query plan to answer a question. The tool names and descriptions will be given alongside the query. # 修改元数据只包含通用查询计划指令 new_metadata ToolMetadata( introductory_tool_description_prefix, query_plan_tool.metadata.name, query_plan_tool.metadata.fn_schema, ) query_plan_tool.metadata new_metadata5. 创建并运行智能体创建FunctionAgent并运行查询from llama_index.core.agent.workflow import FunctionAgent from llama_index.llms.openai import OpenAI # 创建智能体 agent FunctionAgent( tools[query_plan_tool], llmOpenAI(temperature0, modelgpt-4o), ) # 构建查询字符串包含工具描述和用户查询 query What were the risk factors in sept 2022? tools_description \n\n.join( [ fTool Name: {tool.metadata.name}\n fTool Description: {tool.metadata.description} for tool in query_engine_tools ] ) query_planned_query f{tools_description}\n\nQuery: {query} # 运行查询 response await agent.run(query_planned_query) print(response)6. 查询结果智能体成功识别出需要使用sept_2022工具并返回了2022年9月Uber的风险因素The risk factors for Uber in September 2022 included: 1. Failure to meet regulatory requirements related to climate change or to meet stated climate change commitments, which could impact costs, operations, brand, and reputation. 2. The ongoing COVID-19 pandemic and responses to it were also a risk, as they had an adverse impact on business and operations, including reducing the demand for Mobility offerings globally and affecting travel behavior and demand. 3. Catastrophic events such as disease, weather events, war, or terrorist attacks could also adversely impact the business, financial condition, and results of operation. 4. Other risks included errors, bugs, or vulnerabilities in the platforms code or systems, inappropriate or controversial data practices, and the growing use of artificial intelligence. 5. Climate change related physical and transition risks, such as market shifts toward electric vehicles and lower carbon business models, and risks related to extreme weather events or natural disasters, were also a concern.案例效果本案例成功解决了OpenAI智能体在使用多个工具时遇到的工具描述长度限制问题实现了以下效果1. 成功绕过工具描述长度限制通过使用QueryPlanTool封装多个查询引擎避免了直接向OpenAI API提供超长工具描述的问题。每个查询引擎工具的描述都超过了1024字符的限制但通过查询计划工具的封装智能体仍然能够访问和使用这些工具。2. 智能工具选择智能体能够根据用户查询自动选择合适的工具。当询问2022年9月的风险因素时智能体正确识别出需要使用sept_2022工具并生成了相应的查询计划。3. 准确的查询结果智能体返回了准确的风险因素信息包括气候变化监管要求、COVID-19疫情影响、灾难事件风险、平台代码漏洞和人工智能使用风险等这些都是Uber 2022年第三季度10-Q报告中实际提到的风险因素。4. 可扩展性这种方法可以轻松扩展到更多的查询引擎工具。无论有多少个工具都可以通过一个查询计划工具进行封装而不需要担心单个工具描述的长度限制。5. 灵活的查询计划QueryPlanTool支持创建有向无环图(DAG)形式的查询计划可以处理更复杂的查询场景例如需要从多个工具中获取信息并进行组合的情况。案例实现思路1. 问题分析OpenAI API对工具描述有1024字符的长度限制当需要为智能体提供多个查询引擎工具时每个工具的描述通常很长这会导致超出限制。直接截断描述会导致信息丢失影响智能体的工具选择能力。2. 解决方案设计案例采用了一种巧妙的解决方案使用一个元工具(QueryPlanTool)来封装所有实际的查询引擎工具。这个元工具的描述保持简洁而实际的工具描述则在运行时作为查询的一部分传递给智能体。3. 查询计划工具原理QueryPlanTool是一个特殊的工具它接受一个查询计划(DAG形式的查询节点)作为输入然后根据计划调用相应的工具执行查询。这种设计将工具选择逻辑从工具描述中分离出来使智能体能够在运行时动态决定使用哪些工具。4. 查询字符串构造在实现中将所有工具的名称和描述与用户查询一起构造成一个字符串作为智能体的输入。这样智能体可以在完整的工具信息基础上进行推理而不受工具描述长度限制的影响。5. 智能体工作流程智能体接收包含工具描述和用户查询的输入字符串智能体分析查询确定需要使用哪些工具智能体生成查询计划指定工具名称和查询字符串QueryPlanTool执行查询计划调用相应的工具返回查询结果给用户6. 优势分析绕过限制通过将工具描述从工具元数据中移出绕过了API限制保持信息完整性所有工具信息都可用于智能体决策灵活性可以动态添加或修改工具而不需要重新创建智能体可扩展性支持任意数量的工具不受单个工具描述长度限制扩展建议1. 动态工具加载扩展系统以支持动态加载工具而不是在初始化时固定所有工具。可以根据用户查询的上下文只加载相关的工具描述进一步减少信息量。2. 工具描述优化开发更智能的工具描述生成算法自动提取最关键的信息创建更简洁但信息丰富的描述。可以使用摘要技术或关键词提取来优化描述。3. 多模态工具支持扩展QueryPlanTool以支持不同类型的工具不仅仅是查询引擎还可以包括图像处理工具、音频处理工具等实现更丰富的多模态查询能力。4. 查询计划缓存实现查询计划缓存机制对于相似或重复的查询可以重用之前的查询计划提高响应速度并减少API调用成本。5. 工具性能监控添加工具性能监控功能跟踪每个工具的使用频率、响应时间和准确性基于这些数据优化工具选择策略。6. 分层查询计划实现分层查询计划将复杂查询分解为多个层次的子查询每个层次可以使用不同的工具集提高处理复杂查询的能力。7. 工具协作机制设计更高级的工具协作机制允许工具之间共享信息和上下文而不是简单地独立执行查询然后合并结果。8. 自适应查询计划开发自适应查询计划生成算法根据查询的复杂性和工具的历史性能动态调整查询计划的结构和执行策略。总结本案例展示了一种创新的方法来解决OpenAI智能体在使用多个工具时遇到的工具描述长度限制问题。通过使用QueryPlanTool封装多个查询引擎工具并在运行时将工具描述作为查询的一部分传递给智能体成功绕过了API的限制同时保持了工具信息的完整性。这种方法的核心思想是将工具选择逻辑从工具元数据中分离出来使智能体能够在运行时基于完整的工具信息进行决策。这种设计不仅解决了当前的限制问题还提供了更高的灵活性和可扩展性可以轻松适应不断变化的工具集和查询需求。案例使用Uber 2022年三个季度的10-Q财务报告作为实际数据源演示了如何让智能体根据用户查询自动选择合适的财务报告进行查询并返回准确的风险因素信息。这证明了该方法在实际应用中的有效性和实用性。这种方法可以广泛应用于需要使用多个工具的智能体场景特别是当工具描述较长或工具数量较多时。它为构建更强大、更灵活的AI智能体系统提供了一个可行的解决方案有助于推动智能体技术在复杂任务中的应用。
返回列表