langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.12k stars 15.21k forks source link

React Agent Fails to Recognize "python_repl_ast" Tool #23076

Open zakatlassi2 opened 4 months ago

zakatlassi2 commented 4 months ago

Checked other resources

Example Code



import pandas as pd
from langchain.agents import create_react_agent, AgentExecutor
from langchain.llms import VertexAI
from langchain_experimental.tools.python.tool import PythonAstREPLTool
from langchain.prompts import PromptTemplate
from langchain_google_vertexai import VertexAI

# --- Create a Sample DataFrame ---
df = pd.DataFrame({"Age": [25, 30, 35, 40], "Value": [10, 20, 30, 40]})

# --- Initialize Vertex AI ---
llm = VertexAI(model_name="gemini-pro", temperature=0)

# --- Tool ---
python_tool = PythonAstREPLTool(locals={"df": df})

# --- Chain-of-Thought Prompt Template ---
prompt_template = """Tu es un assistant expert en Pandas.
Tu dois répondre aux questions en utilisant **uniquement** le format suivant pour **chaque étape** de ton raisonnement :
tool_code Thought: [ta réflexion ici] Action: [l'outil que tu veux utiliser] Action Input: [le code à exécuter par l'outil] Observation: [le résultat de l'exécution du code]

Outils : {tool_names} {tools}

**Ces mots-clés ne doivent jamais être traduits ni transformés :**
- Action:
- Thought:
- Action Input:
- Observation:

Voici les colonnes disponibles dans le DataFrame : {df.columns}

Question: {input}
Thought: Pour répondre à cette question, je dois d'abord trouver le nom de la colonne qui contient les âges. 
Action: python_repl_ast
Action Input: print(df.columns)
Observation:
Thought: Maintenant que j'ai la liste des colonnes, je peux utiliser la colonne 'Age' et la fonctionmean()de Pandas pour calculer la moyenne des âges.
Action: python_repl_ast
Action Input: print(df['Age'].mean())
Observation:
python {agent_scratchpad} """

prompt = PromptTemplate(
    input_variables=["input", "agent_scratchpad", "df.columns"], template=prompt_template
)

# --- Create ReAct agent ---
react_agent = create_react_agent(
    llm=llm, tools=[python_tool], prompt=prompt, stop_sequence=False
)

# --- Agent Executor ---
agent_executor = AgentExecutor(
    agent=react_agent,
    tools=[python_tool],
    verbose=True,
    handle_parsing_errors=True,
    max_iterations=5,
)

# --- Main Execution Loop ---
test_questions = ["Calcule la moyenne des âges"]

for question in test_questions:
    print(f"Question: {question}")
    try:
        response = agent_executor.invoke(
            {"input": question, "df": df, "df.columns": df.columns}
        )
        print(f"Answer: {response['output']}")
    except Exception as e:
        print(f"An error occurred: {e}")

### Error Message and Stack Trace (if applicable)

** is not a valid tool, try one of [python_repl_ast].

### Description

I am encountering a persistent issue where the React agent fails to recognize and utilize the "python_repl_ast" tool correctly, despite it being defined in the list of tools.

Steps to Reproduce:

Define a Pandas DataFrame.
Initialize VertexAI from langchain_google_vertexai
Create the python_repl_asttool using PythonAstREPLTool and passing the DataFrame.
Define a prompt that includes instructions for the agent to use python_repl_ast to perform a calculation on the DataFrame (e.g., calculate the mean of a column).
Create the React agent using create_react_agentand passing the tool.
Run the agent with a question related to the DataFrame.
Expected Behavior:

The agent should correctly interpret the "Action" and "Action Input" instructions in the prompt, execute the Python code using python_repl_ast and return the result in the "Observation" section.

Actual Behavior:

The agent repeatedly returns the error message "** python_repl_ast

** is not a valid tool, try one of [python_repl_ast]."

### System Info

Windows, vs code, python 3.10, langchain 0.2.2 
AjitKP91 commented 2 months ago

Is there any resolution?