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 Overflow
๐ŸŒ
Langchain
docs.langchain.com โ€บ oss โ€บ python โ€บ integrations โ€บ tools โ€บ python
Python REPL integration - Docs by LangChain
In order to easily do that, we provide a simple Python REPL to execute commands in. This interface will only return things that are printed - therefore, if you want to use it to calculate an answer, make sure to have it print out the answer. Python REPL can execute arbitrary code on the host machine (e.g., delete files, make network requests).
Top answer
1 of 2
2

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?")
2 of 2
0

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.
๐ŸŒ
Kaggle
kaggle.com โ€บ code โ€บ ksmooi โ€บ langchain-data-analysis-with-repl-tool-and-llm
LangChain: Data Analysis with REPL-Tool and LLM
Checking your browser before accessing www.kaggle.com ยท Click here if you are not automatically redirected after 5 seconds
๐ŸŒ
GitHub
github.com โ€บ langchain-ai โ€บ langchain โ€บ issues โ€บ 10079
how to use PythonREPL tool to take dataframe and query ยท Issue #10079 ยท langchain-ai/langchain
September 1, 2023 - Issue you'd like to raise. I need to use Python REPL tool to take data frame and user query and answer based on the data frame. Suggestion: No response
Author ย  bmshambu
๐ŸŒ
LangChain
api.python.langchain.com โ€บ en โ€บ latest โ€บ tools โ€บ langchain_experimental.tools.python.tool.PythonREPLTool.html
langchain_experimental.tools.python.tool.PythonREPLTool โ€” ๐Ÿฆœ๐Ÿ”— LangChain 0.2.17
class langchain_experimental.tools.python.tool.PythonREPLTool[source]ยถ ยท Bases: BaseTool ยท Tool for running python code in a REPL. Initialize the tool. param args_schema: Optional[TypeBaseModel] = Noneยถ ยท Pydantic model class to validate and parse the toolโ€™s input arguments.
Find elsewhere
๐ŸŒ
scikit-learn
sj-langchain.readthedocs.io โ€บ en โ€บ latest โ€บ tools โ€บ langchain.tools.python.tool.PythonREPLTool.html
langchain.tools.python.tool.PythonREPLTool โ€” ๐Ÿฆœ๐Ÿ”— LangChain 0.0.249
param python_repl: langchain.utilities.python.PythonREPL [Optional]ยถ ยท param return_direct: bool = Falseยถ ยท Whether to return the toolโ€™s output directly. Setting this to True means ยท that after the tool is called, the AgentExecutor will stop looping. param sanitize_input: bool = Trueยถ ยท
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
Enabling the 'terminal' and 'python-repl' tools in a langchain agent demonstrate... | Hacker News
March 28, 2023 - The link below is the transcript of a session in which I asked the agent to create a hello world script and executes it. The only input I provide is on line 17. Everything else is the langchain agent iteratively taking an action, observing the results and deciding the next action to take ยท ...
๐ŸŒ
scikit-learn
sj-langchain.readthedocs.io โ€บ en โ€บ latest โ€บ utilities โ€บ langchain.utilities.python.PythonREPL.html
langchain.utilities.python.PythonREPL โ€” ๐Ÿฆœ๐Ÿ”— LangChain 0.0.249
class langchain.utilities.python.PythonREPL(*, _globals: Optional[Dict] = None, _locals: Optional[Dict] = None)[source]ยถ ยท Bases: BaseModel ยท Simulates a standalone Python REPL. Create a new model by parsing and validating input data from keyword arguments.
๐ŸŒ
GitHub
gist.github.com โ€บ wiseman โ€บ 4a706428eaabf4af1002a07a114f61d6
Langchain example: self-debugging ยท GitHub
to this : python_repl = Tool( "PythonREPL",... #no space ... Same here @kplr-io, it seems like the tool name string shouldn't contain spaces to function correctly. langchain = 0.1.13
๐ŸŒ
GitHub
github.com โ€บ langchain-ai โ€บ langchain โ€บ discussions โ€บ 22841
Extracting python code from PythonREPLTool ยท langchain-ai/langchain ยท Discussion #22841
June 13, 2024 - 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.
Author ย  langchain-ai