langchain-ai / langchain

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

NameError: name 'pd' is not defined / #25680

Open sakotham opened 3 weeks ago

sakotham commented 3 weeks ago

Checked other resources

Example Code

from langchain_experimental.agents import create_pandas_dataframe_agent
from langchain_openai import AzureChatOpenAI 
import pandas as pd
import openai

 openai.api_version = "2024-04-01-preview"
 gpt4_turbo = AzureChatOpenAI(deployment_name=CHATGPT_4TURBO_128k,
                                          azure_endpoint = openai.api_base,
                                          api_key=auth_token,
                                          temperature = 0.1,
                                          api_version=openai.api_version,
                                          top_p=0.1,
                                          model_kwargs=dict(
                                              user=f'{{"appkey": "{APP_KEY}"}}',
                                        )
                           )

pandas_agent = create_pandas_dataframe_agent(
                llm=self._gpt4_turbo,
                df=df,
                allow_dangerous_code=True,
                agent_type="openai-tools",
                verbose=True,
            )

memory = []
memory.append(("System", prompt))

 memory.append(("human", query))
  res = pandas_agent.invoke(memory)
  memory.append(("assistant", res['output']))
  _response = res['output']

Error Message and Stack Trace (if applicable)

NameError: name 'pd' is not defined
Invoking: 'python_repl_ast' with '{'query': 'import pandas as}

"Error code: 400 - {'error': {'message': \"An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_CD2Eqr0B3X7T2fG6spWxSAIp\", 'type': 'invalid_request_error', 'param': 'messages', 'code': None}, 'user': '{\"appkey\": \”MY_APPKEY\”, \"session_id\": \”MY_ID\”, \"user\": \"\", \"prompt_truncate\": \"yes\"}'}"

Description

I am using LangChain, create_pandas_dataframe_agent. intermittently I am getting the error in response tool_calls' must be followed by tool messages. But in most of the scenario this is my first question and there is no previous conversation. When I receive this tool_calls error in logs I can see NameError: name 'pd' is not defined Invoking: 'python_repl_ast' with '{'query': 'import pandas as}. All my lib are up-to date

System Info

langchain==0.2.12 langchain-community==0.2.11 langchain-core==0.2.28 langchain-experimental==0.0.64 langchain-openai==0.1.20 langchain-text-splitters==0.2.2 langsmith==0.1.98 openai==1.40.0 pandas==2.2.2

baskaryan commented 1 week ago

so the pandas agent doesn't take in messages as input, it takes a single input string. can you try

pandas_agent.invoke({"input": query})

and see if that works?

if you want to specify the system prompt, you can do so by updating either the default system prompt via prefix:

pandas_agent = create_pandas_dataframe_agent(
                llm=self._gpt4_turbo,
                df=df,
                allow_dangerous_code=True,
                agent_type="openai-tools",
                verbose=True,
                prefix="You are working with a Pandas DataFrame in Python. The name of the dataframe is `df`.\n\n" + prompt
)
sakotham commented 1 week ago

Hi @baskaryan, Without Prompt part in the memory, Can I ask questions in the above format, I have implemented this memory part for the followUp questions (Bot needs to response based on the previous conversations). If this method/way is wrong is there any way to implement memory for Pandas Agent? Thank you...