langchain-ai / langchain

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

How to enable the memory mechanism when using create_pandas_dataframe_agent? #7625

Closed ykemiche closed 9 months ago

ykemiche commented 1 year ago

System Info

LangChain version : 0.0.216 Python version : 3.11.4 System: Windows

Who can help?

@hwchase17 @eyu

Information

Related Components

Reproduction

I want to create a chatbot to retrieve information from my own csv file in response to a query using google PaLM model, I want to improve the model's capabilities to perform specific data retrieval requests from the CSV. Here are a few examples of queries I would like the chatbot to handle:

a-Calculate the average of data from a specific column in the CSV file. b-Return the top 10 scores based on a column of grades as a dataframe(the output of llm should be a json format). c-Track the evolution of a product over time by analyzing a date column.

I have 2 questions :

1- what should I change in the following code to maintain contextual memory during the conversation(by changing question)?

from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import VertexAI

ChatModel = VertexAI(
      model_name="text-bison@001",
      max_output_tokens=1024,
      temperature=0.1,
      top_p=0.8,
      top_k=40,
      verbose=True,
  )

pd_agent = create_pandas_dataframe_agent(ChatModel, 
                                                  df, 
                                                  verbose=True,
                                                  max_iterations=6,)

#prompt=...
#question=...
response = pd_agent.run(prompt + question)

2-

I'm looking for efficient ways to handle different types of tasks in my chatbot. Some questions require DataFrame responses, others need text responses, and some require both. Can I create specialized agents to handle specific tasks separately instead of specifying everything in one prompt?

Expected behavior

1-A chatbot that maintains contextual memory during the conversation using create_pandas_dataframe_agent 2-A suggestion of jobs separation to optimize the output of the chain

mswoff commented 1 year ago

1- To maintain contextual memory during the conversation, you can modify the clear method in the create_pandas_dataframe_agent class. This method is responsible for clearing the memory contents. You can adjust it to keep the context of the conversation.

2- You can create specialized agents to handle specific tasks separately. This can be done by creating different chains for different types of tasks. For example, you can have one chain for tasks that require DataFrame responses, another for tasks that need text responses, and another for tasks that require both. This way, you can optimize the output of the chain by only using the necessary resources for each task.

Let me know if I missed the mark on what you're looking for.

ykemiche commented 1 year ago

@mswoff 1-I found the create_pandas_dataframe_agent method but not the class, where is the clear method?

def create_pandas_dataframe_agent(
    llm: BaseLanguageModel,
    df: Any,
    agent_type: AgentType = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    callback_manager: Optional[BaseCallbackManager] = None,
    prefix: Optional[str] = None,
    suffix: Optional[str] = None,
    input_variables: Optional[List[str]] = None,
    verbose: bool = False,
    return_intermediate_steps: bool = False,
    max_iterations: Optional[int] = 15,
    max_execution_time: Optional[float] = None,
    early_stopping_method: str = "force",
    agent_executor_kwargs: Optional[Dict[str, Any]] = None,
    include_df_in_prompt: Optional[bool] = True,
    **kwargs: Dict[str, Any],
) -> AgentExecutor:
    """Construct a pandas agent from an LLM and dataframe."""
    agent: BaseSingleActionAgent
    if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION:
        prompt, tools = _get_prompt_and_tools(
            df,
            prefix=prefix,
            suffix=suffix,
            input_variables=input_variables,
            include_df_in_prompt=include_df_in_prompt,
        )
        llm_chain = LLMChain(
            llm=llm,
            prompt=prompt,
            callback_manager=callback_manager,
        )
        tool_names = [tool.name for tool in tools]
        agent = ZeroShotAgent(
            llm_chain=llm_chain,
            allowed_tools=tool_names,
            callback_manager=callback_manager,
            **kwargs,
        )
    elif agent_type == AgentType.OPENAI_FUNCTIONS:
        _prompt, tools = _get_functions_prompt_and_tools(
            df,
            prefix=prefix,
            suffix=suffix,
            input_variables=input_variables,
            include_df_in_prompt=include_df_in_prompt,
        )
        agent = OpenAIFunctionsAgent(
            llm=llm,
            prompt=_prompt,
            tools=tools,
            callback_manager=callback_manager,
            **kwargs,
        )
    else:
        raise ValueError(f"Agent type {agent_type} not supported at the moment.")
    return AgentExecutor.from_agent_and_tools(
        agent=agent,
        tools=tools,
        callback_manager=callback_manager,
        verbose=verbose,
        return_intermediate_steps=return_intermediate_steps,
        max_iterations=max_iterations,
        max_execution_time=max_execution_time,
        early_stopping_method=early_stopping_method,
        **(agent_executor_kwargs or {}),
    )

2- OK so I should create separate prompts as follow :

pd_agent = create_pandas_dataframe_agent(ChatModel, df, verbose=True, max_iterations=6,)

prompt_a=...

prompt_a=...

prompt_c=...

how to decide to which prompt to switch to when a user ask a question (in order to run response = pd_agent.run(prompt + question) ?

dosubot[bot] commented 9 months ago

Hi, @ykemiche! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, the issue is about enabling the memory mechanism when using the create_pandas_dataframe_agent in the LangChain library. You had two questions: how to maintain contextual memory during the conversation and if it's possible to create specialized agents to handle specific tasks separately instead of specifying everything in one prompt. User "mswoff" suggested modifying the clear method in the create_pandas_dataframe_agent class to maintain contextual memory and creating different chains for different types of tasks.

Before we close this issue, we wanted to check if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!