(ctx context.Context, cfg *config.Config, req *types.AIRequest, ret map[string]interfaces.RetrievalAdapter, rerank interfaces.RerankAdapter)
| 180 | } |
| 181 | |
| 182 | func runRAGRetrieve(ctx context.Context, cfg *config.Config, req *types.AIRequest, ret map[string]interfaces.RetrievalAdapter, rerank interfaces.RerankAdapter) error { |
| 183 | kb := "" |
| 184 | if req.Context != nil { |
| 185 | if v, ok := req.Context["knowledge_base"].(string); ok { |
| 186 | kb = strings.TrimSpace(v) |
| 187 | } |
| 188 | } |
| 189 | if kb == "" && cfg != nil && len(cfg.KnowledgeBases) > 0 { |
| 190 | kb = cfg.KnowledgeBases[0].Name |
| 191 | } |
| 192 | ad, ok := ret[kb] |
| 193 | if !ok || kb == "" { |
| 194 | return errors.New("rag replay: knowledge base not available") |
| 195 | } |
| 196 | q := "" |
| 197 | if req.Context != nil { |
| 198 | if v, ok := req.Context["query"].(string); ok { |
| 199 | q = strings.TrimSpace(v) |
| 200 | } |
| 201 | } |
| 202 | if q == "" && req.Input != nil { |
| 203 | if t, ok := req.Input["text"].(string); ok { |
| 204 | q = strings.TrimSpace(t) |
| 205 | } |
| 206 | } |
| 207 | if q == "" { |
| 208 | return errors.New("rag replay: missing query") |
| 209 | } |
| 210 | res, err := ad.Retrieve(ctx, q, nil) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | if rerank == nil { |
| 215 | rerank = retrieval.NewRerankFromConfig(nil) |
| 216 | } |
| 217 | out, err := rerank.Rerank(ctx, q, res.Chunks, nil) |
| 218 | if err != nil { |
| 219 | return err |
| 220 | } |
| 221 | retrieval.MergeRetrievalIntoInput(req, out) |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | func buildResponse(cfg *config.Config, req types.AIRequest, policyReason string, primary types.RouteDecision, exec types.ExecutionResult, requestID, traceID string) types.AIResponse { |
| 226 | chosenBackendCfg, _ := cfg.BackendByName(exec.BackendName) |
no test coverage detected