Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erros with langchain CSV agent and Pandas agent #17448

Closed
4 tasks done
Khawlao opened this issue Feb 13, 2024 · 7 comments
Closed
4 tasks done

Erros with langchain CSV agent and Pandas agent #17448

Khawlao opened this issue Feb 13, 2024 · 7 comments
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations

Comments

@Khawlao
Copy link

Khawlao commented Feb 13, 2024

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 didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.

Example Code

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits.csv.base import create_csv_agent
from langchain_openai import OpenAI
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent

agent = create_csv_agent(
llm ,
'train.csv',
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)
agent.run("how many rows are there?")¨
f=pd.read_csv('train.csv',delimiter=';',encoding='Latin-1')
print(df.head())
agent = create_pandas_dataframe_agent(llm, df, agent_type="openai-tools", verbose=True)
agent.invoke(
{
"input": "What's the correlation between age and fare? is that greater than the correlation between fare and survival?"
}
)
**## Output ##
return func(*args, kwargs)
^^^^^^^^^^^^^^^^^^^^^
TypeError: Completions.create() got an unexpected keyword argument 'tools'

and when changing the LLM or agent type i get this error: in create_react_agent
raise ValueError(f"Prompt missing required variables: {missing_vars}")
ValueError: Prompt missing required variables: {'tools', 'tool_names'}

Error Message and Stack Trace (if applicable)

line 26, in
agent = create_csv_agent(llm ,'train.csv',agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\LocalCache\local-packages\Python311\site-packages\langchain_experimental\agents\agent_toolkits\csv\base.py", line 66, in create_csv_agent
return create_pandas_dataframe_agent(llm, df, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_experimental\agents\agent_toolkits\pandas\base.py", line 264, in create_pandas_dataframe_agent
runnable=create_react_agent(llm, tools, prompt), # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain\agents\react\agent.py", line 97, in create_react_agent
raise ValueError(f"Prompt missing required variables: {missing_vars}")
ValueError: Prompt missing required variables: {'tool_names', 'tools'}

Description

I am trying to use the CSV agent to query my CSV but I keep getting the error: TypeError: Completions.create() got an unexpected keyword argument 'tools' ( for agent type = openai tools) and I tried a different agent type and I am having this error ValueError: Prompt missing required variables: {'tools', 'tool_names'}
(i followed the example from the documentation)
if anyone has an idea how to fix this or encountered this issue before please reach out!

System Info

OS: Windows
OS Version: 10.0.19045
Python Version: 3.11.8 (tags/v3.11.8:db85d51, Feb 6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)]

Copy link

dosubot bot commented Feb 13, 2024

🤖

Hey @Khawlao, great to see you diving into the depths of LangChain again! Running into a bit of a snag, I see? Let's unravel this thread together. 🧵

Based on the context provided, it seems like the issue you're encountering is related to the agent type you're using. The valid agent type that can be used with the create_csv_agent and create_pandas_dataframe_agent functions in the LangChain codebase is OpenAIFunctionsAgent.

Here is an example of how to create an instance of OpenAIFunctionsAgent:

from langchain_openai import OpenAIFunctionsAgent, ChatOpenAI

agent = OpenAIFunctionsAgent(
    llm=ChatOpenAI(temperature=0, model="gpt-4"), prompt=prompt, tools=tools
)

You can find this in the templates/csv-agent/csv_agent/agent.py file in the LangChain repository.

Regarding the Completions.create() method, I wasn't able to find specific information about its expected arguments within the LangChain repository. It's possible that the error message you're seeing is due to a mismatch between the arguments you're providing and the ones expected by the method.

If you're still encountering issues, could you please provide more details about the context in which you're calling Completions.create()? This could help in identifying the root cause of the problem.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations labels Feb 13, 2024
@Khawlao
Copy link
Author

Khawlao commented Feb 13, 2024

@dosubot the openAI functions agent doesnt work : ImportError: cannot import name 'OpenAIFunctionsAgent' from 'langchain_openai'. I am not calling completions.create() in my code.

Copy link

dosubot bot commented Feb 13, 2024

🤖

Based on the error messages you provided, it seems like there might be a mismatch between the agent_type you're using and the expected input parameters for the create_react_agent function.

The create_react_agent function expects a tools and tool_names parameter, which are not being provided in your current setup. This is likely the cause of the ValueError: Prompt missing required variables: {'tools', 'tool_names'} error.

Here's the relevant part of the create_react_agent function:

def create_react_agent(
    llm: BaseLanguageModel,
    tools: Sequence[BaseTool],
    prompt: Optional[str] = None,
    agent_type: Optional[AgentType] = None,
    **kwargs: Any,
) -> ReactAgent:
    ...
    missing_vars = set()
    if "tools" not in kwargs:
        missing_vars.add("tools")
    if "tool_names" not in kwargs:
        missing_vars.add("tool_names")
    if missing_vars:
        raise ValueError(f"Prompt missing required variables: {missing_vars}")
    ...

As for the TypeError: Completions.create() got an unexpected keyword argument 'tools' error, it seems like the Completions.create() function does not accept a tools argument. This might be due to a version mismatch or an incorrect usage of the function.

Regarding the ImportError: cannot import name 'OpenAIFunctionsAgent' from 'langchain_openai', it's possible that the OpenAIFunctionsAgent class does not exist in the langchain_openai module. Please ensure that the class is correctly defined and exported in the module.

If you're still encountering these issues, could you please provide more information about how you're setting up and using the create_csv_agent and create_pandas_dataframe_agent functions? This will help in identifying the root cause of the problem.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@Khawlao
Copy link
Author

Khawlao commented Feb 13, 2024

@dosubot I am using the create_csv_agent : agent = create_csv_agent(
llm ,
'train.csv',
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)
and I use the same example as in [(https://python.langchain.com/docs/integrations/toolkits/csv) ]and I keep having the error ValueError: Prompt missing required variables: {'tools', 'tool_names'}

Copy link

dosubot bot commented Feb 13, 2024

🤖

Based on the error messages you're encountering, it seems like there might be a mismatch between the agent type you're trying to use and the language model or the prompt you're using.

The error TypeError: Completions.create() got an unexpected keyword argument 'tools' suggests that the Completions.create() method of the language model you're using doesn't accept a tools argument. This is because the tools argument is specific to the create_openai_tools_agent function in the LangChain framework, and not a general argument for language models.

The error ValueError: Prompt missing required variables: {'tools', 'tool_names'} indicates that the prompt you're using with the create_openai_tools_agent function doesn't have the required tools and tool_names variables. According to the LangChain documentation, the prompt must have an agent_scratchpad key that is a MessagesPlaceholder. The agent_scratchpad is where intermediate agent actions and tool output messages will be passed in.

Here's an example of how to create a compatible prompt:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant"),
        MessagesPlaceholder("chat_history", optional=True),
        ("human", "{input}"),
        MessagesPlaceholder("agent_scratchpad"),
    ]
)

In this example, the agent_scratchpad key is included as a MessagesPlaceholder in the prompt. This should prevent the ValueError from occurring.

As for the create_csv_agent function, it is used to create a pandas dataframe agent by loading a CSV file into a dataframe. The function first checks if the pandas package is installed. If not, it raises an ImportError. Then, it checks the type of the path parameter. If it's a string or a file-like object, it reads the CSV file into a dataframe using pd.read_csv(). If path is a list, it iterates over the list and reads each CSV file into a dataframe, appending it to a list of dataframes. If the type of path is neither a string, a file-like object, nor a list, it raises a ValueError.

Finally, it calls the create_pandas_dataframe_agent() function with the language model, the dataframe(s), and any additional kwargs, and returns the resulting AgentExecutor.

Please ensure that the language model you're using is compatible with the create_openai_tools_agent function, and that the prompt you're using includes the required agent_scratchpad key. If you're still encountering issues, could you please provide more information about the language model and the prompt you're using? This will help in diagnosing the problem.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@Khawlao
Copy link
Author

Khawlao commented Feb 13, 2024

@dosubot I resolved the issue, it's in the base code of the create_csv_agent, agent.py, I had to remove the tools as the input for create_react_agent function.

Copy link

dosubot bot commented Feb 13, 2024

🤖

I'm glad to hear that you were able to resolve the issue with the create_csv_agent function. It seems like there might be a discrepancy between the version of LangChain you're using and the version used in the documentation.

The error TypeError: Completions.create() got an unexpected keyword argument 'tools' suggests that the Completions.create() method in your version of LangChain does not accept a tools argument. This could be due to a change in the API of the Completions class in a recent version of LangChain.

The error ValueError: Prompt missing required variables: {'tools', 'tool_names'} suggests that the create_react_agent function in your version of LangChain expects a tools and tool_names argument, but these were not provided. This could be due to a change in the API of the create_react_agent function in a recent version of LangChain.

It's always a good idea to ensure that you're using the latest version of LangChain. You can update LangChain by running pip install --upgrade langchain in your terminal.

Regarding the ImportError: cannot import name 'OpenAIFunctionsAgent' from 'langchain_openai', it seems like the OpenAIFunctionsAgent class does not exist in your version of the langchain_openai module. This could be due to a change in the API of the langchain_openai module in a recent version of LangChain.

If you continue to encounter issues, I recommend checking the LangChain GitHub repository for any recent changes that might affect your code. You can also check the LangChain documentation for any updates or changes to the API.

I hope this helps! If you have any other questions or need further clarification, please don't hesitate to ask.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label May 14, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations
Projects
None yet
Development

No branches or pull requests

1 participant