vector_store.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package rag
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "github.com/2930134478/AI-CS/backend/infra"
  8. )
  9. // ErrVectorStoreUnavailable 向量库未启用或未连接(写入/索引前会返回该错误)。
  10. var ErrVectorStoreUnavailable = errors.New("向量数据库未启用或未连接")
  11. // VectorStoreService 向量存储服务(业务层)
  12. type VectorStoreService struct {
  13. vectorStore *infra.VectorStore
  14. }
  15. // NewVectorStoreService 创建向量存储服务实例(vectorStore 可为 nil,表示无向量库降级模式)。
  16. func NewVectorStoreService(vectorStore *infra.VectorStore) *VectorStoreService {
  17. return &VectorStoreService{
  18. vectorStore: vectorStore,
  19. }
  20. }
  21. // IsAvailable 当前是否已连接可用的 Milvus 向量存储。
  22. func (s *VectorStoreService) IsAvailable() bool {
  23. return s != nil && s.vectorStore != nil
  24. }
  25. // UpsertVector 插入或更新单个向量
  26. func (s *VectorStoreService) UpsertVector(ctx context.Context, documentID string, knowledgeBaseID string, content string, vector []float32) error {
  27. if s.vectorStore == nil {
  28. return ErrVectorStoreUnavailable
  29. }
  30. return s.vectorStore.UpsertVector(ctx, documentID, knowledgeBaseID, content, vector)
  31. }
  32. // UpsertVectors 批量插入或更新向量
  33. func (s *VectorStoreService) UpsertVectors(ctx context.Context, documentIDs []string, knowledgeBaseIDs []string, contents []string, vectors [][]float32) error {
  34. if s.vectorStore == nil {
  35. return ErrVectorStoreUnavailable
  36. }
  37. return s.vectorStore.UpsertVectors(ctx, documentIDs, knowledgeBaseIDs, contents, vectors)
  38. }
  39. // SearchVectors 搜索相似向量
  40. func (s *VectorStoreService) SearchVectors(ctx context.Context, queryVector []float32, topK int, knowledgeBaseID *string) ([]SearchResult, error) {
  41. if s.vectorStore == nil {
  42. return []SearchResult{}, nil
  43. }
  44. results, err := s.vectorStore.SearchVectors(ctx, queryVector, topK, knowledgeBaseID)
  45. if err != nil {
  46. return nil, fmt.Errorf("向量检索失败: %w", err)
  47. }
  48. // 转换结果
  49. searchResults := make([]SearchResult, len(results))
  50. for i, r := range results {
  51. searchResults[i] = SearchResult{
  52. DocumentID: r.DocumentID,
  53. KnowledgeBaseID: r.KnowledgeBaseID,
  54. Content: r.Content,
  55. Score: r.Score,
  56. }
  57. }
  58. return searchResults, nil
  59. }
  60. // DeleteVector 删除向量
  61. func (s *VectorStoreService) DeleteVector(ctx context.Context, documentID string) error {
  62. if s.vectorStore == nil {
  63. return nil
  64. }
  65. return s.vectorStore.DeleteVector(ctx, documentID)
  66. }
  67. // DeleteVectors 批量删除向量
  68. func (s *VectorStoreService) DeleteVectors(ctx context.Context, documentIDs []string) error {
  69. if s.vectorStore == nil {
  70. return nil
  71. }
  72. return s.vectorStore.DeleteVectors(ctx, documentIDs)
  73. }
  74. // ConvertDocumentID 将 uint 转换为 string
  75. func ConvertDocumentID(id uint) string {
  76. return strconv.FormatUint(uint64(id), 10)
  77. }
  78. // ConvertKnowledgeBaseID 将 uint 转换为 string
  79. func ConvertKnowledgeBaseID(id uint) string {
  80. return strconv.FormatUint(uint64(id), 10)
  81. }