SearchBySourceType 按来源类型检索记忆。
(ctx context.Context, query string, meta map[string]any, topK int, sourceTypes []SourceType)
| 285 | |
| 286 | // SearchBySourceType 按来源类型检索记忆。 |
| 287 | func (sm *SemanticMemory) SearchBySourceType(ctx context.Context, query string, meta map[string]any, topK int, sourceTypes []SourceType) ([]vector.Hit, error) { |
| 288 | if !sm.cfg.EnableProvenance { |
| 289 | return sm.Search(ctx, query, meta, topK) |
| 290 | } |
| 291 | |
| 292 | // 先执行标准检索 |
| 293 | hits, err := sm.Search(ctx, query, meta, topK*2) |
| 294 | if err != nil { |
| 295 | return nil, err |
| 296 | } |
| 297 | |
| 298 | // 按来源类型过滤 |
| 299 | sourceTypeMap := make(map[SourceType]bool) |
| 300 | for _, st := range sourceTypes { |
| 301 | sourceTypeMap[st] = true |
| 302 | } |
| 303 | |
| 304 | var filtered []vector.Hit |
| 305 | for _, hit := range hits { |
| 306 | provenance := FromMetadata(hit.Metadata) |
| 307 | if provenance == nil { |
| 308 | continue |
| 309 | } |
| 310 | |
| 311 | if sourceTypeMap[provenance.SourceType] { |
| 312 | filtered = append(filtered, hit) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // 限制返回数量 |
| 317 | if len(filtered) > topK { |
| 318 | filtered = filtered[:topK] |
| 319 | } |
| 320 | |
| 321 | return filtered, nil |
| 322 | } |
| 323 | |
| 324 | // PruneMemories 剪枝(删除)低置信度记忆。 |
| 325 | // 返回被删除的记忆ID列表。 |
nothing calls this directly
no test coverage detected