微信扫码
添加专属顾问
 
                        我要投稿
from langchain.agents import AgentExecutor, create_react_agentfrom langchain_core.prompts import PromptTemplatefrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAI@tooldef calculate(what: str) -> float:"""Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary"""return eval(what)@tooldef ask_fruit_unit_price(fruit: str) -> str:"""Asks the user for the price of a fruit"""if fruit.casefold() == "apple":return "Apple unit price is 10/kg"elif fruit.casefold() == "banana":return "Banana unit price is 6/kg"else:return "{} unit price is 20/kg".format(fruit)prompt = PromptTemplate.from_template('''Answer the following questions as best you can. You have access to the following tools:{tools}Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [{tool_names}]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: {input}Thought:{agent_scratchpad}''')tools = [calculate, ask_fruit_unit_price]model = ChatOpenAI(model="gpt-4o-mini")agent = create_react_agent(model, tools, prompt)agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)result = agent_executor.invoke({"input": "What is the total price of 3 kg of apple and 2 kg of banana?"})print(result)
@tooldef calculate(what: str) -> float:"""Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary"""return eval(what)@tooldef ask_fruit_unit_price(fruit: str) -> str:"""Asks the user for the price of a fruit"""if fruit.casefold() == "apple":return "Apple unit price is 10/kg"elif fruit.casefold() == "banana":return "Banana unit price is 6/kg"else:return "{} unit price is 20/kg".format(fruit)
@tooldef calculate(what: str) -> float:"""Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary"""return eval(what)print(calculate.name)print(calculate.description)print(calculate.args)
prompt = PromptTemplate.from_template('''Answer the following questions as best you can. You have access to the following tools:{tools}Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [{tool_names}]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: {input}Thought:{agent_scratchpad}''')
calculate(what: str) -> float - Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary
tools = [calculate, ask_fruit_unit_price]model = ChatOpenAI(model="gpt-4o-mini")agent = create_react_agent(model, tools, prompt)agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({    "input": "What is the total price of 3 kg of apple and 2 kg of banana?"})print(result)> Entering new AgentExecutor chain...To find the total price of 3 kg of apples and 2 kg of bananas, I need to know the unit prices of both fruits. I will first ask for the price of apples and then for the price of bananas.Action: ask_fruit_unit_priceAction Input: "apple"Apple unit price is 10/kgNow that I have the unit price of apples, I need to ask for the unit price of bananas.Action: ask_fruit_unit_priceAction Input: "banana"Banana unit price is 6/kgNow that I have the unit prices of both fruits (10/kg for apples and 6/kg for bananas), I can calculate the total price for 3 kg of apples and 2 kg of bananas.Action: calculateAction Input: "3 * 10 + 2 * 6" 42I now know the final answerFinal Answer: The total price of 3 kg of apples and 2 kg of bananas is 42.> Finished chain.{'input': 'What is the total price of 3 kg of apple and 2 kg of banana?', 'output': 'The total price of 3 kg of apples and 2 kg of bananas is 42.'}
shell_tool = ShellTool()tools = [shell_tool]model = ChatOpenAI(model="gpt-4o-mini")agent = create_react_agent(model, tools, prompt)agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)result = agent_executor.invoke({"input": "Create a file named 'test.txt' and write 'Hello, World!' to it."})
I need to create a file named 'test.txt' and then write the text 'Hello, World!' into it. I'll do this using shell commands in the terminal.Action: terminalAction Input: echo 'Hello, World!' > test.txtExecuting command:echo 'Hello, World!' > test.txtThe file 'test.txt' has been created with the content 'Hello, World!'.Final Answer: A file named 'test.txt' has been created with the content 'Hello, World!'.
github = GitHubAPIWrapper()toolkit = GitHubToolkit.from_github_api_wrapper(github)tools = toolkit.get_tools()agent = create_react_agent(model, tools, prompt)...
export GITHUB_APP_ID="your-app-id"export GITHUB_APP_PRIVATE_KEY="path-to-private-key"export GITHUB_REPOSITORY="your-github-repository"
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-31
Spring AI Alibaba/Dify/LangGraph/LangChain 一文讲清四大AI框架怎么选
2025-10-29
为什么我们选择 LangGraph 作为智能体系统的技术底座?
2025-10-27
Langchain 、 Manus 组了一个研讨会:Agent越智能,死得越快!
2025-10-23
LangChain V1.0 深度解析:手把手带你跑通全新智能体架构
2025-10-23
LangChain 与 LangGraph 双双发布 1.0:AI 智能体框架迎来里程碑时刻!
2025-10-19
AI 不再“乱跑”:LangChain × LangGraph 打造可控多阶段智能流程
2025-10-15
LangChain对话Manus创始人:顶级AI智能体上下文工程的“满分作业”首次公开
2025-10-09
Langchain回应OpenAI:为什么我们不做拖拉拽工作流
 
            2025-09-13
2025-09-21
2025-10-19
2025-08-19
2025-08-17
2025-09-19
2025-09-12
2025-09-06
2025-08-03
2025-08-29
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21
2025-05-19