(eid int64, entity *model.Entity)
| 39 | } |
| 40 | |
| 41 | func (s *EntityVectorService) IndexEntity(eid int64, entity *model.Entity) error { |
| 42 | if entity == nil || entity.ID <= 0 { |
| 43 | return fmt.Errorf("invalid entity") |
| 44 | } |
| 45 | if s.vectorDB == nil { |
| 46 | return nil |
| 47 | } |
| 48 | configService := NewChunkConfigService(s.db) |
| 49 | config, err := configService.GetConfig(eid, nil, model.ChunkTypeDefault) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | if config.EmbeddingChannelID == nil { |
| 54 | return fmt.Errorf("未配置向量化渠道") |
| 55 | } |
| 56 | channel, err := model.GetChannelByID(*config.EmbeddingChannelID) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | content := entity.Type + ":" + entity.Name |
| 61 | vector64, err := s.embedding.GenerateEmbedding(eid, content, channel, config, nil) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | vector32 := make([]float32, len(vector64)) |
| 66 | for i, v := range vector64 { |
| 67 | vector32[i] = float32(v) |
| 68 | } |
| 69 | metadata := map[string]interface{}{ |
| 70 | "entity_id": entity.ID, |
| 71 | "eid": eid, |
| 72 | "name": entity.Name, |
| 73 | "type": entity.Type, |
| 74 | "status": entity.Status, |
| 75 | "created_at": time.Now().Unix(), |
| 76 | } |
| 77 | record := vectorstore.VectorRecord{ |
| 78 | ID: entity.ID, |
| 79 | Vector: vector32, |
| 80 | Metadata: metadata, |
| 81 | } |
| 82 | collection := model.GetEntityVectorCollectionName(eid) |
| 83 | ctx := context.Background() |
| 84 | err = s.updateWithAutoCreate(ctx, collection, record, len(vector32)) |
| 85 | if err != nil { |
| 86 | logger.SysLogf("实体向量入库失败: eid=%d id=%d name=%s err=%v", eid, entity.ID, entity.Name, err) |
| 87 | return err |
| 88 | } |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (s *EntityVectorService) SearchEntities(eid int64, keyword string, topK int) ([]int64, error) { |
| 93 | if s.vectorDB == nil { |
no test coverage detected