Documentation
¶
Overview ¶
Package knowledgebase 提供本地文档知识库检索工具。
数据源:本地 Markdown 文档目录(默认 docs/,排除 notes/), 启动时增量扫描建索引,分块 embedding 后持久化到 SQLite (data/db/knowledgebase.db),查询时按需语义检索。
对外能力:
- kb_search / kb_stats:注册给 AI 插件(ToolProvider),LLM 按需调用
- /kb rebuild / /kb status:超管管理命令
Index ¶
- func FormatHits(query string, hits []Hit, maxContentRunes int) string
- func New() *plugin.Descriptor
- type Chunk
- type Config
- type Hit
- type Plugin
- type SourceFile
- type Store
- func (s *Store) Clear() error
- func (s *Store) Close() error
- func (s *Store) CountChunks() (int64, error)
- func (s *Store) LoadIndex() ([]indexedChunk, error)
- func (s *Store) RemoveSource(source string) error
- func (s *Store) ReplaceSource(source, hash string, chunks []Chunk) error
- func (s *Store) SourceFiles() ([]SourceFile, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatHits ¶
FormatHits 将检索结果格式化为工具返回文本。
Types ¶
type Chunk ¶
type Chunk struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
Source string `gorm:"index"` // 源文件相对路径,如 06-plugins/COMMAND_SYSTEM.md
Heading string // 所属 Markdown 标题(检索展示用)
Content string `gorm:"type:text"`
Hash string `gorm:"index"` // content sha256(增量更新检测)
Vector []byte // float32 LE 序列;空表示未嵌入
Dim int // 向量维度
Model string // 生成向量的模型名
UpdatedAt int64
}
Chunk 知识库分块(SQLite 持久化,向量以 float32 LE 字节流存储)。
type Config ¶
type Config struct {
// Enabled 是否启用知识库检索(默认 false)。
Enabled bool `yaml:"enabled"`
// Markdown 平台支持 MD 时用 Markdown 回复(/kb status、/kb rebuild 等),
// 否则回退纯文本(默认 true,与 ai 插件一致)。
Markdown bool `yaml:"markdown"`
// SourceDir 数据源根目录(相对工作目录,默认 docs)。
SourceDir string `yaml:"source_dir"`
// ExcludeDirs 扫描时排除的子目录(默认 ["notes"]:设计笔记多为历史/计划内容)。
ExcludeDirs []string `yaml:"exclude_dirs"`
// DBPath 索引持久化路径(默认 data/db/knowledgebase.db)。
DBPath string `yaml:"db_path"`
// EmbeddingBaseURL OpenAI 兼容 /embeddings 端点根地址(如 http://host:8080/v1)。
// 为空时禁用语义检索,仅使用关键词兜底。
EmbeddingBaseURL string `yaml:"embedding_base_url"`
// EmbeddingAPIKey 嵌入服务 API Key(自托管服务通常为空)。
EmbeddingAPIKey string `yaml:"embedding_api_key"`
// EmbeddingModel 嵌入模型名。
EmbeddingModel string `yaml:"embedding_model"`
// ChunkSize 分块目标长度(rune,默认 600)。
ChunkSize int `yaml:"chunk_size"`
// ChunkOverlap 相邻分块重叠长度(rune,默认 100)。
ChunkOverlap int `yaml:"chunk_overlap"`
// MaxResults kb_search 单次返回条数上限(默认 5)。
MaxResults int `yaml:"max_results"`
// ToolDescription kb_search 工具的自定义描述(告诉 LLM 知识库包含什么内容)。
// 为空时使用通用描述(自动带上数据源目录)。
ToolDescription string `yaml:"tool_description"`
}
Config knowledgebase 插件配置(config.yaml plugins.knowledgebase 节)。
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin 知识库插件实例(Setup 返回值,同时作为 toolkit.ToolProvider 注册给 AI 插件)。
type SourceFile ¶
type SourceFile struct {
Source string `gorm:"primaryKey"`
Hash string // 文件内容 sha256
ChunkCount int
UpdatedAt int64
}
SourceFile 源文件索引状态(文件 hash 变化时增量重建)。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store SQLite 索引存储。
func (*Store) RemoveSource ¶
RemoveSource 删除已不在磁盘上的源文件及其分块。
func (*Store) ReplaceSource ¶
ReplaceSource 原子替换一个源文件的全部分块并记录文件 hash。
func (*Store) SourceFiles ¶
func (s *Store) SourceFiles() ([]SourceFile, error)
SourceFiles 返回全部源文件的索引状态。
Click to show internal directories.
Click to hide internal directories.