SearchSimilar 搜索相似知识
(ctx context.Context, id string, maxResults int)
| 518 | |
| 519 | // SearchSimilar 搜索相似知识 |
| 520 | func (m *manager) SearchSimilar(ctx context.Context, id string, maxResults int) ([]*SearchResult, error) { |
| 521 | item, err := m.Get(ctx, id) |
| 522 | if err != nil { |
| 523 | return nil, fmt.Errorf("knowledge: failed to get item for similarity search: %w", err) |
| 524 | } |
| 525 | |
| 526 | if len(item.Embedding) == 0 && m.corePipeline == nil { |
| 527 | return nil, errors.New("knowledge: item has no embedding for similarity search") |
| 528 | } |
| 529 | |
| 530 | // 优先使用原有向量 |
| 531 | if len(item.Embedding) > 0 { |
| 532 | query := &SearchQuery{ |
| 533 | Vector: item.Embedding, |
| 534 | Strategy: StrategyVector, |
| 535 | Namespace: item.Namespace, |
| 536 | MaxResults: maxResults, |
| 537 | } |
| 538 | return m.Search(ctx, query) |
| 539 | } |
| 540 | |
| 541 | // 回退到核心管线的向量检索(使用文本重新嵌入) |
| 542 | if m.corePipeline != nil { |
| 543 | hits, err := m.corePipeline.Search(ctx, item.Content, maxResults, map[string]any{ |
| 544 | "namespace": item.Namespace, |
| 545 | }) |
| 546 | if err != nil { |
| 547 | return nil, fmt.Errorf("knowledge: core similarity search failed: %w", err) |
| 548 | } |
| 549 | return convertCoreHits(hits), nil |
| 550 | } |
| 551 | |
| 552 | return nil, errors.New("knowledge: no embedding available for similarity search") |
| 553 | } |
| 554 | |
| 555 | // AddRelation 添加知识关系 |
| 556 | func (m *manager) AddRelation(ctx context.Context, fromID, toID string, relationType RelationType, weight float64, label string) error { |
nothing calls this directly
no test coverage detected