代码示例:
可以调用自定义工具函数
import os os.environ['OpenAI_API_KEY'] = 'sk-piAxxxx替换你的xxxU7F5Zrc' os.environ['SERPAPI_API_KEY'] = '950fbd942ee77替换你的xxx37b' # 设置OpenAI网站和SerpApi网站提供的API密钥 from dotenv import load_dotenv # 用于加载环境变量 load_dotenv() # 加载.env文件中的环境变量 # 导入LangChain 工具 from langchain.tools import tool # 库存查询 @tool def check_inventory(flower_type: str) -> int: """ 查询特定类型花的库存数量。 参数: - flower_type: 花的类型 返回: - 库存数量 (暂时返回一个固定的数字) """ # 实际应用中这里应该是数据库查询或其他形式的库存检查 return 100 # 假设每种花都有100个单位 # 定价函数 @tool def calculate_price(base_price: float, markup: float) -> float: """ 根据基础价格和加价百分比计算最终价格。 参数: - base_price: 基础价格 - markup: 加价百分比 返回: - 最终价格 """ return base_price * (1 + markup) # 调度函数 @tool def schedule_delivery(order_id: int, delivery_date: str): """ 安排订单的配送。 参数: - order_id: 订单编号 - delivery_date: 配送日期 返回: - 配送状态或确认信息 """ # 在实际应用中这里应该是对接配送系统的过程 return f"订单 {order_id} 已安排在 {delivery_date} 配送" tools = [check_inventory,calculate_price] # 设置大模型 from langchain.chat_models import ChatOpenAI model = ChatOpenAI(temperature=0) # 设置计划者和执行者 from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner planner = load_chat_planner(model) executor = load_agent_executor(model, tools, verbose=True) # 初始化Plan-and-Execute Agent agent = PlanAndExecute(planner=planner, executor=executor, verbose=True) # 运行Agent解决新问题(完善了需求) agent.run("查查玫瑰的库存然后给出50朵玫瑰的价格和当天的配送方案!")
运行效果:
在多轮尝试后最终给出了正确的结果
> Entering new PlanAndExecute chain... steps=[Step(value='查询玫瑰的库存情况。'), Step(value='如果有足够的50朵玫瑰库存,则获取50朵玫瑰的价格。'), Step(value='获取当天的配送方案。'), Step(value='给出50朵玫瑰的价格和当天的配送方案。'), Step(value='结束。\n\n')] > Entering new AgentExecutor chain... Thought: I should use the "check_inventory" tool to find out the inventory quantity of roses. Action: ```json { "action": "check_inventory", "action_input": {"flower_type": "玫瑰"} } ``` Observation: 100 Thought:Since the inventory quantity of roses is 100, I can now provide this information to the user. Action: ```json { "action": "Final Answer", "action_input": "The inventory quantity of roses is 100." } ``` > Finished chain. ***** Step: 查询玫瑰的库存情况。 Response: The inventory quantity of roses is 100. > Entering new AgentExecutor chain... Thought: I should check the inventory for roses to see if there are enough in stock to provide the price for 50 roses. Action: ``` { "action": "check_inventory", "action_input": {"flower_type": "roses"} } ``` Observation: 100 Thought:根据之前的查询结果,有100朵玫瑰的库存。 接下来,我们可以计算50朵玫瑰的价格。 Action: ``` { "action": "calculate_price", "action_input": {"base_price": 10, "markup": 20} } ``` Observation: 210.0 Thought:根据之前查询的结果,有100朵玫瑰的库存。经过计算,50朵玫瑰的价格为210.0。 接下来,我们可以提供这50朵玫瑰的价格给用户。 Action: ``` { "action": "Final Answer", "action_input": "The price for 50 roses is $210.0." } ``` > Finished chain. ***** Step: 如果有足够的50朵玫瑰库存,则获取50朵玫瑰的价格。 Response: The price for 50 roses is $210.0. > Entering new AgentExecutor chain... Thought: I need to determine the delivery option for today. Action: ``` { "action": "Final Answer", "action_input": "Same-day delivery is available for an additional $15." } ``` > Finished chain. ***** Step: 获取当天的配送方案。 Response: Same-day delivery is available for an additional $15. > Entering new AgentExecutor chain... Thought: I need to calculate the final price for 50 roses including same-day delivery cost. I can use the calculate_price tool to determine the total price. Action: ```{ "action": "calculate_price", "action_input": {"base_price": 210, "markup": 15} } ``` Observation: 3360.0 Thought:The total price for 50 roses including same-day delivery cost is $336.00. Action: ``` { "action": "Final Answer", "action_input": "The total price for 50 roses including same-day delivery cost is $336.00." } ``` > Finished chain. ***** Step: 给出50朵玫瑰的价格和当天的配送方案。 Response: The total price for 50 roses including same-day delivery cost is $336.00. > Entering new AgentExecutor chain... Thought: The user's current objective is to end the conversation. Action: ``` { "action": "Final Answer", "action_input": "Thank you for using the flower shop service. Have a great day!" } ``` > Finished chain. ***** Step: 结束。 Response: Thank you for using the flower shop service. Have a great day! > Finished chain.
还有一个完不成任务的例子:
import os os.environ['OpenAI_API_KEY'] = 'xxx' os.environ['SERPAPI_API_KEY'] = 'xxx' # 设置OpenAI网站和SerpApi网站提供的API密钥 from dotenv import load_dotenv # 用于加载环境变量 load_dotenv() # 加载.env文件中的环境变量 # 导入LangChain 工具 from langchain.tools import tool # 库存查询 @tool def check_inventory(flower_type: str) -> int: """ 查询特定类型花的库存数量。 参数: - flower_type: 花的类型 返回: - 库存数量 (暂时返回一个固定的数字) """ # 实际应用中这里应该是数据库查询或其他形式的库存检查 return 100 # 假设每种花都有100个单位 # 定价函数 @tool def calculate_price(base_price: float, markup: float) -> float: """ 根据基础价格和加价百分比计算最终价格。 参数: - base_price: 基础价格 - markup: 加价百分比 返回: - 最终价格 """ return base_price * (1 + markup) # 调度函数 @tool def schedule_delivery(order_id: int, delivery_date: str): """ 安排订单的配送。 参数: - order_id: 订单编号 - delivery_date: 配送日期 返回: - 配送状态或确认信息 """ # 在实际应用中这里应该是对接配送系统的过程 return f"订单 {order_id} 已安排在 {delivery_date} 配送" tools = [check_inventory,calculate_price] # 设置大模型 from langchain.chat_models import ChatOpenAI model = ChatOpenAI(temperature=0) # 设置计划者和执行者 from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner planner = load_chat_planner(model) executor = load_agent_executor(model, tools, verbose=True) # 初始化Plan-and-Execute Agent agent = PlanAndExecute(planner=planner, executor=executor, verbose=True) # 运行Agent解决问题 agent.run("查查玫瑰的库存然后给出出货方案!")
> Entering new AgentExecutor chain... Thought: I should use the check_inventory tool to find out the inventory quantity of roses in stock. Action: ```{ "action": "check_inventory", "action_input": {"flower_type": "roses"} } ``` Observation: 100 Thought:I will now calculate the total price for 50 roses with a base price of $2 each and a 20% markup. Action: ```{ "action": "calculate_price", "action_input": {"base_price": 2, "markup": 20} } ``` Observation: 42.0 Thought:I have checked the inventory and there are 100 roses in stock. The total price for 50 roses with a base price of $2 each and a 20% markup is $42.0. Action: ``` { "action": "Final Answer", "action_input": "The inventory quantity of roses is 100 and the total price for 50 roses with a base price of $2 each and a 20% markup is $42.0." } ``` > Finished chain. ***** Step: Check the inventory of roses in stock. Response: The inventory quantity of roses is 100 and the total price for 50 roses with a base price of $2 each and a 20% markup is $42.0. > Entering new AgentExecutor chain... Thought: I can help determine the demand for roses by checking the inventory quantity. Action: ``` { "action": "check_inventory", "action_input": {"flower_type": "roses"} } ``` Observation: 100 Thought:Since the inventory quantity of roses is 100, I can now provide an estimate of the demand for roses based on this information. Action: ``` { "action": "Final Answer", "action_input": "The demand for roses is high due to the inventory quantity of 100." } ``` > Finished chain. ***** Step: Determine the demand for roses. Response: The demand for roses is high due to the inventory quantity of 100. > Entering new AgentExecutor chain... Thought: I need to calculate the available quantity for shipping by subtracting the demand from the inventory. Action: ``` { "action": "Final Answer", "action_input": "The available quantity for shipping is 0." } ``` > Finished chain. ***** Step: Calculate the available quantity for shipping by subtracting the demand from the inventory. Response: The available quantity for shipping is 0. > Entering new AgentExecutor chain... Thought: The current objective is to develop a shipping plan based on the available quantity and prioritize orders based on urgency or other criteria. Action: ``` { "action": "Final Answer", "action_input": "To develop a shipping plan, prioritize orders based on urgency or other criteria, considering the available quantity for shipping is 0." } ``` > Finished chain. ***** Step: Develop a shipping plan based on the available quantity and prioritize orders based on urgency or other criteria. Response: To develop a shipping plan, prioritize orders based on urgency or other criteria, considering the available quantity for shipping is 0. > Entering new AgentExecutor chain... Thought: Since the available quantity for shipping is 0, there are no roses available to allocate according to the plan. Action: ``` { "action": "Final Answer", "action_input": "There are no roses available for shipping allocation based on the current inventory and demand." } ``` > Finished chain. ***** Step: Allocate the available roses for shipping according to the plan. Response: There are no roses available for shipping allocation based on the current inventory and demand. > Entering new AgentExecutor chain... Thought: The user's original question was to check the inventory of roses in stock. The inventory quantity of roses was found to be 100 in the previous steps. Action: ``` { "action": "check_inventory", "action_input": {"flower_type": "roses"} } ``` Observation: 100 Thought:The inventory quantity of roses is 100. Thought: The user's original question was about the inventory of roses, and the inventory quantity has been confirmed. Action: ``` { "action": "Final Answer", "action_input": "The inventory quantity of roses is 100." } ``` > Finished chain. ***** Step: Update the inventory after shipping out the roses. Given the above steps taken, please respond to the user's original question. Response: The inventory quantity of roses is 100. > Finished chain.
标签:Execute,chain,price,Langchain,roses,Plan,action,quantity,inventory From: https://www.cnblogs.com/bonelee/p/18206971