You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
llm/04-多模态机器人案例/01-实现消息上下文记录保存.py

66 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from env_util import DASHSCOPE_API_KEY, DASHSCOPE_BASE_URL
# 0、llm~~
llm = ChatOpenAI(
model = "qwen-plus",
base_url=DASHSCOPE_BASE_URL,
api_key=DASHSCOPE_API_KEY,
temperature=0.8,
);
# ===============================================================================================
# 1、定义专门做聊天的提示词模板
prompt = ChatPromptTemplate.from_messages([
('system', '你是一个乐于助人的助手。尽你所能回答所有问题。提供的聊天历史包含与你对话用户的相关信息。'), # 系统提示词
MessagesPlaceholder(variable_name='chat_history', optional=True), #消息占位符
('human', '{input}') #用户提示词input用户传的问题
])
chain = prompt | llm;
# ===============================================================================================
# 2、存储聊天记录 存的谁存的第10行的内容存到哪里内存、关系型数据库或者redis数据库
# 存到内存中,存到字典结构中,存历史记录要根据用户来存 store存储所有会话的所有历史记录
# 所以key : 就是会话ID session_id ,一个会话存一份
store = {}
# 提供工厂函数告诉大模型返回聊天记录传session_id拿到这个会话的历史聊天信息
def get_session_history(session_id: str):
"""从内存中的历史消息列表中 返回当前会话 的所有历史消息"""
# 看session_id在不在字典中
if session_id not in store:
# InMemoryChatMessageHistory 存在内存中的一个历史聊天记录某一个会话的历史聊天记录的列表是langchain提供的
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
# langchain所有消息类型 SystemMessage 系统提示词, HumanMessage 用户提示词, AIMessage ai模型响应回复的消息, ToolMessage 由工具返回的消息
# ===============================================================================================
# 3、创建带历史记录功能的处理链帮我自动存储历史记录
chain_with_message_history = RunnableWithMessageHistory(
chain, # 基础执行链
get_session_history, # 指定工厂函数返回指定session_id的聊天记录
input_messages_key='input', # 指定用户输入的消息的key
history_messages_key='chat_history', # 历史消息记录的key
)
# ===============================================================================================
#4、 测试
result1 = chain_with_message_history.invoke({'input': '你好,我是郑金维!'}, config={"configurable": {"session_id": "user123"}})
print(result1)
result2 = chain_with_message_history.invoke({'input': '我的名字叫什么?'}, config={"configurable": {"session_id": "user123"}})
print(result2)
result3 = chain_with_message_history.invoke({'input': '历史上,和我同名的人有哪些?'}, config={"configurable": {"session_id": "user123"}})
print(result3)