Retrieve 检索相关知识
(ctx context.Context, query string, options ...RAGOption)
| 112 | |
| 113 | // Retrieve 检索相关知识 |
| 114 | func (r *RAG) Retrieve(ctx context.Context, query string, options ...RAGOption) (*RAGResult, error) { |
| 115 | startTime := time.Now() |
| 116 | |
| 117 | // 应用选项 |
| 118 | opts := &RAGOptions{} |
| 119 | for _, opt := range options { |
| 120 | opt(opts) |
| 121 | } |
| 122 | |
| 123 | // 执行多源检索 |
| 124 | items, err := r.multiSourceRetrieval(ctx, query, opts) |
| 125 | if err != nil { |
| 126 | return nil, fmt.Errorf("knowledge: multi-source retrieval failed: %w", err) |
| 127 | } |
| 128 | |
| 129 | retrievalTime := time.Since(startTime) |
| 130 | processingStart := time.Now() |
| 131 | |
| 132 | // 重排序 |
| 133 | if r.config.Reranking { |
| 134 | items = r.rerank(ctx, query, items) |
| 135 | } |
| 136 | |
| 137 | // 应用过滤 |
| 138 | items = r.applyFilters(items, opts) |
| 139 | |
| 140 | // 生成上下文 |
| 141 | context, contextItems, err := r.generateContext(ctx, items, query) |
| 142 | if err != nil { |
| 143 | return nil, fmt.Errorf("knowledge: failed to generate context: %w", err) |
| 144 | } |
| 145 | |
| 146 | // 查询增强 |
| 147 | enhancedQuery := r.enhanceQuery(query, items) |
| 148 | |
| 149 | processingTime := time.Since(processingStart) |
| 150 | |
| 151 | return &RAGResult{ |
| 152 | Query: query, |
| 153 | Results: items, |
| 154 | EnhancedQuery: enhancedQuery, |
| 155 | Context: context, |
| 156 | ContextItems: contextItems, |
| 157 | Confidence: r.calculateConfidence(items), |
| 158 | RetrievalTime: retrievalTime, |
| 159 | ProcessingTime: processingTime, |
| 160 | TotalTime: time.Since(startTime), |
| 161 | }, nil |
| 162 | } |
| 163 | |
| 164 | // RetrieveWithMetadata 带元数据的检索 |
| 165 | func (r *RAG) RetrieveWithMetadata(ctx context.Context, query string, metadata map[string]any) (*RAGResult, error) { |
no test coverage detected