1

I've edited the question as going round in circles trying to fix the original error. Here's the latest:

Current error

pydantic.main.BaseModel.__init__\npydantic.error_wrappers.ValidationError: 1 validation error for ConversationBufferMemory2\nchat\n  field required (type=value_error.missing)\n', 'args': '[]', 'kwargs':

llm.py

    chat = Chat.objects.get(id=chat_id)
    memory = ConversationBufferMemory2()
    memory.set_chat(chat)

models2.py

from langchain.memory.buffer import ConversationBufferMemory
class ConversationBufferMemory2(ConversationBufferMemory):
    """Buffer for storing conversation memory."""

    chat:Chat

    #def __init__(self,*args, **kwargs):
    #    super().__init__(*args, **kwargs)
        
    def set_chat(self,chat:Chat):
        self.chat=chat
        
    @property
    def buffer(self) -> Any:
        """String buffer of memory. Each message is separated by a newline."""
        if self.return_messages:
            return self.chat.messages  # Think this is the right return type ? https://python.langchain.com/docs/modules/memory/
        else:
            return get_buffer_string(
                self.chat.messages,
                human_prefix=self.human_prefix,
                ai_prefix=self.ai_prefix,
            )
ValidationError: 

File "/code/apps/llm_module/llm.py", line 183, in get_response\n    memory = ConversationBufferMemory2()\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/usr/local/lib/python3.11/site-packages/langchain/load/serializable.py", line 74, in __init__\n    super().__init__(**kwargs)\n  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__\npydantic.error_wrappers.ValidationError: 1 validation error for ConversationBufferMemory2\nchat\n  field required (type=value_error.missing)\n', 'args': '[]', 'kwargs':
Reddspark
  • 6,934
  • 9
  • 47
  • 64
  • Looks like `ConversationBufferMemory` uses inspection (in `__init__`, with a metaclass, or otherwise) to notice that it's supposed to have an attribute `chat`, but doesn't. How to fix that depends on what `ConversationBufferMemory` is and expects, but possibly just setting `chat` to some dummy value in `__init__` will do the trick – Brian61354270 Jun 27 '23 at 18:27
  • I've had to update the question. Been chasing the original error all day and the above is currently where I am. – Reddspark Jun 27 '23 at 20:24
  • Ok -- i may have solved. will write up what I know below, – Reddspark Jun 27 '23 at 20:37

1 Answers1

0

Ok it looks like I got past this error and moved on to another error. I do not believe the two are related so I will write this one up to help others.

I replaced chat:Chat with chat:Chat=[] so that there was a default value assigned to the chat variable and Pydantic would stop complaining.

I also couldn't just pass in the chat variable during initialisation as it seemed to cause untold amount of grief in relation to the inherticance of ConversationBufferMemory that is happening.

Reddspark
  • 6,934
  • 9
  • 47
  • 64