微信扫码
和创始人交个朋友
我要投稿
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-02-22
手把手教你使用Qwen-Agent开发智能体应用实战教程
2025-02-21
deepseekers 第一个全面拥抱 deepseek 的国产 AI Agent 框架
2025-02-19
LangMem 发布:任何人都能轻松构建智能体记忆!
2025-02-19
深入探究Langchain v0.3:全面解读
2025-02-18
走进Langchain:全面解析
2025-02-17
基于LangChain爬虫增强RAG应用
2025-02-13
LangChat实战DeepSeek-R1模型
2025-02-05
揭秘LangGraph!如何一步一步构建动态订单管理系统?
2024-10-10
2024-04-08
2024-06-03
2024-09-04
2024-08-18
2024-07-13
2024-04-08
2024-06-24
2024-07-10
2024-04-17
2025-02-05
2024-12-02
2024-11-25
2024-10-30
2024-10-11
2024-08-18
2024-08-16
2024-08-04