langchain React Agent无法识别"python_repl_ast"工具

t5fffqht  于 24天前  发布在  React
关注(0)|答案(1)|浏览(18)

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我使用集成搜索在LangChain文档中进行了搜索。
  • 我使用GitHub搜索查找了一个类似的问题,但没有找到。
  • 我确信这是LangChain中的一个错误,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此错误。

示例代码

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

相关问题