微信扫码
添加专属顾问
我要投稿
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-12-08
让AI智能体拥有像人类的持久记忆:基于LangGraph的长短期记忆管理实践指南
2025-12-04
Agentic RAG这样用LangChain解决复杂问题
2025-12-01
Deep Agent 进化论:基于文件系统的 Context Engineering 深度解析
2025-11-27
langgraph 1.0.4 最新发布:功能优化与修复详解
2025-11-25
LangChain 最新agent框架deepagents测评:长任务友好,高可控
2025-11-25
被 LangChain 全家桶搞晕了?LangGraph、LangSmith、LangFlow 一文读懂
2025-11-21
如何用 LangGraph 构建高效的 Agentic 系统
2025-11-19
LangChain v1.0 模型选型:静态还是动态?一文看懂 Agent 的正确打开方式
2025-11-03
2025-09-21
2025-10-23
2025-10-19
2025-10-31
2025-11-06
2025-11-05
2025-09-19
2025-10-23
2025-11-01
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21