Documentation
¶
Overview ¶
Package conversation 提供多轮对话管理功能
本包实现了对话历史管理、上下文窗口控制和 Token 预算管理:
- 自动维护对话历史
- 基于 Token 预算的上下文窗口
- 超出预算时自动截断旧消息
- 支持对话摘要压缩
使用示例:
mgr := conversation.New(
conversation.WithMaxTokens(4096),
conversation.WithSystemPrompt("你是一个助手"),
)
mgr.AddUserMessage("你好")
messages := mgr.GetMessages()
Index ¶
- type Manager
- func (m *Manager) AddAssistantMessage(content string)
- func (m *Manager) AddSystemMessage(content string)
- func (m *Manager) AddToolResult(toolName string, result string)
- func (m *Manager) AddUserMessage(content string)
- func (m *Manager) Clear()
- func (m *Manager) GetContext(maxTokens int) []llm.Message
- func (m *Manager) GetMessages() []llm.Message
- func (m *Manager) History() []TimedMessage
- func (m *Manager) MessageCount() int
- func (m *Manager) Summarize(ctx context.Context, provider llm.Provider, keepRecent int) error
- func (m *Manager) TurnCount() int
- type Option
- type TimedMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 多轮对话管理器
维护有序的消息列表,支持 Token 预算限制和上下文窗口控制。 线程安全,可在多 goroutine 中使用。
func (*Manager) AddAssistantMessage ¶
AddAssistantMessage 添加助手回复
func (*Manager) AddSystemMessage ¶
AddSystemMessage 添加系统消息
func (*Manager) AddToolResult ¶
AddToolResult 添加工具调用结果
func (*Manager) AddUserMessage ¶
AddUserMessage 添加用户消息
func (*Manager) GetContext ¶
GetContext 获取指定 Token 预算内的消息
func (*Manager) GetMessages ¶
GetMessages 获取消息列表(应用 Token 预算和轮次限制)
始终保留系统提示词,然后从最近的消息开始向前填充, 直到达到 Token 预算或轮次限制。
type Option ¶
type Option func(*Manager)
Option 配置选项
Click to show internal directories.
Click to hide internal directories.