I believe this code as printed in the book "Generative AI with LangChain" relies on and older version of langchain. langchain[docarray]==0.0.284 to be exact.
I suggest setting up a conda environment for the book as there seemed to be breaking changes.
If on the other hand you would want to use the newest LangChain version, this would work:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType, Tool
from langchain.utilities import PythonREPL
from langchain_experimental.tools import PythonREPLTool
agent = initialize_agent(
tools=[PythonREPLTool()],
llm=llm,
)
agent.invoke("what is 2 + 2?")ython_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
agent = initialize_agent(
tools=[python_repl],
llm=llm,
)
agent.invoke("what is 2 + 2?")
Answer from AI-Guru on Stack OverflowI believe this code as printed in the book "Generative AI with LangChain" relies on and older version of langchain. langchain[docarray]==0.0.284 to be exact.
I suggest setting up a conda environment for the book as there seemed to be breaking changes.
If on the other hand you would want to use the newest LangChain version, this would work:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType, Tool
from langchain.utilities import PythonREPL
from langchain_experimental.tools import PythonREPLTool
agent = initialize_agent(
tools=[PythonREPLTool()],
llm=llm,
)
agent.invoke("what is 2 + 2?")ython_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
agent = initialize_agent(
tools=[python_repl],
llm=llm,
)
agent.invoke("what is 2 + 2?")
If you check the source code of load_tools method, you can actually see that the allowed input names are defined inside the following dictionaries:
- _LLM_TOOLS
- _EXTRA_LLM_TOOLS
- _EXTRA_OPTIONAL_TOOLS
- DANGEROUS_TOOLS
It's correct that python_repl name is not recognized. However, you can actually use it as tool like the following:
import os
from langchain_openai import AzureChatOpenAI
from langchain_experimental.tools import PythonREPLTool
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
llm = AzureChatOpenAI(
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
model_version=os.environ["AZURE_OPENAI_MODEL_VERSION"]
)
tools = [PythonREPLTool()]
instructions = """You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
"""
base_prompt = hub.pull("langchain-ai/react-agent-template")
prompt = base_prompt.partial(instructions=instructions)
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "What is the 10th fibonacci number?"})
Giving as output something like:
Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Python_REPL
Action Input:
```
def fibonacci(n):
if n<=1:
return n
else:
return fibonacci(n-1)+fibonacci(n-2)
print(fibonacci(9))
```Python REPL can execute arbitrary code. Use with caution.
34
Do I need to use a tool? No
Final Answer: The 10th fibonacci number is 55.
Finished chain.