[Python] How to persist LangChain conversation memory (save and load)?

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm creating a conversation like so:

    llm = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name=OPENAI_DEFAULT_MODEL)
    conversation = ConversationChain(llm=llm, memory=ConversationBufferMemory())


    But what I really want is to be able to save and load that ConversationBufferMemory() so that it's persistent between sessions. There doesn't seem to be any obvious tutorials for this but I noticed "Pydantic" so I tried to do this:

    saved_dict = conversation.memory.chat_memory.dict()
    cm = ChatMessageHistory(**saved_dict) # or cm = ChatMessageHistory.parse_obj(saved_dict)


    But this fails:

    ValidationError: 6 validation errors for ChatMessageHistory
    messages -> 0
    Can't instantiate abstract class BaseMessage with abstract method type (type=type_error)


    Thoughts? I'd love links to any sort of guide, repo, reference, etc.

    Continue reading...

Compartilhe esta Página