Documentation
¶
Index ¶
- Constants
- type ActionChain
- type AddAction
- type AddContext
- func (c *AddContext) Abort()
- func (c *AddContext) AddEdges(edges ...Edge)
- func (c *AddContext) AddEntities(entities ...Entity)
- func (c *AddContext) AddEpisodes(eps ...Episode)
- func (c *AddContext) AddSummaries(summaries ...Summary)
- func (c *AddContext) AddTokenUsage(actionName string, inputTokens, outputTokens int)
- func (c *AddContext) Error() error
- func (c *AddContext) Get(key string) (any, bool)
- func (c *AddContext) GetTokenUsage(actionName string) TokenUsage
- func (c *AddContext) IsAborted() bool
- func (c *AddContext) LanguageName() string
- func (c *AddContext) Next()
- func (c *AddContext) Set(key string, value any)
- func (c *AddContext) SetError(err error)
- func (c *AddContext) TotalTokenUsage() TokenUsage
- type AddRequest
- type AddResponse
- type Edge
- type Entity
- type EntityType
- type Episode
- type Message
- type Messages
- type RecallAction
- type RecallChain
- type RecallContext
- func (c *RecallContext) Abort()
- func (c *RecallContext) AddTokenUsage(actionName string, inputTokens, outputTokens int)
- func (c *RecallContext) Error() error
- func (c *RecallContext) Get(key string) (any, bool)
- func (c *RecallContext) GetTokenUsage(actionName string) TokenUsage
- func (c *RecallContext) IsAborted() bool
- func (c *RecallContext) Next()
- func (c *RecallContext) Set(key string, value any)
- func (c *RecallContext) SetError(err error)
- func (c *RecallContext) TotalResults() int
- func (c *RecallContext) TotalTokenUsage() TokenUsage
- type RetrieveOptions
- type RetrieveRequest
- type RetrieveResponse
- type Summary
- type TokenUsage
Constants ¶
const ( DocTypeEpisode = "episode" DocTypeEntity = "entity" DocTypeEdge = "edge" DocTypeSummary = "summary" )
const ( RoleUser = "user" RoleAssistant = "assistant" RoleSystem = "system" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionChain ¶
type ActionChain struct {
// contains filtered or unexported fields
}
ActionChain 管理 AddAction 处理器链
func (*ActionChain) Use ¶
func (chain *ActionChain) Use(actions ...AddAction) *ActionChain
Use 添加 action 到链
type AddAction ¶
type AddAction interface {
Name() string
Handle(*AddContext)
}
AddAction 用于 Add 流程的 action
type AddContext ¶
type AddContext struct {
// 输入
Messages Messages
// 输出
Episodes []Episode
Entities []Entity
Edges []Edge
Summaries []Summary
// 配置
Language string // 语言设置
TopicThreshold float64 // 主题变化阈值 (默认 0.7)
// contains filtered or unexported fields
}
AddContext 记忆写入上下文
func NewAddContext ¶
func NewAddContext(ctx context.Context, agentID, userID, sessionID string) *AddContext
NewAddContext 创建新的 AddContext
func (*AddContext) AddEntities ¶
func (c *AddContext) AddEntities(entities ...Entity)
AddEntities 添加 Entity
func (*AddContext) AddEpisodes ¶
func (c *AddContext) AddEpisodes(eps ...Episode)
AddEpisodes 添加 Episode
func (*AddContext) AddSummaries ¶
func (c *AddContext) AddSummaries(summaries ...Summary)
AddSummaries 添加 Summary
func (*AddContext) AddTokenUsage ¶
AddTokenUsage 记录 token 使用量
func (*AddContext) GetTokenUsage ¶
func (c *AddContext) GetTokenUsage(actionName string) TokenUsage
GetTokenUsage 获取指定 action 的 token 使用量
func (*AddContext) TotalTokenUsage ¶
func (c *AddContext) TotalTokenUsage() TokenUsage
TotalTokenUsage 获取所有 action 的 token 使用量总和
type AddRequest ¶
type AddRequest struct {
AgentID string `json:"agent_id"`
UserID string `json:"user_id"`
SessionID string `json:"session_id"`
Messages []Message `json:"messages"`
}
AddRequest 添加记忆请求
type AddResponse ¶
type AddResponse struct {
Success bool `json:"success"`
Episodes []Episode `json:"episodes,omitempty"`
Entities []Entity `json:"entities,omitempty"`
Edges []Edge `json:"edges,omitempty"`
Summaries []Summary `json:"summaries,omitempty"`
}
AddResponse 添加记忆响应
type Edge ¶
type Edge struct {
ID string `json:"id"`
SourceID string `json:"source_id"` // 起始实体 ID
TargetID string `json:"target_id"` // 目标实体 ID
Relation string `json:"relation"` // 关系类型 (如: 喜欢, 认识, 住在)
Fact string `json:"fact"` // 事实描述 (如: "用户喜欢喝咖啡")
// 向量
Embedding []float32 `json:"embedding,omitempty"`
// 双时间轴 (Bi-temporal)
ValidAt *time.Time `json:"valid_at,omitempty"` // 事实生效时间
InvalidAt *time.Time `json:"invalid_at,omitempty"` // 事实失效时间
// 入库时间
CreatedAt time.Time `json:"created_at"`
ExpiredAt *time.Time `json:"expired_at,omitempty"` // 记录过期时间
// 溯源
EpisodeIDs []string `json:"episode_ids,omitempty"` // 来源 Episode
// 检索分数
Score float64 `json:"score,omitempty"`
}
Edge 表示实体间的关系
type Entity ¶
type Entity struct {
ID string `json:"id"`
AgentID string `json:"agent_id"`
UserID string `json:"user_id"`
Name string `json:"name"` // 实体名称
Type EntityType `json:"type"` // 实体类型
Description string `json:"description"` // 实体描述
// 向量
Embedding []float32 `json:"embedding,omitempty"`
// 时间
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 检索分数
Score float64 `json:"score,omitempty"`
}
Entity 表示知识图谱中的实体节点
type EntityType ¶
type EntityType string
EntityType 实体类型
const ( EntityTypePerson EntityType = "person" // 人物 EntityTypePlace EntityType = "place" // 地点 EntityTypeThing EntityType = "thing" // 物品/概念 EntityTypeEvent EntityType = "event" // 事件 EntityTypeEmotion EntityType = "emotion" // 情绪 EntityTypeActivity EntityType = "activity" // 活动 )
type Episode ¶
type Episode struct {
ID string `json:"id"`
AgentID string `json:"agent_id"`
UserID string `json:"user_id"`
SessionID string `json:"session_id"`
// 对话内容
Role string `json:"role"` // user / assistant
Name string `json:"name"` // 发言者名称
// 主题
Topic string `json:"topic"`
TopicEmbedding []float32 `json:"topic_embedding"`
// 内容
Content string `json:"content"`
Embedding []float32 `json:"content_embedding"`
// 时间
Timestamp time.Time `json:"timestamp"` // 对话发生时间
CreatedAt time.Time `json:"created_at"` // 入库时间
// 检索分数 (查询时填充)
Score float64 `json:"score,omitempty"`
}
Episode 表示一条原始对话记录 存储在 OpenSearch (向量检索)
type Message ¶
type Message struct {
Role string `json:"role"` // user / assistant / system
Content string `json:"content"` // 消息内容
Name string `json:"name,omitempty"` // 发言者名称
}
Message 表示一条对话消息
type RecallAction ¶
type RecallAction interface {
Name() string
HandleRecall(*RecallContext)
}
RecallAction 用于 Recall 流程的 action
type RecallChain ¶
type RecallChain struct {
// contains filtered or unexported fields
}
RecallChain 管理 RecallAction 处理器链
func (*RecallChain) Use ¶
func (chain *RecallChain) Use(actions ...RecallAction) *RecallChain
Use 添加 action 到链
type RecallContext ¶
type RecallContext struct {
// 查询参数
Query string
Embedding []float32
Limit int
Options RetrieveOptions
// 检索结果 - Zep 三层结构
Episodes []Episode
Entities []Entity
Edges []Edge
Summaries []Summary
// contains filtered or unexported fields
}
RecallContext 记忆检索上下文
func NewRecallContext ¶
func NewRecallContext(ctx context.Context, req *RetrieveRequest) *RecallContext
NewRecallContext 创建新的 RecallContext
func (*RecallContext) AddTokenUsage ¶
AddTokenUsage 记录 token 使用量
func (*RecallContext) GetTokenUsage ¶
func (c *RecallContext) GetTokenUsage(actionName string) TokenUsage
GetTokenUsage 获取指定 action 的 token 使用量
func (*RecallContext) TotalResults ¶
func (c *RecallContext) TotalResults() int
TotalResults 返回检索结果总数
func (*RecallContext) TotalTokenUsage ¶
func (c *RecallContext) TotalTokenUsage() TokenUsage
TotalTokenUsage 获取所有 action 的 token 使用量总和
type RetrieveOptions ¶
type RetrieveOptions struct {
MaxHops int `json:"max_hops,omitempty"` // 图遍历最大跳数
MaxTokens int `json:"max_tokens,omitempty"` // 总 token 预算,默认 2000
// 各类型数量限制(-1 禁用,0 使用默认值,>0 自定义)
MaxSummaries int `json:"max_summaries,omitempty"` // 默认 3
MaxEdges int `json:"max_edges,omitempty"` // 默认 10
MaxEntities int `json:"max_entities,omitempty"` // 默认 5
MaxEpisodes int `json:"max_episodes,omitempty"` // 默认 5
}
RetrieveOptions 检索选项 按优先级检索:Summary > Edge > Entity > Episode 设置 Max* = 0 可禁用该类型检索,不设置则使用默认值
type RetrieveRequest ¶
type RetrieveRequest struct {
AgentID string `json:"agent_id"`
UserID string `json:"user_id"`
SessionID string `json:"session_id,omitempty"`
Query string `json:"query"`
Limit int `json:"limit,omitempty"`
// 检索选项
Options RetrieveOptions `json:"options,omitempty"`
}
RetrieveRequest 检索记忆请求
type RetrieveResponse ¶
type RetrieveResponse struct {
Success bool `json:"success"`
Episodes []Episode `json:"episodes,omitempty"`
Entities []Entity `json:"entities,omitempty"`
Edges []Edge `json:"edges,omitempty"`
Summaries []Summary `json:"summaries,omitempty"`
Total int `json:"total"`
// 格式化后的记忆上下文 (用于 LLM prompt)
MemoryContext string `json:"memory_context,omitempty"`
}
RetrieveResponse 检索记忆响应
type Summary ¶
type Summary struct {
ID string `json:"id"`
AgentID string `json:"agent_id"`
UserID string `json:"user_id"`
EpisodeIDs []string `json:"episode_ids"` // 关联的 Episode ID
Content string `json:"content"` // 摘要内容
Topic string `json:"topic"` // 主题标签
// 向量
Embedding []float32 `json:"embedding,omitempty"`
// 时间
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 检索分数
Score float64 `json:"score,omitempty"`
}
Summary 表示一组对话的压缩摘要
type TokenUsage ¶
type TokenUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
TokenUsage 记录 token 使用量