SearchWithConfidenceFilter 执行检索并按置信度过滤。 minConfidence: 最低置信度阈值(0.0-1.0)
(ctx context.Context, query string, meta map[string]any, topK int, minConfidence float64)
| 236 | // SearchWithConfidenceFilter 执行检索并按置信度过滤。 |
| 237 | // minConfidence: 最低置信度阈值(0.0-1.0) |
| 238 | func (sm *SemanticMemory) SearchWithConfidenceFilter(ctx context.Context, query string, meta map[string]any, topK int, minConfidence float64) ([]vector.Hit, error) { |
| 239 | if !sm.cfg.EnableProvenance { |
| 240 | return sm.Search(ctx, query, meta, topK) |
| 241 | } |
| 242 | |
| 243 | // 先执行标准检索 |
| 244 | hits, err := sm.Search(ctx, query, meta, topK*2) // 获取更多结果以便过滤 |
| 245 | if err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | // 过滤和重排序 |
| 250 | var filtered []vector.Hit |
| 251 | for _, hit := range hits { |
| 252 | provenance := FromMetadata(hit.Metadata) |
| 253 | if provenance == nil { |
| 254 | continue |
| 255 | } |
| 256 | |
| 257 | // 更新置信度 |
| 258 | if sm.cfg.ConfidenceCalculator != nil { |
| 259 | sm.cfg.ConfidenceCalculator.UpdateConfidence(provenance) |
| 260 | } |
| 261 | |
| 262 | // 过滤低置信度 |
| 263 | if provenance.Confidence < minConfidence { |
| 264 | continue |
| 265 | } |
| 266 | |
| 267 | // 标记访问 |
| 268 | provenance.MarkAccessed() |
| 269 | |
| 270 | // 重新计算相关性得分(结合置信度) |
| 271 | if sm.cfg.ConfidenceCalculator != nil { |
| 272 | hit.Score = sm.cfg.ConfidenceCalculator.ScoreByRelevance(hit.Score, provenance) |
| 273 | } |
| 274 | |
| 275 | filtered = append(filtered, hit) |
| 276 | } |
| 277 | |
| 278 | // 限制返回数量 |
| 279 | if len(filtered) > topK { |
| 280 | filtered = filtered[:topK] |
| 281 | } |
| 282 | |
| 283 | return filtered, nil |
| 284 | } |
| 285 | |
| 286 | // SearchBySourceType 按来源类型检索记忆。 |
| 287 | func (sm *SemanticMemory) SearchBySourceType(ctx context.Context, query string, meta map[string]any, topK int, sourceTypes []SourceType) ([]vector.Hit, error) { |
nothing calls this directly
no test coverage detected