vector

package
v0.0.0-...-a4f33a8 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

向量存储抽象层

本模块提供了向量存储的抽象接口,支持多种向量数据库实现。

支持的向量存储

  • Elasticsearch (ES)
  • Qdrant

配置说明

使用 Elasticsearch
vector:
  type: "es"  # 或 "elasticsearch"
  indexName: "rag-test"
  es:
    address: "http://elasticsearch:9200"
    username: "elastic"  # 可选
    password: "123456"   # 可选
使用 Qdrant
vector:
  type: "qdrant"
  indexName: "rag-test"
  qdrant:
    address: "http://qdrant:6333"
    apiKey: ""  # 可选,如果需要认证

接口说明

VectorStore 接口
type VectorStore interface {
    // 创建索引/集合
    CreateIndex(ctx context.Context, indexName string) error
    
    // 检查索引/集合是否存在
    IndexExists(ctx context.Context, indexName string) (bool, error)
    
    // 删除文档
    DeleteDocument(ctx context.Context, indexName, documentID string) error
    
    // 获取知识库列表
    GetKnowledgeBaseList(ctx context.Context, indexName string) ([]string, error)
    
    // 搜索文档
    SearchDocuments(ctx context.Context, req *SearchRequest) (*SearchResponse, error)
    
    // 关闭连接
    Close() error
}

实现新的向量存储

要添加新的向量存储实现:

  1. vector 包中创建新文件(如 milvus.go
  2. 实现 VectorStore 接口
  3. factory.go 中添加新类型的支持
  4. 更新配置结构

迁移说明

从旧配置迁移

旧配置格式:

es:
  address: "http://elasticsearch:9200"
  indexName: "rag-test"
  username: "elastic"
  password: "123456"

新配置格式:

vector:
  type: "es"
  indexName: "rag-test"
  es:
    address: "http://elasticsearch:9200"
    username: "elastic"
    password: "123456"
代码迁移

旧代码:

err := common.CreateIndexIfNotExists(ctx, client, indexName)
err := common.DeleteDocument(ctx, client, documentID)

新代码:

exists, err := vectorStore.IndexExists(ctx, indexName)
if !exists {
    err = vectorStore.CreateIndex(ctx, indexName)
}
err := vectorStore.DeleteDocument(ctx, indexName, documentID)

TODO

  • 添加单元测试
  • 添加 Milvus 支持
  • 添加 Pinecone 支持

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Type      string        // 类型:es 或 qdrant
	IndexName string        // 索引名称
	ES        *ESConfig     // ES 配置
	Qdrant    *QdrantConfig // Qdrant 配置
}

Config 向量存储配置

type ESConfig

type ESConfig struct {
	Address  string
	Username string
	Password string
}

ESConfig Elasticsearch 配置

type ESVectorStore

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

ESVectorStore Elasticsearch 向量存储实现

func NewESVectorStore

func NewESVectorStore(cfg *ESConfig) (*ESVectorStore, error)

NewESVectorStore 创建 ES 向量存储实例

func (*ESVectorStore) Close

func (s *ESVectorStore) Close() error

Close 关闭连接

func (*ESVectorStore) CreateIndex

func (s *ESVectorStore) CreateIndex(ctx context.Context, indexName string) error

CreateIndex 创建索引

func (*ESVectorStore) DeleteDocument

func (s *ESVectorStore) DeleteDocument(ctx context.Context, indexName, documentID string) error

DeleteDocument 删除文档

func (*ESVectorStore) GetClient

func (s *ESVectorStore) GetClient() *elasticsearch.Client

GetClient 获取 ES 客户端(用于兼容现有代码)

func (*ESVectorStore) GetKnowledgeBaseList

func (s *ESVectorStore) GetKnowledgeBaseList(ctx context.Context, indexName string) ([]string, error)

GetKnowledgeBaseList 获取知识库列表

func (*ESVectorStore) IndexExists

func (s *ESVectorStore) IndexExists(ctx context.Context, indexName string) (bool, error)

IndexExists 检查索引是否存在

func (*ESVectorStore) SearchDocuments

func (s *ESVectorStore) SearchDocuments(ctx context.Context, req *SearchRequest) (*SearchResponse, error)

SearchDocuments 搜索文档

type QdrantConfig

type QdrantConfig struct {
	Address string
	Port    int
	APIKey  string
}

QdrantConfig Qdrant 配置

type QdrantSearchQuery

type QdrantSearchQuery struct {
	CollectionName string
	Query          *qdrant.Query
}

QdrantSearchQuery Qdrant 搜索查询

type QdrantVectorStore

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

QdrantVectorStore Qdrant 向量存储实现

func NewQdrantVectorStore

func NewQdrantVectorStore(cfg *QdrantConfig) (*QdrantVectorStore, error)

NewQdrantVectorStore 创建 Qdrant 向量存储实例

func (*QdrantVectorStore) Close

func (s *QdrantVectorStore) Close() error

Close 关闭连接

func (*QdrantVectorStore) CreateIndex

func (s *QdrantVectorStore) CreateIndex(ctx context.Context, indexName string) error

CreateIndex 创建集合

func (*QdrantVectorStore) DeleteDocument

func (s *QdrantVectorStore) DeleteDocument(ctx context.Context, indexName, documentID string) error

DeleteDocument 删除文档

func (*QdrantVectorStore) GetClient

func (s *QdrantVectorStore) GetClient() *qdrant.Client

GetClient 获取 Qdrant 客户端(用于兼容现有代码)

func (*QdrantVectorStore) GetKnowledgeBaseList

func (s *QdrantVectorStore) GetKnowledgeBaseList(ctx context.Context, indexName string) ([]string, error)

GetKnowledgeBaseList 获取知识库列表

func (*QdrantVectorStore) IndexExists

func (s *QdrantVectorStore) IndexExists(ctx context.Context, indexName string) (bool, error)

IndexExists 检查集合是否存在

func (*QdrantVectorStore) SearchDocuments

func (s *QdrantVectorStore) SearchDocuments(ctx context.Context, req *SearchRequest) (*SearchResponse, error)

SearchDocuments 搜索文档

type SearchRequest

type SearchRequest struct {
	IndexName     string
	Query         interface{} // 查询条件,不同实现可能不同
	Size          int
	KnowledgeName string
	DocIDs        []string
}

SearchRequest 搜索请求

type SearchResponse

type SearchResponse struct {
	Documents []*schema.Document
	Total     int64
}

SearchResponse 搜索响应

type VectorStore

type VectorStore interface {
	// CreateIndex 创建索引
	CreateIndex(ctx context.Context, indexName string) error

	// IndexExists 检查索引是否存在
	IndexExists(ctx context.Context, indexName string) (bool, error)

	// DeleteDocument 删除文档
	DeleteDocument(ctx context.Context, indexName, documentID string) error

	// GetKnowledgeBaseList 获取知识库列表
	GetKnowledgeBaseList(ctx context.Context, indexName string) ([]string, error)

	// SearchDocuments 搜索文档
	SearchDocuments(ctx context.Context, req *SearchRequest) (*SearchResponse, error)

	// Close 关闭连接
	Close() error
}

VectorStore 向量存储接口

func NewVectorStore

func NewVectorStore(cfg *Config) (VectorStore, error)

NewVectorStore 创建向量存储实例

Jump to

Keyboard shortcuts

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