langchain icon indicating copy to clipboard operation
langchain copied to clipboard

ConversationBufferWindowMemory returns empty string on empty history instead of empty array.

Open Joseph-Cardwell opened this issue 11 months ago • 3 comments

System Info

Python 3.10 Langchain 0.0.226

Windows 11

Who can help?

No response

Information

  • [ ] The official example notebooks/scripts
  • [X] My own modified scripts

Related Components

  • [ ] LLMs/Chat Models
  • [ ] Embedding Models
  • [ ] Prompts / Prompt Templates / Prompt Selectors
  • [ ] Output Parsers
  • [ ] Document Loaders
  • [ ] Vector Stores / Retrievers
  • [X] Memory
  • [X] Agents / Agent Executors
  • [ ] Tools / Toolkits
  • [ ] Chains
  • [ ] Callbacks/Tracing
  • [ ] Async

Reproduction

import os, langchain

os.environ['SERPAPI_API_KEY'] = ""
os.environ['OPENAI_API_KEY'] = ""

from langchain.chains import LLMChain
from langchain.agents import ConversationalChatAgent, SelfAskWithSearchChain, AgentExecutor
from langchain.memory import ConversationBufferWindowMemory
from langchain.tools import Tool
from langchain.llms import OpenAI

conversation_buffer_window_memory: ConversationBufferWindowMemory = ConversationBufferWindowMemory(
    input_key="input", memory_key="chat_history")

search = langchain.SerpAPIChain()
self_ask_and_search = SelfAskWithSearchChain(llm=OpenAI(), search_chain=search, verbose=True)

tools = [
    Tool(
        name="Search",
        func=self_ask_and_search.run,
        description="useful for when you need to answer questions about current events",
    )
]

prompt = ConversationalChatAgent.create_prompt(
    tools, input_variables=["input", "chat_history", "agent_scratchpad"]
)

llm_chain = LLMChain(
    llm=OpenAI(temperature=0), prompt=prompt)

agent = ConversationalChatAgent(
    llm_chain=llm_chain, tools=tools, verbose=True)

agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, verbose=True, memory=conversation_buffer_window_memory)
  
agent_executor.run('what is the capital of texas?')

returns the following error: (<class 'ValueError'>, ValueError('variable chat_history should be a list of base messages, got '), <traceback object at 0x0000027BA6366E40>)

Expected behavior

Return the LLM result while updating the memory mechanism.

Joseph-Cardwell avatar Jul 07 '23 17:07 Joseph-Cardwell

The problem appears in the file: langchain/memory/buffer_window.py

It appears to be caused in the following function: langchain.memory.buffer_window.ConversationBufferWindowMemory.load_memory_variables

In load_memory_variables() it makes a call to get_buffer_string() in langchain/schema/messages.py which converts the empty array of messages to an empty string, which then throws the error in langchain/prompts/chat.py when it tests for memory to be an instance of List.

Joseph-Cardwell avatar Jul 07 '23 18:07 Joseph-Cardwell

Answer generated by a 🤖

Answer

I understand that you're encountering an issue with the ConversationBufferWindowMemory class in LangChain. When the history is empty, it seems to be returning an empty string instead of an empty list, which is causing a ValueError in your code.

To address this issue, I suggest modifying the load_memory_variables method in the ConversationBufferWindowMemory class. This method should return an empty list when the history buffer is empty, rather than an empty string. Here's how you can adjust the method:

def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
    """Return history buffer."""

    buffer: Any = self.buffer[-self.k * 2 :] if self.k > 0 else []
    if not buffer:
        return {self.memory_key: []}
    if not self.return_messages:
        buffer = get_buffer_string(
            buffer,
            human_prefix=self.human_prefix,
            ai_prefix=self.ai_prefix,
        )
    return {self.memory_key: buffer}

In this adjusted code, we first check if the buffer is empty. If it is, we return an empty list. If it's not, we proceed as before. This should prevent the ValueError you're encountering when the history is empty.

Please note that this is one potential solution based on the information provided. If you're using other classes like ConversationBufferMemory or ConversationStringBufferMemory, similar adjustments might be necessary in their load_memory_variables methods.

If this solution works for you and you believe this is a bug that could impact other users, I encourage you to make a pull request with this change. If the issue persists or this solution doesn't align with your experience, please ensure you're using the latest version of LangChain, as updates often contain bug fixes and improvements.

Sources


This response is meant to be useful, save you time, and share context. 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.

dosubot[bot] avatar Jul 07 '23 19:07 dosubot[bot]

This would likely break compatibility. If the API is going to make this alteration then an automatic adapter must be employed. Either way, it is a larger consideration.

Joseph-Cardwell avatar Jul 07 '23 23:07 Joseph-Cardwell

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

From what I understand, the issue is related to the ConversationBufferWindowMemory class in the Langchain library. Currently, when the chat history is empty, the class returns an empty string instead of an empty array. I have suggested a potential solution to modify the load_memory_variables method in the ConversationBufferWindowMemory class to return an empty list when the history buffer is empty. However, you raised concerns about compatibility and the need for an automatic adapter.

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

Thank you for your understanding and contribution to the LangChain project. Let us know if you have any further questions or concerns.

dosubot[bot] avatar Oct 06 '23 16:10 dosubot[bot]

This is still relevant as of 0.345

erlandsen-tech avatar Dec 05 '23 11:12 erlandsen-tech

@hwchase17, @efriis @baskaryan did you didn't fix it intentionally?

umair313 avatar Dec 05 '23 11:12 umair313

        memory.chat_memory.add_user_message("Hi there!")
        memory.chat_memory.add_ai_message("hello :)")

I could make it work by first adding messages to the memory for initialization.

erlandsen-tech avatar Dec 05 '23 12:12 erlandsen-tech