Chat History¶
显式历史管理保证多轮可控。
源码¶
from agently import Agently
agent = Agently.create_agent()
Agently.set_settings(
"OpenAICompatible",
{
"base_url": "http://127.0.0.1:11434/v1",
"model": "qwen2.5:7b",
},
)
## Chat History: basic multi-turn management
def chat_history_basic():
# You can add messages to chat_history to keep multi-turn context.
agent.set_chat_history(
[
{"role": "user", "content": "Hi, who are you?"},
{"role": "assistant", "content": "I'm an Agently assistant."},
]
)
result = agent.input("What did I ask you before?").start()
print(result)
# You can append new turns, or reset the history.
# Treat the last answer as a new user message to continue the thread.
agent.add_chat_history({"role": "user", "content": result})
follow_up = agent.input("Summarize my last message in one sentence.").start()
print(follow_up)
agent.reset_chat_history()
# chat_history_basic()
讲解¶
- 使用 set/add/reset 管理对话。
- 只保留必要轮次。
注释解读¶
- Idea 表示案例思路
- Flow 表示执行编排路径
- Expect 表示预期输出或行为
你学会了什么¶
- 显式管理历史对话以保证可控
- 理解 history 与 system prompt 的区别
练习任务¶
- 构造 3 轮对话并手动注入历史
- 只保留最近两轮观察变化