domain

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DocTypeEpisode = "episode"
	DocTypeEntity  = "entity"
	DocTypeEdge    = "edge"
	DocTypeSummary = "summary"
)
View Source
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 NewActionChain

func NewActionChain() *ActionChain

NewActionChain 创建新的 Action 链

func (*ActionChain) Run

func (chain *ActionChain) Run(c *AddContext)

Run 顺序执行链中的所有 action

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) Abort

func (c *AddContext) Abort()

Abort 终止链式执行

func (*AddContext) AddEdges

func (c *AddContext) AddEdges(edges ...Edge)

AddEdges 添加 Edge

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

func (c *AddContext) AddTokenUsage(actionName string, inputTokens, outputTokens int)

AddTokenUsage 记录 token 使用量

func (*AddContext) Error

func (c *AddContext) Error() error

Error 返回错误

func (*AddContext) Get

func (c *AddContext) Get(key string) (any, bool)

Get 获取元数据

func (*AddContext) GetTokenUsage

func (c *AddContext) GetTokenUsage(actionName string) TokenUsage

GetTokenUsage 获取指定 action 的 token 使用量

func (*AddContext) IsAborted

func (c *AddContext) IsAborted() bool

IsAborted 返回链是否被终止

func (*AddContext) LanguageName

func (c *AddContext) LanguageName() string

LanguageName 返回语言名称

func (*AddContext) Next

func (c *AddContext) Next()

Next 调用链中的下一个 action

func (*AddContext) Set

func (c *AddContext) Set(key string, value any)

Set 存储元数据

func (*AddContext) SetError

func (c *AddContext) SetError(err error)

SetError 设置错误并终止链

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 表示实体间的关系

func (*Edge) IsValid

func (e *Edge) IsValid(at time.Time) bool

IsValid 检查边在指定时间点是否有效

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 Messages

type Messages []Message

Messages 消息列表

func (Messages) AssistantName

func (m Messages) AssistantName() string

AssistantName 获取助手名称

func (Messages) Format

func (m Messages) Format() string

Format 格式化为对话文本

func (Messages) UserName

func (m Messages) UserName() string

UserName 获取用户名称

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 NewRecallChain

func NewRecallChain() *RecallChain

NewRecallChain 创建新的 Recall 链

func (*RecallChain) Run

func (chain *RecallChain) Run(c *RecallContext)

Run 顺序执行链中的所有 action

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) Abort

func (c *RecallContext) Abort()

Abort 终止链式执行

func (*RecallContext) AddTokenUsage

func (c *RecallContext) AddTokenUsage(actionName string, inputTokens, outputTokens int)

AddTokenUsage 记录 token 使用量

func (*RecallContext) Error

func (c *RecallContext) Error() error

Error 返回错误

func (*RecallContext) Get

func (c *RecallContext) Get(key string) (any, bool)

Get 获取元数据

func (*RecallContext) GetTokenUsage

func (c *RecallContext) GetTokenUsage(actionName string) TokenUsage

GetTokenUsage 获取指定 action 的 token 使用量

func (*RecallContext) IsAborted

func (c *RecallContext) IsAborted() bool

IsAborted 返回链是否被终止

func (*RecallContext) Next

func (c *RecallContext) Next()

Next 调用链中的下一个 action

func (*RecallContext) Set

func (c *RecallContext) Set(key string, value any)

Set 存储元数据

func (*RecallContext) SetError

func (c *RecallContext) SetError(err error)

SetError 设置错误并终止链

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 使用量

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL