96SEO 2026-09-06 07:50 3
在实际项目中,最常见的痛点之一就是切换会话后忘记之前说过的关键信息。这不仅影响使用者体验,也导致开发者频繁重复实现相同功能。上一章我们只讨论了 Agent 的短期记忆——checkpointer——它只能在同一线程内保持状态,切换线程就会导致所有对话历史被抹去。

checkpointer: 绑定 thread_id,只在单个线程内有效;适合快速原型和调试,
Store: 无关 thread_id,可以在不同线程之间共享数据;适合真正需要持久化和多租户隔离的场景。
| checkpointer | Store | |
|---|---|---|
| 作用域 | 单 thread | 跨 thread |
| 存什么 | Agent 状态 | 业务数据 |
| 隔离维度 | thread_id | namespace |
from langgraph.store.memory import InMemoryStore
store = InMemoryStore# 写:namespace= + key + 值字典store.put。"user_123",{"name": "小明","language": "中文"})# 读:按 namespace + key 精确查找,返回 Item,.value 取字典item = store.get,"user_123")print # {'name': '小明','language': '中文'}
print # user_123# namespace 是元组、可多层:
store.put,"profile",{"city": "北京","hobby": "篮球"})
store.put,"profile",{"city": "上海","hobby": "足球"})
print,".value") # {'city': '北京','hobby': '篮球'}
print,".value") # {'city': '上海','hobby': '足球'}
# put 写入;老实说,namespace 是元组,key 是字符串,值是字典。# get 读取;
返回 Item,可通过 .value 获取字典。
天然实现按使用者隔离。# 同一个 key 在不同 namespace 下互不干扰 再看alice,{'city': '北京','hobby': '篮球'} 从bob来看,{'city': '上海'。'hobby': '足球'}
# InMemoryStore 存在内存里生产环境请使用 PostgresStore 等落盘方案。
下面演示如何让同一个 Agent 在不同 thread_id 上保持自身状态,同时共享长期数据。
from langgraph.store.memory import InMemoryStore
from langgraph.graph import create_agent
store = InMemoryStore
store.put。"alice",{"name":"Alice","city":"北京"})
@tooldef
def get_user:
"""查使用者信息。"""
item = runtime.store.get,user_id)
return str if item else f"未找到 {user_id}"
agent = create_agent(
model=deepseek_llm,tools=,store=store,checkpointer=InMemorySaver。)
config1 = {"configurable": {"thread_id":"t1"}}
config2 = {"configurable": {"thread_id":"t2"}}
# 两个 thread 各自自我介绍
agent.invoke
agent.invoke
# 一样问名字 / 一样查 alice
agent.invoke
agent.invoke
agent.invoke
agent.invoke
UserContext 用于把运行时的 User ID 注入到工具调用中,这样就能实现多租户支持。
from dataclasses import dataclass
from typing_extensions import TypedDict
@dataclass
class UserContext:
user_id: str # 当前请求使用者 ID
class UserInfo:
name的观点是。str
再看city,str
@tooldef
def save_user_info -> str:
"""保存当前使用者的信息到长期记忆."""
runtime.store.put,runtime.context.user_id,dict)
return f"已保存使用者 {runtime.context.user_id} 的信息。
"
@tooldef
def get_user_info -> str:
"""从长期记忆读取当前使用者的信息."""
item = runtime.store.get,runtime.context.user_id)
return str if item else f"暂无使用者 {runtime.context.user_id} 的信息。"
agent = create_agent(
model=deepseek_llm,tools=,store=store,context_schema=UserContext,)
# alice & bob 各自保存自己的信息
agent.invoke(
{"messages":},context=UserContext,)
agent.invoke(
{"messages":},context=UserContext。)
# 各自查询自己的信息
agent.invoke(
{"messages":},context=UserContext,)
agent.invoke(
{"messages":},context=UserContext,)
"同一个 Agent 用同一套工具。通过传递不同 `User ID` 完全服务多位使用者,无需额外配置。"
If you want fuzzy retrieval rar than exact match。you can let Store perform vector search via an IndexConfig.
from langgraph.store.base import IndexConfig
def embed:
"""文本列表 → 向量列表 ."""
return openai_embeddings.embed_documents
index_cfg = IndexConfig
store = InMemoryStore
namespace=
for k,text in {
\"sport\":\"我喜欢打篮球,每周都去球场。\",\"movie\":\"我爱看电影,特别是科幻片。\",\"pet\": \"我养的猫叫橘子,今年三岁。老实说,\",\"tech\": \"最近在学 LangChain 做 Agent。\",}.items:
store.put
for it in store.search:
print
| Get | Search<\/th> | |
|---|---|---|
| 优点<\/th> | ||
| 缺点<\/th> | ||
|
| |
作为专业的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