indexDB

apigo.cc/go/indexDB 提供了统一的混合检索引擎,结合了基于 bleve 的全文检索和基于 chromem-go 的向量检索。

注意: 启用向量检索需要传入 embedding 外部回调函数,这会增加额外的内存和计算开销,请在必要时使用。

特性

  • 全文+向量 混合检索内置倒数排序融合RRF进行评分合并。
  • 无状态依赖,仅接收和存储数据。不绑定特定的 LLM 或业务模型。
  • 傻瓜化检索 API,支持多种复杂条件 ConditionidPrefix
  • 平滑重建,支持从 Bleve 重新导出数据生成全新的全文本及向量数据库,确保分词模型更改后能平滑过渡。
  • 细粒度权限控制,在引擎层进行系统和用户级别的视图隔离。

安装

go get apigo.cc/go/indexDB

使用示例

1. 初始化引擎

package main

import (
	"log"
	"apigo.cc/go/indexDB"
)

func mockEmbedding(text string) ([]float32, error) {
	// ... 请求大模型获取向量
	return []float32{0.1, 0.2, 0.3}, nil
}

func main() {
    // 若不传入 embedding 函数,则仅使用全文检索
	dbUnauth, err := indexDB.GetDB("./data_dir", mockEmbedding, log.Default())
	if err != nil {
		panic(err)
	}

    // 绑定系统管理员或特定用户权限
	db := dbUnauth.Auth(indexDB.SystemUserID) // 获取全部权限

    // 2. 添加数据
    db.Add("doc1", "这是一段测试文本", map[string]any{"source": "test"}, []string{"user1"})

    // 3. 混合检索
    filter := []indexDB.Condition{
        {Field: "source", Operator: "eq", Value: "test"},
    }
    results, err := db.Search("", "测试", 10, filter)
    for _, r := range results {
        log.Println("ID:", r.ID, "Score:", r.Score)
    }

    // 4. 重建索引
    db.RebuildAll()
}

API 指南

  • GetDB(indexDBPath string, embedding func(string) ([]float32, error), logger *log.Logger) (*IndexDBUnauthorized, error): 获取引擎的非授权实例。
  • (*IndexDBUnauthorized) Auth(userId string) *IndexDB: 获得特定用户的授权实例。
  • (*IndexDB) Add(id string, text string, metadata map[string]any, allowUsers []string) error: 将数据添加到引擎,触发回调写入向量。
  • (*IndexDB) Remove(id string) error: 从引擎中删除。
  • (*IndexDB) Search(idPrefix string, query string, topK int, filter []Condition) ([]SearchResult, error): 混合检索接口。
  • (*IndexDB) RebuildAll() error: 根据旧索引生成全新的版本以适用新的分词或模型算法。
Description
No description provided
Readme 7.3 MiB
Languages
Go 100%