Repository navigation
zh dev star guides ai conversations
会话由消息来源 event.unified_msg_origin(UMO)标识;对话由 cid 标识,用来保存一组模型消息历史。同一会话可以创建和切换不同的对话。
需要等待用户的后续消息时,参见会话控制器。本页介绍对话历史的读取与写入。
通过 self.context.conversations 创建、读取、更新、切换和删除对话。Conversation.history 是包含模型消息的 JSON 字符串。
_no_save 会将消息或内容片段排除在后续模型上下文之外,但不会阻止原文保存。
Tip
从 v4.30.0 开始,对话管理接口有以下调整:
-
self.context.conversation_manager保留为self.context.conversations的兼容别名。 - 将
event.conversation_events和get_conversation_events()替换为append_message()或append_plugin_event()。 - 将
update_conversation_title()和update_conversation_persona_id()替换为update_conversation(..., title=...)和update_conversation(..., persona_id=...)。 - 平台历史关联中的
llm_checkpoint_id改用turn_id和context_event_id。
conversations = self.context.conversations
message_id = await conversations.append_message(
"用户偏好简洁的回答。",
cid=target_cid,
)content 接受文本或 AstrBot 的 ContentPart 对象列表:
from astrbot.core.agent.message import ImageURLPart, TextPart
await conversations.append_message(
[
TextPart(text="请描述这张图片。"),
ImageURLPart(
image_url=ImageURLPart.ImageURL(url="https://example.com/image.png")
),
],
cid=target_cid,
)消息以 role="user" 保存并进入后续上下文,不会修改正在运行的 Agent 的上下文。返回值是已保存的消息 ID;可传入 message_id 标识同一次提交的重试。
event_id = await conversations.append_plugin_event(
event_type="my_plugin.retrieval_completed",
data={"document_ids": ["document-1"]},
cid=target_cid,
)插件事件与对话一起保存,不会进入模型上下文。event_type 建议使用插件名称前缀,data 必须是可 JSON 序列化的字典。返回值是已保存的事件 ID;可传入 event_id 标识同一次提交的重试。
无需事件历史的可变状态可使用插件 KV 存储。按对话区分时,将 cid 放入 key。
两个追加接口都必须传入已有对话的 cid,无效或不存在时抛出 ValueError。它们不会启动 Agent turn。
修改当前执行的上下文,请编辑相应 hook 的 ProviderRequest.contexts 或运行时消息列表。直接调用对话写入接口(包括 update_conversation() 和 add_message_pair())不会更新正在运行的 Agent 上下文;并发修改已保存的历史可能触发 ConversationConflictError。
# AstrBot v4.30.0 之前的导入方式:
# from astrbot.core.conversation_mgr import Conversation
from astrbot.api.provider import Conversation
uid = event.unified_msg_origin
conv_mgr = self.context.conversations
curr_cid = await conv_mgr.get_curr_conversation_id(uid)
conversation = await conv_mgr.get_conversation(uid, curr_cid) # Conversation::: details Conversation 类型定义
@dataclass
class Conversation:
"""The conversation entity representing a chat session."""
platform_id: str
"""The platform ID in AstrBot"""
user_id: str
"""The user ID associated with the conversation."""
cid: str
"""The conversation ID, in UUID format."""
history: str = ""
"""The conversation history as a string."""
title: str | None = ""
"""The title of the conversation. For now, it's only used in WebChat."""
persona_id: str | None = ""
"""The persona ID associated with the conversation."""
created_at: int = 0
"""The timestamp when the conversation was created."""
updated_at: int = 0
"""The timestamp when the conversation was last updated.""":::
# AstrBot v4.30.0 之前的导入方式:
# from astrbot.core.agent.message import AssistantMessageSegment, UserMessageSegment, TextPart
from astrbot.api.agents import (
AssistantMessageSegment,
UserMessageSegment,
TextPart,
)
conv_mgr = self.context.conversations
provider_id = await self.context.get_current_chat_provider_id(event.unified_msg_origin)
curr_cid = await conv_mgr.get_curr_conversation_id(event.unified_msg_origin)
user_msg = UserMessageSegment(content=[TextPart(text="hi")])
llm_resp = await self.context.llm_generate(
chat_provider_id=provider_id, # 聊天模型 ID
contexts=[
user_msg
], # 当未指定 prompt 时,使用 contexts 作为输入;同时指定 prompt 和 contexts 时,prompt 会被添加到 LLM 输入的最后
)
await conv_mgr.add_message_pair(
cid=curr_cid,
user_message=user_msg,
assistant_message=AssistantMessageSegment(
content=[TextPart(text=llm_resp.completion_text)]
),
)Warning
上面用 unified_msg_origin 作为会话历史的存储键。开启隔离对话(platform_settings.unique_session)后,群消息的 unified_msg_origin 会变化(仅限支持隔离的渠道),同一个群在不同开关状态下会解析到不同的会话。需要按群定位时请使用 event.get_group_id(),详见处理消息事件。
- 首页
- 文档入口
- Top Level
- community events
- config
- deploy
- dev
- others
- platform
- 接入 OneBot v11 协议实现
- 接入钉钉 DingTalk
- 接入 Discord
- 接入 Kook
- 接入飞书
- 接入 LINE
- 接入 Matrix
- 接入 Mattermost
- 接入 Misskey 平台
- 接入 QQ 官方机器人平台
- 通过 QQ官方机器人 接入 QQ (Webhook)
- 通过 QQ官方机器人 接入 QQ (Websockets)
- 接入 Satori 协议
- 接入 server-satori (基于 Koishi)
- 接入 Slack
- 接入消息平台
- 接入 Telegram
- 接入 VoceChat
- AstrBot 接入企业微信
- 接入企业微信智能机器人平台
- AstrBot 接入微信公众平台
- 接入个人微信
- providers
- use
- Home
- Docs Entry
- Top Level
- config
- deploy
- Deploy AstrBot on 1Panel
- Deploy AstrBot on BT Panel
- Deploy AstrBot on CasaOS
- Deploy AstrBot from Source Code
- Community-Provided Deployment Methods
- Deploy via Compshare
- Deploy with AstrBot Desktop Client
- Deploy AstrBot with Docker
- Deploy AstrBot with Kubernetes
- Deploy AstrBot with AstrBot Launcher
- Other Deployments
- Script Deployment
- Installation via System Package Manager
- Preface
- dev
- AstrBot Configuration File
- API Scope–Endpoint Reference
- AstrBot HTTP API
- AstrBot Plugin Market JSON Specification
- AstrBot Plugin Market JSON Specification
- Developing a Platform Adapter
- plugin
- AI
- Agents
- Sessions and Conversations
- Models
- Other APIs
- Tools
- Documentation moved
- Text to Image
- Handling Message Events
- Plugin Configuration
- Plugin Internationalization
- Documentation moved
- Shared Bridge API
- Interactive message component
- Standalone plugin page
- Backend Web APIs
- Workbench page
- Sending Messages
- Session Control
- Minimal Example
- Plugin Storage
- AstrBot Plugin Development Guide 🌠
- Publishing Plugins to the Plugin Marketplace
- ospp
- others
- platform
- Connect OneBot v11 Protocol Implementations
- Connect to DingTalk
- Connecting to Discord
- Connect to KOOK
- Connecting to Lark
- Connecting to LINE
- Connecting to Matrix
- Connecting to Mattermost
- Connecting to Misskey Platform
- Connect QQ Official Bot
- Connect QQ via QQ Official Bot (Webhook)
- Connect QQ via QQ Official Bot (Websockets)
- Connect to Satori Protocol
- Connect server-satori (Koishi)
- Connecting to Slack
- Messaging Platforms
- Connecting to Telegram
- Connect to VoceChat
- Connect AstrBot to WeCom
- Connect to WeCom AI Bot Platform
- Connect AstrBot to WeChat Official Account Platform
- Connect Personal WeChat
- providers
- Connect 302.AI
- Agent Runners
- Built-in Agent Runner
- Connect to Coze
- Connect to Alibaba Cloud Bailian Application
- Connect to DeerFlow
- Connect to Dify
- Connect AIHubMix
- coze
- dashscope
- DeepSeek STT Integration
- DeepSeek TTS Integration
- dify
- Model input images
- Large Language Model Providers
- Connect MiraRouter
- NewAPI
- Connect PPIO Cloud
- Connect LM Studio to Use DeepSeek-R1 and Other Models
- Integrating Ollama
- Connect ShengSuanYun
- Connecting to SiliconFlow
- Connecting Model Services
- Connecting to TokenPony
- use
- Agent Execution Mode {#agent-runner}
- Agent Sandbox Environment
- astrbot sandbox
- AstrBot CLI {#cli-commands}
- Docker-based Code Interpreter
- Built-in Commands
- computer
- Context Compression
- Session Configuration Mappings (formerly Custom Rules)
- AstrBot Knowledge Base
- MCP
- AstrBot Plugins {#astrbot-star}
- Proactive Capabilities
- Skills {#anthropic-skills}
- Subagents {#agent-handsoff-and-subagent}
- Unified Webhook Mode
- Web Search
- DeepSeek Search API
- SerpApi
- WebUI