尧图精选

【Agent】【tools】10.带有工件编辑器工具的订单完成代理分析

🕒 发布时间:2026/10/1 10:08:30 📁 来源:尧图网络
1. 案例目标本案例展示了如何构建一个聊天助手专门用于填写自定义表单。具体来说案例实现了一个订单接收助手需要从终端用户那里获取一些预设的信息片段如配送地址和订单内容然后才能继续处理订单。案例使用LlamaIndex的新组件ArtifactEditorToolSpec和ArtifactMemoryBlock来构建一个披萨订购系统能够收集用户的披萨订单和配送地址信息。2. 技术栈与核心依赖核心库llama-index - 核心框架提供代理和工作流功能llama-index-tools-artifact-editor - 工件编辑器工具规范pydantic - 数据模型定义和验证AI组件OpenAI GPT-4.1 - 语言模型处理用户对话FunctionAgent - 函数调用代理AgentWorkflow - 代理工作流3. 环境配置安装步骤pip install llama-index llama-index-tools-artifact-editor环境变量设置if OPENAI_API_KEY not in os.environ: os.environ[OPENAI_API_KEY] getpass(OpenAI API Key: )注意使用本案例前需要设置OpenAI API密钥以便访问GPT-4.1模型。4. 案例实现4.1 导入必要模块import osimport jsonfrom getpass import getpassfrom pydantic import BaseModel, Fieldfrom llama_index.llms.openai import OpenAIfrom llama_index.core.memory import Memoryfrom llama_index.core.agent.workflow import (FunctionAgent,AgentWorkflow,ToolCallResult,AgentStream,)from llama_index.tools.artifact_editor import (ArtifactEditorToolSpec,ArtifactMemoryBlock,)4.2 定义数据模型class Pizza(BaseModel):name: str Field(descriptionThe name of the pizza)remove: list[str] | None Field(descriptionIf exists, the ingredients the customer requests to remove,defaultNone,)add: list[str] | None Field(descriptionIf exists, the ingredients the customer requests to be added,defaultNone,)class Address(BaseModel):street_address: str Field(descriptionThe street address of the customer)city: str Field(descriptionThe city of the customer)state: str Field(descriptionThe state of the customer)zip_code: str Field(descriptionThe zip code of the customer)class Order(BaseModel):pizzas: list[Pizza] | None Field(descriptionThe pizzas ordered by the customer,defaultNone)address: Address | None Field(descriptionThe full address of the customer,defaultNone)4.3 创建工具规范和代理tool_spec ArtifactEditorToolSpec(Order)tools tool_spec.to_tool_list()# Initialize the memorymemory Memory.from_defaults(session_idorder_editor,memory_blocks[ArtifactMemoryBlock(artifact_spectool_spec)],token_limit60000,chat_history_token_ratio0.7,)llm OpenAI(modelgpt-4.1)agent AgentWorkflow(agents[FunctionAgent(llmllm,toolstools,system_promptYou are a worker at a Pizzeria. Your job is to talk to users and gather order information. At every step, you should check the order completeness before responding to the user, and ask for any possibly missing information.,)],)4.4 实现聊天循环async def chat():while True:user_msg input(User: ).strip()if user_msg.lower() in [exit, quit]:print(\n------ORDER COMPLETION-------\n)print(fThe Order was placed with the following Order schema:\n: {json.dumps(tool_spec.get_current_artifact(), indent4)})breakhandler agent.run(user_msg, memorymemory)async for ev in handler.stream_events():if isinstance(ev, AgentStream):print(ev.delta, end, flushTrue)elif isinstance(ev, ToolCallResult):print(f\n\nCalling tool: {ev.tool_name} with kwargs: {ev.tool_kwargs})print(\n\nCurrent artifact: , tool_spec.get_current_artifact())5. 案例效果案例运行后代理能够与用户进行多轮对话收集披萨订单信息Hello! Welcome to our pizzeria. Would you like to place an order? If so, could you please tell me what kind of pizza youd like?Id like a pepperoni pizza with olives and a margherita pizza.Calling tool: create_artifact with kwargs: {pizzas: [{name: pepperoni, add: [olives]}, {name: margherita}]}Youve ordered: - 1 Pepperoni pizza with added olives - 1 Margherita pizza To complete your order, could you please provide your delivery address (street address, city, state, and zip code)?1 Sesame Street, Amsterdam, North-Holand, 1111ABCalling tool: apply_patch with kwargs: {patch: {operations: [{op: replace, path: /address, value: {street_address: 1 Sesame Street, city: Amsterdam, state: North-Holand, zip_code: 1111AB}}]}}Thank you! Your order is now complete: - 1 Pepperoni pizza with added olives - 1 Margherita pizza Delivery address: 1 Sesame Street, Amsterdam, North-Holand, 1111AB Would you like to add anything else to your order, or should I proceed with placing it?Thats all, please place the order.Great! Your order has been placed: - 1 Pepperoni pizza with added olives - 1 Margherita pizza Delivery to: 1 Sesame Street, Amsterdam, North-Holand, 1111AB Thank you for ordering with us! Your pizzas will be delivered soon. Have a delicious day!代理能够准确理解用户意图收集必要信息并在对话过程中动态更新订单状态最终生成完整的订单结构化数据。6. 案例实现思路6.1 核心组件本案例的核心是两个新组件ArtifactEditorToolSpec- 基于Pydantic模型生成工具集允许创建和编辑结构化数据ArtifactMemoryBlock- 内存块用于存储和跟踪工件结构化数据的状态6.2 工作流程定义Pydantic模型Pizza、Address、Order来描述数据结构使用ArtifactEditorToolSpec基于Order模型创建工具集初始化包含ArtifactMemoryBlock的内存创建FunctionAgent配置工具和系统提示在聊天循环中代理根据用户输入调用相应工具工具调用会更新内存中的工件状态代理检查工件完整性并询问缺失信息6.3 工具操作ArtifactEditorToolSpec提供的工具包括create_artifact- 创建新的工件实例apply_patch- 应用JSON Patch操作更新工件get_current_artifact- 获取当前工件状态7. 扩展建议7.1 功能扩展多步骤验证- 添加地址验证、支付信息验证等订单跟踪- 集成订单跟踪系统提供实时状态更新个性化推荐- 基于历史订单提供个性化推荐多语言支持- 添加多语言对话能力7.2 技术优化错误处理- 增强错误处理和恢复机制并发控制- 支持多用户并发订单处理持久化存储- 将订单数据持久化到数据库集成外部API- 连接支付网关、配送系统等外部服务7.3 应用场景扩展医疗表单填写- 患者信息收集系统客户服务- 客户投诉处理和问题解决招聘流程- 候选人信息收集和面试安排金融服务- 贷款申请和开户流程8. 总结本案例展示了如何使用LlamaIndex的ArtifactEditorToolSpec和ArtifactMemoryBlock构建一个智能订单收集系统。通过定义Pydantic模型我们能够创建结构化的数据表示并让代理通过工具调用来填充这些结构。这种方法的优势在于结构化数据管理- 通过Pydantic模型确保数据的一致性和有效性动态更新- 使用JSON Patch操作灵活更新数据状态跟踪- 通过内存块跟踪数据收集进度自然交互- 代理能够以自然对话方式收集信息ArtifactEditorToolSpec为构建需要结构化数据收集的应用提供了强大而灵活的基础可以广泛应用于各种需要表单填写、信息收集和状态管理的场景。
上一篇/下一篇内容由系统自动关联 返回资讯列表 →