The observation says it all. Use python_repl_ast instead of Python REPL. Following is the updated code:
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?")
Output:

LangChain
api.python.langchain.com › en › latest › tools › langchain_experimental.tools.python.tool.PythonAstREPLTool.html
langchain_experimental.tools.python.tool.PythonAstREPLTool — 🦜🔗 LangChain 0.2.17
PythonAstREPLTool implements the standard Runnable Interface.
GitHub
github.com › langchain-ai › langchain › issues › 10583
PythonAstREPLTool won't execute code with functions/lambdas · Issue #10583 · langchain-ai/langchain
September 14, 2023 - from langchain.tools.python.tool import PythonAstREPLTool query = """ import pandas as pd import random import string def generate_random_text(): return ''.join(random.choices(string.ascii_letters + string.digits, k=128)) df = pd.DataFrame({ 'Column1': [generate_random_text() for _ in range(1000)], 'Column2': [generate_random_text() for _ in range(1000)], 'Column3': [generate_random_text() for _ in range(1000)] }) df """ ast_repl = PythonAstREPLTool() ast_repl(query) >>> "NameError: name 'generate_random_text' is not defined" I expect it to return a df.
Author ivandkh
artificial intelligence - Observation: Python REPL is not a valid tool, try one of [python_repl_ast]. in Langchain - Stack Overflow
from langchain.agents import initialize_agent from langchain.llms.fake import FakeListLLM from langchain.agents import AgentType from langchain_experimental.tools import PythonAstREPLTool res = [&q... More on stackoverflow.com
use some python package for python agent
Yes, it’s very possible with custom tools https://python.langchain.com/docs/modules/agents/tools/custom_tools#subclassing-the-basetool . You can basically just create a class that defines the behavior of the tool and is just a function that uses the library that you wish to incorporate More on reddit.com
Consider changing PythonAstREPLTool default for locals to be None
Checked other resources I added a very descriptive title to this issue. I searched the LangChain documentation with the integrated search. I used the GitHub search to find a similar question and di... More on github.com
python - Langchain pandas agent unable to run pandas commands - Stack Overflow
I'm trying to use langchain's pandas agent on python for some development work but it goes into a recursive loop due to it being unable to take action on a thought, the thought being, having to run... More on stackoverflow.com
Videos
scikit-learn
sj-langchain.readthedocs.io › en › latest › tools › langchain.tools.python.tool.PythonAstREPLTool.html
langchain.tools.python.tool.PythonAstREPLTool — 🦜🔗 LangChain 0.0.249
class langchain.tools.python.tool.PythonAstREPLTool(*, name: str = 'python_repl_ast', description: str = 'A Python shell. Use this to execute python commands. Input should be a valid python command.
GitHub
github.com › langchain-ai › langchain › discussions › 27565
PythonAstREPLTool - How to add few-shot examples? · langchain-ai/langchain · Discussion #27565
October 22, 2024 - Examples: 1. Input: `print("Hello, World!")` Output: `Hello, World!` 2. Input: `1 + 1` Output: `2` 3. Input: `len("LangChain")` Output: `9` ... """ # Initialize the text splitter text_splitter = RecursiveCharacterTextSplitter( chunk_size=1024, # Adjust the chunk size as needed chunk_overlap=100 # Optional: overlap to maintain context ) # Split the long description chunks = text_splitter.split(long_description) # Use the first chunk as the description tool = PythonAstREPLTool(description=chunks[0])
Author langchain-ai
LangChain
python.langchain.com › api_reference › experimental › tools › langchain_experimental.tools.python.tool.PythonAstREPLTool.html
PythonAstREPLTool — 🦜🔗 LangChain documentation
PythonAstREPLTool implements the standard Runnable Interface.
GitHub
microsoft.github.io › autogen › 0.4.0 › › reference › python › autogen_ext.tools.langchain.html
autogen_ext.tools.langchain — AutoGen
Use the PythonAstREPLTool from the langchain_experimental package to create a tool that allows you to interact with a Pandas DataFrame.
Reddit
reddit.com › r/langchain › use some python package for python agent
r/LangChain on Reddit: use some python package for python agent
November 26, 2023 -
Hi, i want to use langchain python agent and openai, to use some python packages. e.g. scanpy, and some python data object like adata.
agent_executor = create_python_agent(
llm=openai_llm,
tool=PythonAstREPLTool(locals={"adata": adata, "scanpy": sc} ),
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)but got error message like this one
However, since I cannot import external libraries like scanpy in this environment or access external data objects like `adata`, I cannot execute this task directly.
My question is how can I use package like scanpy with python agent, is that even possible?
Top answer 1 of 3
1
Yes, it’s very possible with custom tools https://python.langchain.com/docs/modules/agents/tools/custom_tools#subclassing-the-basetool . You can basically just create a class that defines the behavior of the tool and is just a function that uses the library that you wish to incorporate
2 of 3
1
u/gordlesio Thanks! You are very helpful! Would like your comment on my thinking here: python agent (including pandas agent) is very powerful, it knows how to call a method given the prompt. e.g., if you use pandas agent and give it a table, and ask to generate a correlation stat, it can do that, and import related libraries, and call the method appropriately, etc. However, when asking something outside its comfort zone (a list of core libraries, pandas, stats, scipy, ... limited ones), when it realize it need to use some other libraries e.g. scanpy, it just output some message saying it cannot do because it cannot import it. We can generate some custom-made functions, but it is very constrained, comparing to many functionalities a library provide.
GitHub
github.com › langchain-ai › langchain › issues › 26553
Consider changing PythonAstREPLTool default for locals to be None · Issue #26553 · langchain-ai/langchain
September 16, 2024 - code = """ for company in [1]: list(company for _ in range(1)) """ tool = langchain_experimental.tools.PythonAstREPLTool(locals=None) print(tool.invoke({"query": code}))
Author bernardorufino
LangChain
api.python.langchain.com › en › latest › _modules › langchain_experimental › tools › python › tool.html
langchain_experimental.tools.python.tool — 🦜🔗 LangChain 0.2.17
[docs]class PythonAstREPLTool(BaseTool): """Tool for running python code in a REPL.""" name: str = "python_repl_ast" description: str = ( "A Python shell. Use this to execute python commands. " "Input should be a valid python command.
GitHub
github.com › langchain-ai › langchain › issues › 8821
How to pass a CSV file or a dataframe to Pandas REPL tool in Langchain? · Issue #8821 · langchain-ai/langchain
August 6, 2023 - ``` df = pd.read_csv(file_path) tools = [PythonAstREPLTool(locals={"df": df})] agent = initialize_agent( agent='chat-conversational-react-description', tools=tools, llm=llm, verbose=True, max_iterations=3, early_stopping_method='generate', memory=conversational_memory ) query = 'What is the longest name?' print(agent(query))
Author moayadeldin
LiteLLM
aidoczh.com › langchain_api › html › _modules › langchain_experimental › tools › python › tool.html
langchain_experimental.tools.python.tool — 🦜🔗 LangChain 0.2.0
June 20, 2024 - [docs]class PythonAstREPLTool(BaseTool): """用于在REPL中运行Python代码的工具。""" name: str = "python_repl_ast" description: str = ( "A Python shell. Use this to execute python commands. " "Input should be a valid python command.
GitHub
microsoft.github.io › autogen › dev › › _modules › autogen_ext › tools › langchain › _langchain_adapter.html
autogen_ext.tools.langchain._langchain_adapter — AutoGen
.. code-block:: bash pip install -U "autogen-ext[langchain]" Args: langchain_tool (LangChainTool): A LangChain tool to wrap Examples: Use the `PythonAstREPLTool` from the `langchain_experimental` package to create a tool that allows you to interact with a Pandas DataFrame.
GitHub
github.com › langchain-ai › langchain › discussions › 26665
Using Field Descriptions for Enhanced Data Narratives with LLMs - By Using Python REPL Tool · langchain-ai/langchain · Discussion #26665
September 19, 2024 - This setup allows you to use the PythonAstREPLTool to execute code that generates narratives based on field descriptions.
Author langchain-ai
LiteLLM
aidoczh.com › langchain_api › html › tools › langchain_experimental.tools.python.tool.PythonAstREPLTool.html
langchain_experimental.tools.python.tool.PythonAstREPLTool — 🦜🔗 LangChain 0.2.0
langchain_experimental.tools.python.tool.PythonAstREPLTool · class langchain_experimental.tools.python.tool.PythonAstREPLTool[source]¶ · Bases: BaseTool · 用于在REPL中运行Python代码的工具。 · Create a new model by parsing and validating input data from keyword arguments.
GitHub
github.com › langflow-ai › langflow › issues › 1466
Are the tools PythonRPLTool and PythonastREPLTool no longer available in 6.4 ? · Issue #1466 · langflow-ai/langflow
February 26, 2024 - LangFlow 0.5 used to contain two useful components : PythonRPLTool and PythonastREPLTool. Having upgraded to 6.4 these are no longer available on the toolbar and no longer work when imported from J...
Author Arron-Clague