WorkflowDemoRunEvalHandler 返回一个 HTTP handler, 用于运行内置 demo 工作流并对最终回答进行评估。 路径示例: POST /v1/workflows/demo/run-eval 请求体: { "workflow_id": "sequential_demo", "input": "处理用户数据", "reference": "期望的总结结果...", "keywords": ["收集", "分析", "报告"], "scorers": ["keyword_coverage", "lexical_similar
()
| 275 | // ] |
| 276 | // } |
| 277 | func (s *Server) WorkflowDemoRunEvalHandler() http.Handler { |
| 278 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 279 | if r.Method != http.MethodPost { |
| 280 | http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | var req WorkflowRunEvalRequest |
| 285 | if err := jsonNewDecoder(r.Body).Decode(&req); err != nil { |
| 286 | http.Error(w, "invalid JSON body", http.StatusBadRequest) |
| 287 | return |
| 288 | } |
| 289 | |
| 290 | if req.WorkflowID == "" { |
| 291 | http.Error(w, "workflow_id is required", http.StatusBadRequest) |
| 292 | return |
| 293 | } |
| 294 | if req.Input == "" { |
| 295 | http.Error(w, "input is required", http.StatusBadRequest) |
| 296 | return |
| 297 | } |
| 298 | |
| 299 | // 验证 scorers 列表 |
| 300 | scorerNames := req.Scorers |
| 301 | if len(scorerNames) == 0 { |
| 302 | scorerNames = []string{"keyword_coverage", "lexical_similarity"} |
| 303 | } |
| 304 | for _, name := range scorerNames { |
| 305 | if name != "keyword_coverage" && name != "lexical_similarity" { |
| 306 | http.Error(w, "unsupported scorer: "+name, http.StatusBadRequest) |
| 307 | return |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | ctx := r.Context() |
| 312 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 313 | defer cancel() |
| 314 | |
| 315 | wf, err := s.buildDemoWorkflow(req.WorkflowID) |
| 316 | if err != nil { |
| 317 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 318 | return |
| 319 | } |
| 320 | |
| 321 | var ( |
| 322 | events []WorkflowEvent |
| 323 | rawEvents []session.Event |
| 324 | ) |
| 325 | |
| 326 | reader := wf.Execute(ctx, req.Input) |
| 327 | for { |
| 328 | ev, err := reader.Recv() |
| 329 | if err != nil { |
| 330 | if errors.Is(err, io.EOF) { |
| 331 | break |
| 332 | } |
| 333 | resp := &WorkflowRunEvalResponse{ |
| 334 | WorkflowRunResponse: WorkflowRunResponse{ |
nothing calls this directly
no test coverage detected