在LangChain中,如何将详细输出保存到变量?

huangapple go评论72阅读模式
英文:

In LangChain, how to save the verbose output to a variable?

问题

我尝试执行一个langchain代理。我想将verbose的输出保存到一个变量中,但我只能从agent.run中访问最终答案。

如何将verbose输出保存到一个变量中,以便我以后可以使用它?

我的代码:

import json
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.agents import Tool
from langchain.utilities import PythonREPL

llm = OpenAI(temperature=0.1)

## 定义工具
python_repl = PythonREPL()

tools = load_tools(["python_repl", "llm-math"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

response = agent.run("What is 3^2. Use calculator to solve.")

我尝试访问来自代理的响应,但它只是最终答案,而不是verbose输出。

打印response只会输出9。但我想要像下面这样的verbose过程:

> 进入新的AgentExecutor链...
 我需要使用计算器来解决这个问题。
动作:计算器
动作输入:3^2
观察:答案:9
思考:我现在知道了最终答案。
最终答案:9
英文:

I tried executing a langchain agent. I want to save the output from verbose into a variable, but all I can access from the agent.run is only the final answer.

How can I save the verbose output to a variable so that I can use later?

My code:

import json
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.agents import Tool
from langchain.utilities import PythonREPL

llm = OpenAI(temperature=0.1)

## Define Tools
python_repl = PythonREPL()

tools = load_tools(["python_repl", "llm-math"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

response = agent.run("What is 3^2. Use calculator to solve.")

I tried accessing the response from the agent, but it's only the final answer instead of the verbose output.

printing response gives only 9. But I would like the verbose process like:

> Entering new AgentExecutor chain...
 I need to use the calculator to solve this.
Action: Calculator
Action Input: 3^2
Observation: Answer: 9
Thought: I now know the final answer.
Final Answer: 9

答案1

得分: 3

I don't find any API to save verbose output as a variable.

However, I think an alternative solution to the question can be achieved by accessing intermediate steps in this link.

That is set return_intermediate_steps=True,

agent = initialize_agent(
  tools, 
  llm,
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, 
  verbose=True,
  return_intermediate_steps=True
)

and use response = agent({"input":"What is 3^2. Use calculator to solve"}) instead of agent.run.

Finally, you can access the intermediate steps in response["intermediate_steps"]

Hope this will help.

英文:

I don't find any API to save verbose output as a variable.

However, I think an alternative solution to the question can be achieved by access intermediate steps in this link.

That is set return_intermediate_steps=True,

agent = initialize_agent(
  tools, 
  llm,
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, 
  verbose=True,
  return_intermediate_steps=True
)

and use response = agent({"input":"What is 3^2. Use calculator to solve"}) instead of agent.run.

Finally, you can access the intermediate steps in response["intermediate_steps"]

Hope this will help.

huangapple
  • 本文由 发表于 2023年6月5日 19:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定