applyFilters 应用过滤器
(items []*RetrievalItem, opts *RAGOptions)
| 458 | |
| 459 | // applyFilters 应用过滤器 |
| 460 | func (r *RAG) applyFilters(items []*RetrievalItem, opts *RAGOptions) []*RetrievalItem { |
| 461 | var filtered []*RetrievalItem |
| 462 | |
| 463 | for _, item := range items { |
| 464 | // 相关性阈值过滤 |
| 465 | if item.Score < r.config.RelevanceThreshold { |
| 466 | continue |
| 467 | } |
| 468 | |
| 469 | // 质量过滤 |
| 470 | if r.config.EnableQualityFilter && opts.QualityFilter != nil { |
| 471 | if item.Item.Quality < opts.QualityFilter.MinQuality { |
| 472 | continue |
| 473 | } |
| 474 | if item.Item.Confidence < opts.QualityFilter.MinConfidence { |
| 475 | continue |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // 时效性过滤 |
| 480 | if r.config.EnableTemporalFilter && opts.TimeRange != nil { |
| 481 | if item.Item.CreatedAt.Before(opts.TimeRange.Start) || |
| 482 | item.Item.CreatedAt.After(opts.TimeRange.End) { |
| 483 | continue |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | filtered = append(filtered, item) |
| 488 | } |
| 489 | |
| 490 | return filtered |
| 491 | } |
| 492 | |
| 493 | // generateContext 生成上下文 |
| 494 | func (r *RAG) generateContext(_ context.Context, items []*RetrievalItem, _ string) (string, []*KnowledgeItem, error) { |