96SEO 2026-09-08 04:40 42
说起来,
上一篇我们已经搞清楚了一件事:

最简单的实现是:
const history = new InMemoryChatMessageHistory;按理说,
至于每轮对话。
await history.addMessage;const messages = await history.getMessages;按理说,const response = await model.invoke;await history.addMessage;
只要程序还在运行,这套逻辑完全没有问题。
如果我们现在把 Node.js 程序停掉。再重新运行,会发生什么,
Memory 没了。
JavaScript 程序运行
↓内存中存在 history
↓程序结束
↓进程内存被释放
↓history 消失
例如使用者已经聊了两轮:
Human:这个🍅是啥?再看AI,这是番茄,…再看Human,好吃吗,AI的观点是,…,
这些消息都保存在:
history
里面。它们只是当前 Node.js 进程中的 JavaScript 对象。程序一旦结束,这些对象自然也就不存在了。怎么说呢,下一次重新 node xxx.mjs 创建 new InMemoryChatMessageHistory 得到的是一份全新的 Memory。InMemoryChatMessageHistory 解决的是一次程序运行期间如何管理多轮聊天历史,却没有解决程序重启后如何继续使用以前的聊天历史。话说回来,
这就是为什么聊天历史不能只存在程序内存——还必须进入某种可以长期保存数据的地方。如文件、数据库、Redis 或云端存储。
为了把这个过程看清楚,我们先不急着接数据库。怎么说呢,直接把聊天历史写进一个 JSON 文件。这里使用 FileSystemChatMessageHistory。例如的观点是,
import path from "node:path";import { FileSystemChatMessageHistory } from "@langchain/community/stores/message/file_system";
接下来确定聊天记录保存的位置:
const filePath = path.join,"chat_history.json");
此时 chat_history.json 存在于项目目录下即使 JavaScript 内存消失,它仍然保留。
filePath 和 sessionId 分别在解决什么问题?Create History:
const history = new FileSystemChatMessageHistory({
filePath。sessionId: "user_session_001",});
const systemMessage = new SystemMessage;
const userMessage1 = new HumanMessage;await history.addMessage;话说回来,// 写入 History
const messages1 =;const response1 = await model.invoke;不过,await history.addMessage;老实说,// 保存 AI 回复
// 第一轮完整历史变成:
Human的观点是。这个🍅是啥,AI这方面,这是番茄…,
十、“持久化 Memory”到底比 InMemory 多了什么?
-
A quick comparison table :
text
User chats → History in memory → Program closed → History disappears
User chats → History written to file/DB → Program closed → File/DB persists → Restart → Load file/DB → Continue chat
Thus,wher or not Memory is persisted has nothing directly to do with LLM itself—it’s purely an application‑level storage issue.
It shifts focus from “how does model get previous context?” to “where is that context stored?”
十一、为什么还需要 ?
-
If multiple users share a single application,ir messages must not be mixed. Each must have a unique identifier . The storage layout might look like:
text
session_001 ├─ messageA
├─ messageB
└─ messageC
session_002 ├─ …
When calling LLM,we first retrieve correct Session’s History and n feed it.
"""
"File Memory"不是终点 – 在真实生产环境中可能使用更复杂的数据层。但主要流程保持不变:
| 步骤 | 描述 |
|------|------|
| 持久化存储 | 将 Session 数据写入数据库或云端 |
| 查询 Session | 根据 session/id 获取对应记录 |
| 恢复 History | 构造本次上下文 |
| 调用 LLM | 给定上下文得到答案 |
|
持久化 | 保存最新回复 |
这才是真正聊天程序中的 Memory。"
但解决持久化后一个更麻烦的问题出现了 – 历史太长怎么办?
-
User may talk for months resulting in thousands of messages. Sending all of m every time is impractical.
• Need Context Truncation – limit how many tokens are sent back to LLM while still retaining essential information.
• Techniques include summarization。sliding windows,or policy-based retention.
• Core challenge becomes Memory Management rar than simple persistence.
"""
。
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback