mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-07 18:35:22 +03:00
The current example in https://python.langchain.com/en/latest/modules/agents/plan_and_execute.html has inconsistent reasoning step (observing 28 years and thinking it's 26 years): ``` Observation: 28 years Thought:Based on my search, Gigi Hadid's current age is 26 years old. Action: { "action": "Final Answer", "action_input": "Gigi Hadid's current age is 26 years old." } ``` Guessing this is model noise. Rerunning seems to give correct answer of 28 years.
10 KiB
10 KiB
In [1]:
from langchain.chat_models import ChatOpenAI
from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.llms import OpenAI
from langchain import SerpAPIWrapper
from langchain.agents.tools import Tool
from langchain import LLMMathChainIn [2]:
search = SerpAPIWrapper()
llm = OpenAI(temperature=0)
llm_math_chain = LLMMathChain.from_llm(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]:
model = ChatOpenAI(temperature=0)In [4]:
planner = load_chat_planner(model)In [6]:
executor = load_agent_executor(model, tools, verbose=True)In [9]:
agent = PlanAndExecute(planner=planner, executer=executor, verbose=True)In [10]:
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")Out [10]:
[1m> Entering new PlanAndExecute chain...[0m
steps=[Step(value="Search for Leo DiCaprio's girlfriend on the internet."), Step(value='Find her current age.'), Step(value='Raise her current age to the 0.43 power using a calculator or programming language.'), Step(value='Output the result.'), Step(value="Given the above steps taken, respond to the user's original question.\n\n")]
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction:
```
{
"action": "Search",
"action_input": "Who is Leo DiCaprio's girlfriend?"
}
```
[0m
Observation: [36;1m[1;3mDiCaprio broke up with girlfriend Camila Morrone, 25, in the summer of 2022, after dating for four years. He's since been linked to another famous supermodel – Gigi Hadid. The power couple were first supposedly an item in September after being spotted getting cozy during a party at New York Fashion Week.[0m
Thought:[32;1m[1;3mBased on the previous observation, I can provide the answer to the current objective.
Action:
```
{
"action": "Final Answer",
"action_input": "Leo DiCaprio is currently linked to Gigi Hadid."
}
```
[0m
[1m> Finished chain.[0m
*****
Step: Search for Leo DiCaprio's girlfriend on the internet.
Response: Leo DiCaprio is currently linked to Gigi Hadid.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction:
```
{
"action": "Search",
"action_input": "What is Gigi Hadid's current age?"
}
```
[0m
Observation: [36;1m[1;3m28 years[0m
Thought:[32;1m[1;3mPrevious steps: steps=[(Step(value="Search for Leo DiCaprio's girlfriend on the internet."), StepResponse(response='Leo DiCaprio is currently linked to Gigi Hadid.'))]
Current objective: value='Find her current age.'
Action:
```
{
"action": "Search",
"action_input": "What is Gigi Hadid's current age?"
}
```
[0m
Observation: [36;1m[1;3m28 years[0m
Thought:[32;1m[1;3mPrevious steps: steps=[(Step(value="Search for Leo DiCaprio's girlfriend on the internet."), StepResponse(response='Leo DiCaprio is currently linked to Gigi Hadid.')), (Step(value='Find her current age.'), StepResponse(response='28 years'))]
Current objective: None
Action:
```
{
"action": "Final Answer",
"action_input": "Gigi Hadid's current age is 28 years."
}
```
[0m
[1m> Finished chain.[0m
*****
Step: Find her current age.
Response: Gigi Hadid's current age is 28 years.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction:
```
{
"action": "Calculator",
"action_input": "28 ** 0.43"
}
```
[0m
[1m> Entering new LLMMathChain chain...[0m
28 ** 0.43[32;1m[1;3m
```text
28 ** 0.43
```
...numexpr.evaluate("28 ** 0.43")...
[0m
Answer: [33;1m[1;3m4.1906168361987195[0m
[1m> Finished chain.[0m
Observation: [33;1m[1;3mAnswer: 4.1906168361987195[0m
Thought:[32;1m[1;3mThe next step is to provide the answer to the user's question.
Action:
```
{
"action": "Final Answer",
"action_input": "Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."
}
```
[0m
[1m> Finished chain.[0m
*****
Step: Raise her current age to the 0.43 power using a calculator or programming language.
Response: Gigi Hadid's current age raised to the 0.43 power is approximately 4.19.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction:
```
{
"action": "Final Answer",
"action_input": "The result is approximately 4.19."
}
```
[0m
[1m> Finished chain.[0m
*****
Step: Output the result.
Response: The result is approximately 4.19.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction:
```
{
"action": "Final Answer",
"action_input": "Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."
}
```
[0m
[1m> Finished chain.[0m
*****
Step: Given the above steps taken, respond to the user's original question.
Response: Gigi Hadid's current age raised to the 0.43 power is approximately 4.19.
[1m> Finished chain.[0m
"Gigi Hadid's current age raised to the 0.43 power is approximately 4.19."
In [ ]: