memory

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmbedderFunc

func EmbedderFunc(e Embedder) gorm.EmbeddingFunc

Types

type CompositeLongTermStore added in v1.1.0

type CompositeLongTermStore struct {
	// contains filtered or unexported fields
}

CompositeLongTermStore 组合 Store + Retriever 实现 LongTermStore

func NewCompositeLongTermStore added in v1.1.0

func NewCompositeLongTermStore(store Store, retriever Retriever) *CompositeLongTermStore

NewCompositeLongTermStore 创建组合存储

func (*CompositeLongTermStore) AddHook added in v1.1.0

func (c *CompositeLongTermStore) AddHook(hook StoreHook)

AddHook 添加存储事件钩子 当 Store 发生 Save/Clear 操作时,通知 Retriever 更新索引

func (*CompositeLongTermStore) AttachIndexer added in v1.1.0

func (c *CompositeLongTermStore) AttachIndexer(indexer Indexer)

AttachIndexer 关联向量索引管理器

func (*CompositeLongTermStore) ClearSession added in v1.1.0

func (c *CompositeLongTermStore) ClearSession(ctx context.Context, sessionID string) error

func (*CompositeLongTermStore) Close added in v1.1.0

func (c *CompositeLongTermStore) Close() error

func (*CompositeLongTermStore) GetRetriever added in v1.1.0

func (c *CompositeLongTermStore) GetRetriever() Retriever

GetRetriever 获取底层 Retriever

func (*CompositeLongTermStore) GetSession added in v1.1.0

func (c *CompositeLongTermStore) GetSession(ctx context.Context, sessionID string) ([]*schema.Message, error)

func (*CompositeLongTermStore) GetStore added in v1.1.0

func (c *CompositeLongTermStore) GetStore() Store

GetStore 获取底层 Store

func (*CompositeLongTermStore) IndexReady added in v1.1.0

func (c *CompositeLongTermStore) IndexReady() bool

IndexReady 返回向量索引是否就绪(无 indexer 时始终返回 true)

func (*CompositeLongTermStore) Recall added in v1.1.0

func (c *CompositeLongTermStore) Recall(ctx context.Context, sessionID string, query string, topK int) ([]*schema.Message, error)

func (*CompositeLongTermStore) Save added in v1.1.0

func (c *CompositeLongTermStore) Save(ctx context.Context, sessionID string, msgs []*schema.Message) error

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller 记忆控制器,管理三类记忆

func NewController

func NewController(systemPrompt []*schema.Message, shortMemory ShortMemoryManager, opts ...Option) *Controller

NewController 创建记忆控制器(保持向后兼容)

func (*Controller) AddSystemPrompt added in v1.1.0

func (c *Controller) AddSystemPrompt(msgs ...*schema.Message)

AddSystemPrompt 追加系统提示词

func (*Controller) BuildContext

func (c *Controller) BuildContext(ctx context.Context, sessionID string, currentQuery string) ([]*schema.Message, error)

BuildContext 构建带记忆的上文

func (*Controller) Clear

func (c *Controller) Clear(ctx context.Context, sessionID string) error

Clear 清空会话

func (*Controller) Close

func (c *Controller) Close() error

Close 释放资源

func (*Controller) GetHistory

func (c *Controller) GetHistory(ctx context.Context, sessionID string) ([]*schema.Message, error)

GetHistory 获取完整历史

func (*Controller) GetShortMemory added in v1.1.0

func (c *Controller) GetShortMemory() ShortMemoryManager

GetShortMemory 获取短期记忆管理器

func (*Controller) SaveTurn

func (c *Controller) SaveTurn(ctx context.Context, sessionID string, msgs []*schema.Message) error

SaveTurn 保存一轮对话(user + assistant)

type DocumentStore added in v1.1.0

type DocumentStore interface {
	// SaveDocuments 批量保存文档
	SaveDocuments(ctx context.Context, collection string, docs []*schema.Document) error

	// RecallDocuments 根据查询召回相关文档
	RecallDocuments(ctx context.Context, collection string, query string, topK int) ([]*schema.Document, error)

	// GetDocuments 获取集合中的所有文档
	GetDocuments(ctx context.Context, collection string) ([]*schema.Document, error)

	// DeleteCollection 删除整个集合
	DeleteCollection(ctx context.Context, collection string) error

	// Close 关闭存储
	Close() error
}

DocumentStore 通用文档存储接口 用于非对话型 Agent 场景:用户画像 Agent、企业管理 Agent 等

type Embedder

type Embedder interface {
	Embed(ctx context.Context, text string) ([]float32, error)
}

Embedder 文本转向量接口

type EmbeddingFunc

type EmbeddingFunc func(ctx context.Context, text string) ([]float32, error)

EmbeddingFunc 文本转向量函数

type Indexer added in v1.1.0

type Indexer interface {
	// AddToIndex 将消息的嵌入向量添加到索引
	AddToIndex(ctx context.Context, sessionID string, msgs []*schema.Message) error

	// RemoveFromIndex 从索引中移除指定会话的所有向量
	RemoveFromIndex(sessionID string) error

	// RebuildIndex 从存储中重建完整索引
	RebuildIndex()

	// IndexReady 返回索引是否就绪
	IndexReady() bool
}

Indexer 向量索引管理接口 允许外部通知检索器更新索引(新增/删除向量、重建索引)

type LongTermStore

type LongTermStore interface {
	Store
	Retriever
}

LongTermStore 记忆存储接口(组合 Store + Retriever)

type MessageRecord

type MessageRecord struct {
	ID        string            `json:"id"`
	SessionID string            `json:"session_id"`
	Role      string            `json:"role"`
	Content   string            `json:"content"`
	Embedding []float32         `json:"embedding,omitempty"`
	Timestamp time.Time         `json:"timestamp"`
	Metadata  map[string]string `json:"metadata,omitempty"`
}

MessageRecord 存储的记录结构

type Option added in v1.1.0

type Option func(*Controller)

Option Controller 配置选项

func WithLongStore added in v1.1.0

func WithLongStore(store LongTermStore) Option

WithLongStore 设置长期记忆存储器

func WithTopK added in v1.1.0

func WithTopK(k int) Option

WithTopK 设置长期记忆召回数量

type Retriever added in v1.1.0

type Retriever interface {
	// Recall 根据查询召回相关消息
	Recall(ctx context.Context, sessionID string, query string, topK int) ([]*schema.Message, error)

	// Close 关闭检索器
	Close() error
}

Retriever 消息检索接口 负责根据查询召回相关消息,不关心存储细节

type ShortMemoryManager

type ShortMemoryManager interface {
	// GetRecent 返回当前窗口内的完整消息(不进行压缩)
	GetRecent(sessionID string) []*schema.Message

	// GetContextMessages 返回构建上下文所需的短期记忆消息
	// 内部可能整合:窗口内的完整消息 + 超出部分的摘要
	GetContextMessages(sessionID string) []*schema.Message

	// AddTurn 添加一轮对话,触发窗口维护、摘要生成等
	AddTurn(sessionID string, msgs []*schema.Message)

	// Clear 清空会话短期记忆
	Clear(sessionID string)
}

ShortMemoryManager 短期记忆管理器

type Store added in v1.1.0

type Store interface {
	// Save 保存消息
	Save(ctx context.Context, sessionID string, msgs []*schema.Message) error

	// GetSession 获取完整会话历史
	GetSession(ctx context.Context, sessionID string) ([]*schema.Message, error)

	// ClearSession 清空会话
	ClearSession(ctx context.Context, sessionID string) error

	// Close 关闭存储
	Close() error
}

Store 纯消息存储接口 负责消息的持久化,不关心检索逻辑

type StoreEvent added in v1.1.0

type StoreEvent struct {
	Type      StoreEventType
	SessionID string
	Messages  []*schema.Message // Save 事件时有值
}

StoreEvent 存储事件

type StoreEventType added in v1.1.0

type StoreEventType int

StoreEventType 存储事件类型

const (
	StoreEventSave  StoreEventType = iota // 新消息保存
	StoreEventClear                       // 会话清空
)

type StoreHook added in v1.1.0

type StoreHook func(event StoreEvent)

StoreHook 存储事件回调

Directories

Path Synopsis
Package embedder 提供文本转向量的实现 支持 OpenAI 兼容 API 和 Ollama 原生 API
Package embedder 提供文本转向量的实现 支持 OpenAI 兼容 API 和 Ollama 原生 API
Package milvus 提供基于 Milvus 的消息检索
Package milvus 提供基于 Milvus 的消息检索
Package redis 提供基于 RediSearch 的消息检索 依赖 Redis Stack(>= 7.0)的 RediSearch 模块
Package redis 提供基于 RediSearch 的消息检索 依赖 Redis Stack(>= 7.0)的 RediSearch 模块

Jump to

Keyboard shortcuts

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