MCPcopy Create free account
hub / github.com/astercloud/aster / BatchEvalHandler

Method BatchEvalHandler

pkg/server/server.go:929–1095  ·  view source on GitHub ↗

BatchEvalHandler 返回批量评估的HTTP handler

()

Source from the content-addressed store, hash-verified

927
928// BatchEvalHandler 返回批量评估的HTTP handler
929func (s *Server) BatchEvalHandler() http.Handler {
930 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
931 start := time.Now()
932
933 if r.Method != http.MethodPost {
934 http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
935 return
936 }
937
938 var req BatchEvalRequest
939 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
940 http.Error(w, "invalid JSON body", http.StatusBadRequest)
941 return
942 }
943
944 if len(req.TestCases) == 0 {
945 http.Error(w, "test_cases is required", http.StatusBadRequest)
946 return
947 }
948
949 // 默认使用基础 scorers
950 scorerNames := req.Scorers
951 if len(scorerNames) == 0 {
952 scorerNames = []string{"keyword_coverage", "lexical_similarity"}
953 }
954
955 ctx := r.Context()
956 ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
957 defer cancel()
958
959 // 创建 scorers
960 scorers := make([]evals.Scorer, 0, len(scorerNames))
961 var llmProvider provider.Provider
962
963 for _, name := range scorerNames {
964 switch name {
965 case "keyword_coverage":
966 scorer := evals.NewKeywordCoverageScorer(evals.KeywordCoverageConfig{
967 Keywords: req.Keywords,
968 CaseInsensitive: true,
969 })
970 scorers = append(scorers, scorer)
971
972 case "lexical_similarity":
973 scorer := evals.NewLexicalSimilarityScorer(evals.LexicalSimilarityConfig{
974 MinTokenLength: 2,
975 })
976 scorers = append(scorers, scorer)
977
978 case "faithfulness", "hallucination", "answer_relevancy", "context_relevancy",
979 "toxicity", "tone_consistency", "coherence", "completeness":
980 // LLM-based scorers 需要 Provider
981 if llmProvider == nil {
982 if req.ProviderConfig == nil {
983 http.Error(w, "provider_config is required for LLM-based scorers", http.StatusBadRequest)
984 return
985 }
986 var err error

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewKeywordCoverageScorerFunction · 0.92
NewFaithfulnessScorerFunction · 0.92
NewHallucinationScorerFunction · 0.92
NewAnswerRelevancyScorerFunction · 0.92
NewToxicityScorerFunction · 0.92
NewToneConsistencyScorerFunction · 0.92
NewCoherenceScorerFunction · 0.92
NewCompletenessScorerFunction · 0.92
RunBatchFunction · 0.92

Tested by

no test coverage detected