mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
6.2 KiB
6.2 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, SerpAPIChain
llm = OpenAI(temperature=0)
search = SerpAPIChain()
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 [4]:
agent.run("What is the age of Olivia Wilde's boyfriend raised to the 0.23 power?")Out [4]:
What is the age of Olivia Wilde's boyfriend raised to the 0.23 power? Thought:[32;1m[1;3m I need to find the age of Olivia Wilde's boyfriend 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 the age of Harry Styles 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 to the 0.23 power Action: Calculator Action Input: 28^0.23[0m [1m> Entering new chain...[0m 28^0.23[32;1m[1;3m ```python print(28**0.23) ``` [0m Answer: [33;1m[1;3m2.1520202182226886 [0m [1m> Finished chain.[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 2.1520202182226886[0m
'2.1520202182226886'
In [ ]: