mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-14 21:59:33 +03:00
15 KiB
15 KiB
In [1]:
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAIIn [2]:
# Load the tool configs that are needed.
from langchain import LLMMathChain, SerpAPIWrapper
llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
),
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math"
)
]In [3]:
# Construct the agent. We will use the default agent type here.
# See documentation for a full list of options.
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [11]:
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")Out [11]:
[1m> Entering new AgentWithTools chain...[0m [32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. Action: Search Action Input: Olivia Wilde's boyfriend[0m Observation: [36;1m[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.[0m Thought:[32;1m[1;3m I need to find out Harry Styles' age. Action: Search Action Input: Harry Styles age[0m Observation: [36;1m[1;3m28 years[0m Thought:[32;1m[1;3m I need to calculate 28 raised to the 0.23 power. Action: Calculator Action Input: 28^0.23[0m [1m> Entering new LLMMathChain chain...[0m 28^0.23[32;1m[1;3m ```python import math print(math.pow(28, 0.23)) ``` [0m Answer: [33;1m[1;3m2.1520202182226886 [0m [1m> Finished LLMMathChain chain.[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.[0m [1m> Finished AgentWithTools chain.[0m
"Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886."
In [4]:
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)In [5]:
response = agent({"input":"How old is Olivia Wilde's boyfriend? What is that number raised to the 0.23 power?"})[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out how old Olivia Wilde's boyfriend is, and then use a calculator to calculate the power. Action: Search Action Input: Olivia Wilde's boyfriend age[0m Observation: [36;1m[1;3mWhile Wilde, 37, and Styles, 27, have both kept a low profile when it comes to talking about their relationship, Wilde did address their ...[0m Thought:[32;1m[1;3m Olivia Wilde's boyfriend is 27 years old. Action: Calculator Action Input: 27^0.23[0m [1m> Entering new LLMMathChain chain...[0m 27^0.23[32;1m[1;3m ```python import math print(math.pow(27, 0.23)) ``` [0m Answer: [33;1m[1;3m2.1340945944237553 [0m [1m> Finished LLMMathChain chain.[0m Observation: [33;1m[1;3mAnswer: 2.1340945944237553 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: 2.1340945944237553[0m [1m> Finished AgentExecutor chain.[0m
In [27]:
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])[(AgentAction(tool='Search', tool_input="Olivia Wilde's boyfriend age", log=" I need to find out how old Olivia Wilde's boyfriend is, and then use a calculator to calculate the power.\nAction: Search\nAction Input: Olivia Wilde's boyfriend age"), 'While Wilde, 37, and Styles, 27, have both kept a low profile when it comes to talking about their relationship, Wilde did address their ...'), (AgentAction(tool='Calculator', tool_input='27^0.23', log=" Olivia Wilde's boyfriend is 27 years old.\nAction: Calculator\nAction Input: 27^0.23"), 'Answer: 2.1340945944237553\n')]
In [26]:
import json
print(json.dumps(response["intermediate_steps"], indent=2))[
[
[
"Search",
"Olivia Wilde's boyfriend age",
" I need to find out how old Olivia Wilde's boyfriend is, and then use a calculator to calculate the power.\nAction: Search\nAction Input: Olivia Wilde's boyfriend age"
],
"While Wilde, 37, and Styles, 27, have both kept a low profile when it comes to talking about their relationship, Wilde did address their ..."
],
[
[
"Calculator",
"27^0.23",
" Olivia Wilde's boyfriend is 27 years old.\nAction: Calculator\nAction Input: 27^0.23"
],
"Answer: 2.1340945944237553\n"
]
]
In [4]:
from langchain.agents import load_tools
tools = load_tools(["serpapi", "llm-math"], llm=llm)In [5]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [12]:
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")Out [12]:
[1m> Entering new AgentWithTools chain...[0m [32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. Action: Search Action Input: Olivia Wilde's boyfriend[0m Observation: [36;1m[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.[0m Thought:[32;1m[1;3m I need to find out Harry Styles' age. Action: Search Action Input: Harry Styles age[0m Observation: [36;1m[1;3m28 years[0m Thought:[32;1m[1;3m I need to calculate 28 raised to the 0.23 power. Action: Calculator Action Input: 28^0.23[0m [1m> Entering new LLMMathChain chain...[0m 28^0.23[32;1m[1;3m ```python import math print(math.pow(28, 0.23)) ``` [0m Answer: [33;1m[1;3m2.1520202182226886 [0m [1m> Finished LLMMathChain chain.[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.[0m [1m> Finished AgentWithTools chain.[0m
"Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886."
In [ ]: