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.
I just did something similar, hopefully this will be helpful. On a high level:
ConversationBufferMemory as the memory to pass to the Chain initializationllm = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo-0301')
original_chain = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)
original_chain.run('what do you know about Python in less than 10 words')
List[langchain.schema.HumanMessage|AIMessage] (not serializable)extracted_messages = original_chain.memory.chat_memory.messages
ingest_to_db = messages_to_dict(extracted_messages)
json.dumps and json.loads to illustrateretrieve_from_db = json.loads(json.dumps(ingest_to_db))
List[langchain.schema.HumanMessage|AIMessage]retrieved_messages = messages_from_dict(retrieve_from_db)
ChatMessageHistory from the messagesretrieved_chat_history = ChatMessageHistory(messages=retrieved_messages)
ConversationBufferMemory from ChatMessageHistoryretrieved_memory = ConversationBufferMemory(chat_memory=retrieved_chat_history)
reloaded_chain = ConversationChain(
llm=llm,
verbose=True,
memory=retrieved_memory
)
You can find the full code snippet at this GitHub Link
Rather than mess around too much with LangChain/Pydantic serialization issues, I decided to just use Pickle the whole thing and that worked fine:
pickled_str = pickle.dumps(conversation.memory)
conversation2 = ConversationChain(llm=llm, memory=pickle.loads(pickled_str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With