惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 木乃伊人

VS Code 搭建LangChain开发环境 VSCode Unable to import XXX 问题解决记录 try-catch中的throw和throw ex的区别 TDD Google Chrome 默认非安全端口列表 EF Core的预先加载、延迟加载、实体追踪 同步、异步、回调 软件设计原则 UML类图 Seq Serilog 多线程整理 Vuex和Pinia Vue3+TS+Vite+pinia 枚举 Vite创建Vue3项目 闭包 IdentitySrever4 ElasticSearch 前端防止重复提交案例
大模型应用开发-聊天机器人-保存聊天记录
木乃伊人 · 2026-02-04 · via 博客园 - 木乃伊人

一、简介

在聊天机器人应用中,如果我们要保存聊天记录,不光可以让机器人与用户进行多轮对话,还可以根据聊天记录进行检索。

LangChain中,提供了一个BaseChatMessageHistory父类,专门用于保存聊天记录。默认情况下,LangChain只提供一个基于内存的InMemoryChatMessageHistory来保存聊天记录。 

image

        这种通过内存管理消息记录的方式是不太安全的,应用一旦重启,消息记录就会丢失。故而,LangChain还提供了很多机遇其他存储的扩展依赖。比如可以用MongoDB,PostgreSQL,Mysql,Redis等数据库来保存聊天记录。

具体参考官网:https://python.langchain.com/docs/integration/memory/

以Redis为例,将上面案例中的消息记录保存到Redis中,要做的,只是替换一下history的实现类。

【注意】

Redis也是有很多版本的,常用的社区版本Redis Community Edition版本(即社区版本),只支持做键值对存储,不适用于存储对话,需要再安装一个Redis Stack。

image

 二、使用Redis保存对话

首先安装langchain针对redis的扩展依赖。

image

 然后,替换history的实现。此处部署Redis拂去,过程略过。

image

 运行完成后,就可以再Redis中查询聊天记录。

image