runRAGPipeline runs retrieve + rerank for request_type=rag. req is updated in place (merged chunks).
(ctx context.Context, sw *execution.StepWriter, req *types.AIRequest)
| 26 | |
| 27 | // runRAGPipeline runs retrieve + rerank for request_type=rag. req is updated in place (merged chunks). |
| 28 | func (s *Server) runRAGPipeline(ctx context.Context, sw *execution.StepWriter, req *types.AIRequest) error { |
| 29 | kb := kbName(*req, s.cfg) |
| 30 | if kb == "" || len(s.retrieval) == 0 { |
| 31 | return &ragPipelineError{trace: "rag_not_configured", httpStatus: http.StatusBadRequest, errCode: errCodeRAGNotConfigured, msg: "configure knowledge_bases and set context.knowledge_base or use the first KB"} |
| 32 | } |
| 33 | ad, ok := s.retrieval[kb] |
| 34 | if !ok { |
| 35 | return &ragPipelineError{trace: "rag_not_configured", httpStatus: http.StatusBadRequest, errCode: errCodeRAGNotConfigured, msg: "unknown knowledge_base " + kb} |
| 36 | } |
| 37 | q := ragQuery(*req) |
| 38 | if q == "" { |
| 39 | return &ragPipelineError{trace: "rag_not_configured", httpStatus: http.StatusBadRequest, errCode: errCodeInvalidRequest, msg: "rag requires input.text or context.query"} |
| 40 | } |
| 41 | var retrieveRes types.RetrievalResult |
| 42 | err := sw.Run(ctx, execution.StepRetrieve, ad.Name(), map[string]any{"query": q, "kb": kb}, func() (map[string]any, error) { |
| 43 | res, e := ad.Retrieve(ctx, q, nil) |
| 44 | if e != nil { |
| 45 | return nil, e |
| 46 | } |
| 47 | retrieveRes = res |
| 48 | return map[string]any{"chunks": len(res.Chunks)}, nil |
| 49 | }) |
| 50 | if err != nil { |
| 51 | return &ragPipelineError{trace: "retrieve_failed", httpStatus: http.StatusBadGateway, errCode: errCodeExecutionFailed, msg: err.Error()} |
| 52 | } |
| 53 | err = sw.Run(ctx, execution.StepRerank, s.rerank.Name(), map[string]any{"query": q, "chunks_in": len(retrieveRes.Chunks)}, func() (map[string]any, error) { |
| 54 | out, e := s.rerank.Rerank(ctx, q, retrieveRes.Chunks, nil) |
| 55 | if e != nil { |
| 56 | return nil, e |
| 57 | } |
| 58 | retrieval.MergeRetrievalIntoInput(req, out) |
| 59 | return map[string]any{"chunks_out": len(out.Chunks)}, nil |
| 60 | }) |
| 61 | if err != nil { |
| 62 | return &ragPipelineError{trace: "rerank_failed", httpStatus: http.StatusBadGateway, errCode: errCodeExecutionFailed, msg: err.Error()} |
| 63 | } |
| 64 | return nil |
| 65 | } |
no test coverage detected