Help us shape the future of Go! Take the 2026 Go Developer Survey

knowledgebase

package
v1.71.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 28 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatHits

func FormatHits(query string, hits []Hit, maxContentRunes int) string

FormatHits 将检索结果格式化为工具返回文本。

func New

func New() *plugin.Descriptor

New 返回 knowledgebase 插件描述符。

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 Hit

type Hit struct {
	Source  string
	Heading string
	Content string
	Score   float32
}

Hit 一条检索命中。

type Plugin

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

Plugin 知识库插件实例(Setup 返回值,同时作为 toolkit.ToolProvider 注册给 AI 插件)。

func (*Plugin) Build

func (p *Plugin) Build(ctx context.Context, full bool) error

Build 扫描数据源并增量更新索引。 full=true 时清空后全量重建(/kb rebuild);否则只重建 hash 变化的文件。

func (*Plugin) ListTools

func (p *Plugin) ListTools() []toolkit.Tool

ListTools 返回可供 AI 调用的知识库工具。实现 toolkit.ToolProvider。

func (*Plugin) Search

func (p *Plugin) Search(ctx context.Context, query string, limit int) ([]Hit, error)

Search 检索知识库:embedding 可用时语义精排,否则关键词重叠兜底。

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 OpenStore

func OpenStore(path string) (*Store, error)

OpenStore 打开或创建知识库索引数据库,自动建表。

func (*Store) Clear

func (s *Store) Clear() error

Clear 清空全部索引(/kb rebuild 全量模式使用)。

func (*Store) Close

func (s *Store) Close() error

Close 关闭数据库连接。

func (*Store) CountChunks

func (s *Store) CountChunks() (int64, error)

CountChunks 返回分块总数。

func (*Store) LoadIndex

func (s *Store) LoadIndex() ([]indexedChunk, error)

LoadIndex 加载全部分块到内存(docs 规模 ~千级 chunk,内存占用可忽略)。

func (*Store) RemoveSource

func (s *Store) RemoveSource(source string) error

RemoveSource 删除已不在磁盘上的源文件及其分块。

func (*Store) ReplaceSource

func (s *Store) ReplaceSource(source, hash string, chunks []Chunk) error

ReplaceSource 原子替换一个源文件的全部分块并记录文件 hash。

func (*Store) SourceFiles

func (s *Store) SourceFiles() ([]SourceFile, error)

SourceFiles 返回全部源文件的索引状态。

Jump to

Keyboard shortcuts

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