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-多模态机器人案例/02-关系型数据库保存聊天上下文.py

65 lines
2.7 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_community.chat_message_histories import SQLChatMessageHistory
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 = {}
def get_session_history(session_id: str):
"""从关系型数据库的历史消息列表中 返回当前会话 的所有历史消息"""
# SQLChatMessageHistory是langchain提供的
return SQLChatMessageHistory(
session_id=session_id,
# 这里url换为自己的数据库即可
connection_string='sqlite:///chat_history.db',
)
# ===============================================================================================
# 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)