mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
87e502c6bc
Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
68 lines
1.7 KiB
Plaintext
68 lines
1.7 KiB
Plaintext
```python
|
|
from langchain.agents import load_tools
|
|
from langchain.agents import initialize_agent
|
|
from langchain.agents import AgentType
|
|
from langchain.llms import OpenAI
|
|
```
|
|
|
|
First, let's load the language model we're going to use to control the agent.
|
|
|
|
|
|
```python
|
|
llm = OpenAI(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.
|
|
|
|
|
|
```python
|
|
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.
|
|
|
|
|
|
```python
|
|
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
|
```
|
|
|
|
Now let's test it out!
|
|
|
|
|
|
```python
|
|
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
|
|
```
|
|
|
|
<CodeOutputBlock lang="python">
|
|
|
|
```
|
|
|
|
|
|
> Entering new AgentExecutor chain...
|
|
I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
|
|
Action: Search
|
|
Action Input: "Leo DiCaprio girlfriend"
|
|
Observation: Camila Morrone
|
|
Thought: I need to find out Camila Morrone's age
|
|
Action: Search
|
|
Action Input: "Camila Morrone age"
|
|
Observation: 25 years
|
|
Thought: I need to calculate 25 raised to the 0.43 power
|
|
Action: Calculator
|
|
Action Input: 25^0.43
|
|
Observation: Answer: 3.991298452658078
|
|
|
|
Thought: I now know the final answer
|
|
Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.
|
|
|
|
> Finished chain.
|
|
|
|
|
|
|
|
|
|
|
|
"Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078."
|
|
```
|
|
|
|
</CodeOutputBlock>
|