代码拉取完成,页面将自动刷新
import pickle
from langchain.chains.conversation.base import ConversationChain
from langchain_core.messages import messages_to_dict, messages_from_dict
from ChatGLM_new import xinghuo_llm
from langchain.memory import ChatMessageHistory, ConversationBufferMemory, ConversationSummaryMemory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")
print(history.messages)
print("##################对话缓冲区内存(ConversationBufferMemory)#######################")
conversation = ConversationChain(
llm=xinghuo_llm,
verbose=True,
memory=ConversationBufferMemory()
)
# conversation.predict(input="I'm doing well!")
# conversation.predict(input="你是谁")
# conversation.predict(input="你的背后原理是什么")
#
# print(conversation.memory.chat_memory.messages)
print("##################保存历史消息#######################")
# dicts = messages_to_dict(conversation.memory.chat_memory.messages)
# print(dicts)
#
# f = open("./memory", 'wb')
# pickle.dump(dicts,f)
# f.close()
print("##################读取历史消息#######################")
# 读取
# dicts_load = pickle.load(open("./memory", "rb"))
#
# new_messages = messages_from_dict(dicts_load)
# retrieved_chat_history = ChatMessageHistory(messages=new_messages)
# retrieved_memory = ConversationBufferMemory(chat_memory=retrieved_chat_history)
# conversation_reload = ConversationChain(
# llm=xinghuo_llm,
# verbose=True,
# memory=retrieved_memory
# )
# conversation_reload.predict(input="我回来了")
print("##################会话摘要(ConversationSummaryMemory)#######################")
# conversation_with_summary = ConversationChain(
# llm=xinghuo_llm,
# memory=ConversationSummaryMemory(llm=xinghuo_llm),
# verbose=True
# )
# print(conversation_with_summary.predict(input="嗨,怎么了?"))
# print(conversation_with_summary.predict(input="告诉我更多关于它的信息!"))
# print(conversation_with_summary.predict(input="很酷 -- 项目的范围是什么?"))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。