Documentation
¶
Overview ¶
Package conversation 提供多代理对话编排与状态流转能力。
Index ¶
- type Branch
- type ChatMessage
- type Conversation
- type ConversationAgent
- type ConversationConfig
- type ConversationMode
- type ConversationResult
- type ConversationState
- type ConversationTree
- func (t *ConversationTree) AddMessage(msg llm.Message) *ConversationState
- func (t *ConversationTree) DeleteBranch(branchName string) error
- func (t *ConversationTree) Export() ([]byte, error)
- func (t *ConversationTree) FindSnapshot(label string) *ConversationState
- func (t *ConversationTree) Fork(branchName string) (*Branch, error)
- func (t *ConversationTree) GetCurrentState() *ConversationState
- func (t *ConversationTree) GetHistory() []*ConversationState
- func (t *ConversationTree) GetMessages() []llm.Message
- func (t *ConversationTree) ListBranches() []string
- func (t *ConversationTree) MergeBranch(sourceBranch string) error
- func (t *ConversationTree) RestoreSnapshot(label string) error
- func (t *ConversationTree) Rollback(stateID string) error
- func (t *ConversationTree) RollbackN(n int) error
- func (t *ConversationTree) Snapshot(label string) *ConversationState
- func (t *ConversationTree) SwitchBranch(branchName string) error
- type GroupChatManager
- type LLMClient
- type LLMSelector
- type RoundRobinSelector
- type SpeakerSelector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Branch ¶
type Branch struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
States []*ConversationState `json:"states"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
IsActive bool `json:"is_active"`
}
分会代表谈话分会.
type ChatMessage ¶
type ChatMessage struct {
ID string `json:"id"`
Role string `json:"role"` // user, assistant, system
SenderID string `json:"sender_id"`
Content string `json:"content"`
Timestamp time.Time `json:"timestamp"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ChatMessage代表谈话中的信息.
type Conversation ¶
type Conversation struct {
ID string
Mode ConversationMode
Agents []ConversationAgent
Messages []ChatMessage
Config ConversationConfig
Selector SpeakerSelector
// contains filtered or unexported fields
}
对话管弦乐会多代理对话.
func NewConversation ¶
func NewConversation(mode ConversationMode, agents []ConversationAgent, config ConversationConfig, logger *zap.Logger) *Conversation
新联想创造了一个新的对话。
func (*Conversation) GetMessages ¶
func (c *Conversation) GetMessages() []ChatMessage
GetMessages 返回所有信件 。
func (*Conversation) Start ¶
func (c *Conversation) Start(ctx context.Context, initialMessage string) (*ConversationResult, error)
以初始消息启动对话 。
type ConversationAgent ¶
type ConversationAgent interface {
ID() string
Name() string
SystemPrompt() string
Reply(ctx context.Context, messages []ChatMessage) (*ChatMessage, error)
ShouldTerminate(messages []ChatMessage) bool
}
对话代理界面 。
type ConversationConfig ¶
type ConversationConfig struct {
MaxRounds int `json:"max_rounds"`
MaxMessages int `json:"max_messages"`
Timeout time.Duration `json:"timeout"`
AllowInterrupts bool `json:"allow_interrupts"`
TerminationWords []string `json:"termination_words"`
}
对话 Config 配置对话 。
func DefaultConversationConfig ¶
func DefaultConversationConfig() ConversationConfig
默认 Conversation Config 返回默认配置 。
type ConversationMode ¶
type ConversationMode string
对话 模式定义代理如何互动 。
const ( ModeRoundRobin ConversationMode = "round_robin" // Agents take turns ModeSelector ConversationMode = "selector" // Selector chooses next speaker ModeGroupChat ConversationMode = "group_chat" // Free-form group discussion ModeHierarchical ConversationMode = "hierarchical" // Manager delegates ModeAutoReply ConversationMode = "auto_reply" // Automatic response chain )
type ConversationResult ¶
type ConversationResult struct {
ConversationID string `json:"conversation_id"`
Messages []ChatMessage `json:"messages"`
TotalRounds int `json:"total_rounds"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
TerminationReason string `json:"termination_reason"`
}
对话Result包含对话结果.
type ConversationState ¶
type ConversationState struct {
ID string `json:"id"`
ParentID string `json:"parent_id,omitempty"`
Messages []llm.Message `json:"messages"`
Metadata map[string]any `json:"metadata,omitempty"`
CreatedAt time.Time `json:"created_at"`
Label string `json:"label,omitempty"`
}
对话状态(Conversation State)代表时间点的对话状态.
type ConversationTree ¶
type ConversationTree struct {
ID string `json:"id"`
RootState *ConversationState `json:"root_state"`
Branches map[string]*Branch `json:"branches"`
ActiveBranch string `json:"active_branch"`
// contains filtered or unexported fields
}
对话 树用分支管理对话历史.
func (*ConversationTree) AddMessage ¶
func (t *ConversationTree) AddMessage(msg llm.Message) *ConversationState
添加 Message 为活动分支添加了消息 。
func (*ConversationTree) DeleteBranch ¶
func (t *ConversationTree) DeleteBranch(branchName string) error
删除Branch删除一个分支(不能删除活动分支或主分支).
func (*ConversationTree) FindSnapshot ¶
func (t *ConversationTree) FindSnapshot(label string) *ConversationState
FindSnapshot通过标签找到快照.
func (*ConversationTree) Fork ¶
func (t *ConversationTree) Fork(branchName string) (*Branch, error)
叉从当前状态创建出一个新的分支.
func (*ConversationTree) GetCurrentState ¶
func (t *ConversationTree) GetCurrentState() *ConversationState
GetCurentState 返回活动分支当前状态 。
func (*ConversationTree) GetHistory ¶
func (t *ConversationTree) GetHistory() []*ConversationState
GetHistory还原了目前分行的州史.
func (*ConversationTree) GetMessages ¶
func (t *ConversationTree) GetMessages() []llm.Message
GetMessages 返回当前状态下的所有信件 。
func (*ConversationTree) ListBranches ¶
func (t *ConversationTree) ListBranches() []string
ListBranches 返回所有分支名称 。
func (*ConversationTree) MergeBranch ¶
func (t *ConversationTree) MergeBranch(sourceBranch string) error
合并Branch将一个分支合并到活动分支.
func (*ConversationTree) RestoreSnapshot ¶
func (t *ConversationTree) RestoreSnapshot(label string) error
还原Snapshot恢复到标签快照.
func (*ConversationTree) Rollback ¶
func (t *ConversationTree) Rollback(stateID string) error
后滚回当前分行的上个状态.
func (*ConversationTree) Snapshot ¶
func (t *ConversationTree) Snapshot(label string) *ConversationState
抓图创建当前状态的标签快照.
func (*ConversationTree) SwitchBranch ¶
func (t *ConversationTree) SwitchBranch(branchName string) error
切换Branch切换到不同的分支.
type GroupChatManager ¶
type GroupChatManager struct {
// contains filtered or unexported fields
}
GroupChatManager管理分组聊天对话.
func NewGroupChatManager ¶
func NewGroupChatManager(logger *zap.Logger) *GroupChatManager
NewGroupChatManager创建了新的分组聊天管理器.
func (*GroupChatManager) CreateChat ¶
func (m *GroupChatManager) CreateChat(agents []ConversationAgent, config ConversationConfig) *Conversation
CreateChat 创建了新的分组聊天.
func (*GroupChatManager) GetChat ¶
func (m *GroupChatManager) GetChat(id string) (*Conversation, bool)
GetChat通过身份证检索谈话内容.
type LLMSelector ¶
type LLMSelector struct {
LLM LLMClient
}
LLMSelector使用LLM来选择下一位扬声器.
func (*LLMSelector) SelectNext ¶
func (s *LLMSelector) SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)
type RoundRobinSelector ¶
type RoundRobinSelector struct {
// contains filtered or unexported fields
}
roundRobinSelector按顺序选择代理.
func (*RoundRobinSelector) SelectNext ¶
func (s *RoundRobinSelector) SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)
type SpeakerSelector ¶
type SpeakerSelector interface {
SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)
}
发言人选择下一位发言人.