题意:如何持久化 LangChain 对话记忆(保存和加载)
问题背景:
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:
但我真正想要的是能够保存和加载 ConversationBufferMemory()
,以便它在会话之间保持持久性。似乎没有关于这方面的明显教程,但我注意到了“Pydantic”,所以我尝试这样做:
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 -> 0Can'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:
我刚刚做了一些类似的事情,希望能有所帮助。在高层次上:
1. use ConversationBufferMemory
as the memory to pass to the Chain initialization
使用 ConversationBufferMemory
作为内存传递给链(Chain)的初始化
llm = 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')
2. extract messages from memory in the form of List[langchain.schema.HumanMessage|AIMessage]
(not serializable)
从内存中提取消息,以 List[langchain.schema.HumanMessage | AIMessage]
的形式(不可序列化)
extracted_messages = original_chain.memory.chat_memory.messages
3. transform the extracted message to serializable native Python objects
将提取的消息转换为可序列化的原生Python对象
ingest_to_db = messages_to_dict(extracted_messages)
4. perform db operations to write to and read from database of your choice, I'll just use json.dumps
and json.loads
to illustrate
执行数据库操作以写入和读取您选择的数据库,为了说明,我将只使用 json.dumps
和 json.loads
retrieve_from_db = json.loads(json.dumps(ingest_to_db))
5. transform the retrieved serialized object back to List[langchain.schema.HumanMessage|AIMessage]
将检索到的序列化对象转换回 List[langchain.schema.HumanMessage | AIMessage]
的形式
retrieved_messages = messages_from_dict(retrieve_from_db)
6. construct ChatMessageHistory
from the messages
从消息中构建 ChatMessageHistory
retrieved_chat_history = ChatMessageHistory(messages=retrieved_messages)
7. construct ConversationBufferMemory
from ChatMessageHistory
从 ChatMessageHistory
中构建 ConversationBufferMemory
retrieved_memory = ConversationBufferMemory(chat_memory=retrieved_chat_history)
8. pass memory back to the newly initiated Chain
将内存(memory
)传递回新初始化的链(Chain
)
reloaded_chain = ConversationChain(llm=llm,verbose=True,memory=retrieved_memory
)