Skip to content

05 Persistence

🎯 学完能让 agent 状态自动保存 / 一次性 AI 调用不污染历史 / 中断恢复。

概念

tangyuanAI 默认在每次 conversation() / aconversation() 退出时自动把 agent 状态存到 .tas 文件(可换 sqlite 等后端)。无需手动保存。

关键参数(conversation() / aconversation() 都支持):

参数默认含义
addhistory=TrueTrue是否把 user / assistant / tool_result 写入 history
tooluse=TrueTrue是否派发 tools schema + 走 FC 续轮

addhistory=False 是 v1.3.0 新加的——做"不计入对话"的一次性 AI 调用(分类 / 路由 / 上下文增强),LLM 回复你拿得到,但 history 不留痕。

5 行代码起步

python
import tangyuanAI
tangyuanAI.configure(auto_save=True)              # ← 默认就在最外层保存

agent = tangyuanAI.agent_list["weather"]
out = agent.conversation("北京天气?")              # 默认入 history,退出自动存
out = agent.conversation("分类这段文本", tooluse=False, addhistory=False)  # 一次性

手动恢复

python
state = tangyuanAI.load_state(agent_uuid="weather-1")    # → dict
# 或:tangyuanAI.load_state(agent_name="weather")
new_agent = tangyuanAI.BaseAgent.from_state_dict(state)
new_agent.conversation("继续刚才的对话")                  # 接续 history

后端切换

bash
# 文件后端(默认)
tangyuanai configure --backend file --base-dir ./sessions

# SQLite 后端
tangyuanai configure --backend sqlite --db-path ./sessions.db

完整 demo

进阶

常见问题

Q: 我的 agent 是批处理一次性脚本,不需要持久化怎么办? A: tangyuanai configure --auto-save false 关闭全局;或在 agent 上设 auto_save=False(per-agent)。

Q: 多 agent 互相调(ask_for_help),持久化会不会冲突? A: 不会。框架用 _conv_depth 计数器保证递归调用只在最外层出口触发一次 auto_save

Q: addhistory=False 后,工具结果会进 history 吗? A: 默认不进。但若开了 FC(tooluse=True),工具调用过程中 assistant(tool_calls)tool(role) 会进 history(FC 协议要求)。要彻底无痕用 tooluse=False, addhistory=False