微信扫码
添加专属顾问
我要投稿
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
@tool
def 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)
@tool
def 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 answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
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)
@tool
def 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)
@tool
def 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)
@tool
def 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 answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
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_price
Action Input: "apple"
Apple unit price is 10/kg
Now that I have the unit price of apples, I need to ask for the unit price of bananas.
Action: ask_fruit_unit_price
Action Input: "banana"
Banana unit price is 6/kg
Now 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: calculate
Action Input: "3 * 10 + 2 * 6" 42
I now know the final answer
Final 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: terminal
Action Input: echo 'Hello, World!' > test.txt
Executing command:
echo 'Hello, World!' > test.txt
The 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-07-31
Agent真的很热!不玩一下说不过去!所以我捣鼓了下这个东西
2025-07-30
用 LangChain 打造你的第一个 AI Agent:从零到一的实践指南(含关键代码)
2025-07-29
LangGraph v0.6 重磅发布!全新 Context API 让智能体开发更简单
2025-07-25
LangChain---Memory:为AI应用赋予“记忆”
2025-07-23
Langchain学习教程一(Hello World)
2025-07-23
Langchain学习教程二(提示词模板和 LCEL 语法)
2025-07-23
Langchain 教程三(FewShotChatMessagePromptTemplate)
2025-07-23
Langchain 教程四(OutputParser)
2025-06-05
2025-05-06
2025-05-28
2025-05-08
2025-05-28
2025-07-14
2025-05-19
2025-06-26
2025-05-19
2025-05-14
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21
2025-05-19
2025-05-08