搭建工具
- serpai是一个聚合搜索引擎,需要安装谷歌搜索包以及申请账号 https://serpapi.com/manage-api-key
- llm-math是一个封装好的数学计算链
from langchain.agents import load_tools
tools = load_tools(["serpapi","llm-math"], llm=llm)
定义agent,使用小样本增强生成类型
from langchain.agents import initialize_agent
from langchain.agents import AgentType
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,#这里有不同的类型
verbose=True,#是否打印日志
)
agent.run("请问现任的美国总统是谁?他的年龄的平方是多少?")
/Users/tomiezhang/.pyenv/versions/3.10.12/envs/langchains/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function run
was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.
warn_deprecated(
Entering new AgentExecutor chain… I need to find out who the current president of the United States is and then calculate the square of his age. Action: Search Action Input: current president of the United States Observation: Joe Biden Thought:Now I need to find out Joe Biden’s age and calculate the square of it. Action: Search Action Input: Joe Biden age Observation: 81 years Thought:I will now calculate the square of Joe Biden’s age. Action: Calculator Action Input: 81 * 81 Observation: Answer: 6561 Thought:I now know the final answer. Final Answer: Joe Biden, 81 years old, square of his age is 6561.
Finished chain.
‘Joe Biden, 81 years old, square of his age is 6561.’