from langchain.agents import initialize_agent
from langchain.llms.fake import FakeListLLM
from langchain.agents import AgentType
from langchain_experimental.tools import PythonAstREPLTool
res = ["Action: Python REPL\nAction Input: print(2.2 + 2.22)", "Final Answer: 4.42"]
llm = FakeListLLM(responses=res)
agent = initialize_agent(tools=[PythonAstREPLTool()],
llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run("what is 2.2 + 2.22?")
这段代码有什么问题?我收到以下错误:
有人可以向我解释一下使用
Langchain
实现假 LLM 的更好方法吗?
观察结果说明了一切。使用
python_repl_ast
代替 Python REPL
。以下是更新后的代码:
from langchain.agents import initialize_agent
from langchain.llms.fake import FakeListLLM
from langchain.agents import AgentType
from langchain_experimental.tools import PythonAstREPLTool
res = ["Action: python_repl_ast\nAction Input: print(2.2 + 2.22)", "Final Answer: 4.42"]
llm = FakeListLLM(responses=res)
agent = initialize_agent(tools=[PythonAstREPLTool()], llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("what is 2.2 + 2.22?")
输出: