跳转至

多轮对话与记忆

让 Agent 记住上一轮内容,只需在连续调用中使用同一个 session_id。不传会话或历史时,每次调用独立。

记住项目名称

完成模型配置,运行 python examples/runtime/sessions.py:

import asyncio
import os

import bumblehive


async def main() -> None:
    config = bumblehive.RuntimeArguments(
        model=os.environ["BUMBLEHIVE_MODEL"],
        api_key=os.environ["BUMBLEHIVE_API_KEY"],
        base_url=os.environ["BUMBLEHIVE_BASE_URL"],
        skill_names=[],
        tool_names=[],
    )
    async with bumblehive.from_config(config) as runtime:
        first = await runtime.run("记住:我的项目叫 BumbleHive。", session_id="hello-agent")
        print(first.error.message if first.error else first.final_content)
        second = await runtime.run("我的项目叫什么?", session_id="hello-agent")
        print(second.error.message if second.error else second.final_content)


if __name__ == "__main__":
    asyncio.run(main())

两次调用使用同一个 ID。第二次回答应包含 BumbleHive,因为 SDK 会读取第一轮的对话。

会话默认保存在 ~/.bumblehive/sessions/。程序重启后仍可使用相同 ID 继续;换一个 ID 就是另一段对话。

只在内存中保存

临时对话可以使用 MessageHistory。运行 python examples/runtime/conversation_history.py:

import asyncio
import os

import bumblehive


async def main() -> None:
    config = bumblehive.RuntimeArguments(
        model=os.environ["BUMBLEHIVE_MODEL"],
        api_key=os.environ["BUMBLEHIVE_API_KEY"],
        base_url=os.environ["BUMBLEHIVE_BASE_URL"],
        skill_names=[],
        tool_names=[],
    )
    history = bumblehive.MessageHistory()

    async with bumblehive.from_config(config) as runtime:
        first = await runtime.run(
            "Remember that my project is named Bumblehive.",
            history=history,
        )
        history.replace_run_messages(first.messages)
        second = await runtime.run(
            "What is my project named?",
            history=history,
        )
        history.replace_run_messages(second.messages)

    print("First:", first.final_content)
    print("Second:", second.final_content)
    print("History messages:", len(history.get_history()))


if __name__ == "__main__":
    asyncio.run(main())

与 session_id 不同,Runtime 不会自动修改这个对象。每轮用 history.replace_run_messages(result.messages) 更新后,下一轮才能看到新内容。该方法会去除运行生成的系统上下文。

清空对话

history.clear()  # 清空内存历史。
await runtime.delete_session("hello-agent")  # 删除持久化会话。

一次调用只选择 history 或 session_id 其中一种。并发使用约束见消息与会话参考。