mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
87e502c6bc
Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
56 lines
1.8 KiB
Plaintext
56 lines
1.8 KiB
Plaintext
```python
|
|
from langchain.agents import load_tools
|
|
from langchain.agents import initialize_agent
|
|
from langchain.agents import AgentType
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.llms import OpenAI
|
|
|
|
# First, let's load the language model we're going to use to control the agent.
|
|
chat = ChatOpenAI(temperature=0)
|
|
|
|
# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
|
|
llm = OpenAI(temperature=0)
|
|
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
|
|
|
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
|
agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
|
|
|
# Now let's test it out!
|
|
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
|
|
```
|
|
|
|
```pycon
|
|
> Entering new AgentExecutor chain...
|
|
Thought: I need to use a search engine to find Olivia Wilde's boyfriend and a calculator to raise his age to the 0.23 power.
|
|
Action:
|
|
{
|
|
"action": "Search",
|
|
"action_input": "Olivia Wilde boyfriend"
|
|
}
|
|
|
|
Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.
|
|
Thought:I need to use a search engine to find Harry Styles' current age.
|
|
Action:
|
|
{
|
|
"action": "Search",
|
|
"action_input": "Harry Styles age"
|
|
}
|
|
|
|
Observation: 29 years
|
|
Thought:Now I need to calculate 29 raised to the 0.23 power.
|
|
Action:
|
|
{
|
|
"action": "Calculator",
|
|
"action_input": "29^0.23"
|
|
}
|
|
|
|
Observation: Answer: 2.169459462491557
|
|
|
|
Thought:I now know the final answer.
|
|
Final Answer: 2.169459462491557
|
|
|
|
> Finished chain.
|
|
'2.169459462491557'
|
|
```
|
|
|