2025-08-05 07:08:07 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/errors"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
2025-11-25 15:33:34 +00:00
|
|
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
2025-08-05 07:08:07 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EvaluationHandler handles evaluation related HTTP requests
|
|
|
|
|
type EvaluationHandler struct {
|
|
|
|
|
evaluationService interfaces.EvaluationService // Service for evaluation operations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEvaluationHandler creates a new EvaluationHandler instance
|
|
|
|
|
func NewEvaluationHandler(evaluationService interfaces.EvaluationService) *EvaluationHandler {
|
|
|
|
|
return &EvaluationHandler{evaluationService: evaluationService}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EvaluationRequest contains parameters for evaluation request
|
|
|
|
|
type EvaluationRequest struct {
|
2025-08-12 07:35:24 +00:00
|
|
|
DatasetID string `json:"dataset_id"` // ID of dataset to evaluate
|
|
|
|
|
KnowledgeBaseID string `json:"knowledge_base_id"` // ID of knowledge base to use
|
|
|
|
|
ChatModelID string `json:"chat_id"` // ID of chat model to use
|
|
|
|
|
RerankModelID string `json:"rerank_id"` // ID of rerank model to use
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
// Evaluation godoc
|
|
|
|
|
// @Summary 执行评估
|
|
|
|
|
// @Description 对知识库进行评估测试
|
|
|
|
|
// @Tags 评估
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param request body EvaluationRequest true "评估请求参数"
|
docs(swagger): document the real request and response types (#4304)
* docs(swagger): add shared response envelope types
* docs(swagger): type knowledge base responses
* docs(swagger): add concrete response body types
* docs(swagger): type chunk list responses
* docs(swagger): type knowledge mutation responses
* docs(swagger): type wiki search and autofix responses
* docs(swagger): type FAQ responses
* docs(swagger): type tag responses
* docs(swagger): type message responses and SSE schemas
* docs(swagger): type agent responses
* docs(swagger): type remaining handler responses
* docs(swagger): regenerate OpenAPI spec
* docs(swagger): type knowledge, activity and wiki maintenance endpoints
Covers the endpoints left out of the earlier passes: knowledge base
detail, document list/detail/spans/reparse/cancel/delete, knowledge
base activity and the Wiki stats, issues, lint and rebuild-links
endpoints. The spans handler now writes the documented structs instead
of gin.H, so the schema cannot drift from the body; the JSON is
unchanged. List filters gain their real enums and bounds, including
the finalizing, cancelled and deleting parse states.
* docs(swagger): document JSON-valued fields as JSON, not byte arrays
types.JSON and json.RawMessage are byte slices, so swag rendered every
metadata, details and config field as an array of integers. A swag
overrides file documents them as objects; the four fields that hold
arrays (relation chunk ids, allowed origins, image refs) say so with a
swaggertype tag.
* chore(lint): exempt swag annotation lines from the line-length limit
swag reads each @Param, @Success and @Failure as a single line, so an
enum list or a composed response type cannot be wrapped to fit.
* chore(lint): exempt struct tags carrying swag attributes from the line limit
Struct tags cannot wrap either; a field documented with swaggertype or
extensions keeps its tags on the declaration line.
2026-10-14 17:41:42 +00:00
|
|
|
// @Success 200 {object} handler.DataResponse{data=types.EvaluationDetail} "评估任务"
|
2025-12-16 08:40:13 +00:00
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
// @Router /evaluation/ [post]
|
2025-08-05 07:08:07 +00:00
|
|
|
func (e *EvaluationHandler) Evaluation(c *gin.Context) {
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
|
|
|
|
logger.Info(ctx, "Start processing evaluation request")
|
|
|
|
|
|
2025-11-25 15:33:34 +00:00
|
|
|
var request EvaluationRequest
|
2025-08-05 07:08:07 +00:00
|
|
|
if err := c.ShouldBind(&request); err != nil {
|
|
|
|
|
logger.Error(ctx, "Failed to parse request parameters", err)
|
|
|
|
|
c.Error(errors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tenantID, exists := c.Get(string(types.TenantIDContextKey))
|
|
|
|
|
if !exists {
|
|
|
|
|
logger.Error(ctx, "Failed to get tenant ID")
|
|
|
|
|
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 07:35:24 +00:00
|
|
|
logger.Infof(ctx, "Executing evaluation, tenant: %v, dataset: %s, knowledge_base: %s, chat: %s, rerank: %s",
|
2025-11-25 15:33:34 +00:00
|
|
|
tenantID,
|
|
|
|
|
secutils.SanitizeForLog(request.DatasetID),
|
|
|
|
|
secutils.SanitizeForLog(request.KnowledgeBaseID),
|
|
|
|
|
secutils.SanitizeForLog(request.ChatModelID),
|
|
|
|
|
secutils.SanitizeForLog(request.RerankModelID),
|
|
|
|
|
)
|
2025-11-25 14:06:43 +00:00
|
|
|
|
2025-08-05 07:08:07 +00:00
|
|
|
task, err := e.evaluationService.Evaluation(ctx,
|
2025-11-26 03:27:48 +00:00
|
|
|
secutils.SanitizeForLog(request.DatasetID),
|
|
|
|
|
secutils.SanitizeForLog(request.KnowledgeBaseID),
|
|
|
|
|
secutils.SanitizeForLog(request.ChatModelID),
|
|
|
|
|
secutils.SanitizeForLog(request.RerankModelID),
|
2025-08-05 07:08:07 +00:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
c.Error(errors.NewInternalServerError(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Infof(ctx, "Evaluation task created successfully")
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"data": task,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetEvaluationRequest contains parameters for getting evaluation result
|
|
|
|
|
type GetEvaluationRequest struct {
|
|
|
|
|
TaskID string `form:"task_id" binding:"required"` // ID of evaluation task
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 08:40:13 +00:00
|
|
|
// GetEvaluationResult godoc
|
|
|
|
|
// @Summary 获取评估结果
|
|
|
|
|
// @Description 根据任务ID获取评估结果
|
|
|
|
|
// @Tags 评估
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param task_id query string true "评估任务ID"
|
docs(swagger): document the real request and response types (#4304)
* docs(swagger): add shared response envelope types
* docs(swagger): type knowledge base responses
* docs(swagger): add concrete response body types
* docs(swagger): type chunk list responses
* docs(swagger): type knowledge mutation responses
* docs(swagger): type wiki search and autofix responses
* docs(swagger): type FAQ responses
* docs(swagger): type tag responses
* docs(swagger): type message responses and SSE schemas
* docs(swagger): type agent responses
* docs(swagger): type remaining handler responses
* docs(swagger): regenerate OpenAPI spec
* docs(swagger): type knowledge, activity and wiki maintenance endpoints
Covers the endpoints left out of the earlier passes: knowledge base
detail, document list/detail/spans/reparse/cancel/delete, knowledge
base activity and the Wiki stats, issues, lint and rebuild-links
endpoints. The spans handler now writes the documented structs instead
of gin.H, so the schema cannot drift from the body; the JSON is
unchanged. List filters gain their real enums and bounds, including
the finalizing, cancelled and deleting parse states.
* docs(swagger): document JSON-valued fields as JSON, not byte arrays
types.JSON and json.RawMessage are byte slices, so swag rendered every
metadata, details and config field as an array of integers. A swag
overrides file documents them as objects; the four fields that hold
arrays (relation chunk ids, allowed origins, image refs) say so with a
swaggertype tag.
* chore(lint): exempt swag annotation lines from the line-length limit
swag reads each @Param, @Success and @Failure as a single line, so an
enum list or a composed response type cannot be wrapped to fit.
* chore(lint): exempt struct tags carrying swag attributes from the line limit
Struct tags cannot wrap either; a field documented with swaggertype or
extensions keeps its tags on the declaration line.
2026-10-14 17:41:42 +00:00
|
|
|
// @Success 200 {object} handler.DataResponse{data=types.EvaluationDetail} "评估结果"
|
2025-12-16 08:40:13 +00:00
|
|
|
// @Failure 400 {object} errors.AppError "请求参数错误"
|
|
|
|
|
// @Security Bearer
|
2025-12-22 06:36:29 +00:00
|
|
|
// @Security ApiKeyAuth
|
2025-12-16 08:40:13 +00:00
|
|
|
// @Router /evaluation/ [get]
|
2025-08-05 07:08:07 +00:00
|
|
|
func (e *EvaluationHandler) GetEvaluationResult(c *gin.Context) {
|
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
|
|
|
|
|
logger.Info(ctx, "Start retrieving evaluation result")
|
|
|
|
|
|
2025-11-25 14:06:43 +00:00
|
|
|
var request GetEvaluationRequest
|
2025-08-05 07:08:07 +00:00
|
|
|
if err := c.ShouldBind(&request); err != nil {
|
|
|
|
|
logger.Error(ctx, "Failed to parse request parameters", err)
|
|
|
|
|
c.Error(errors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-25 14:06:43 +00:00
|
|
|
|
2025-11-26 05:14:29 +00:00
|
|
|
result, err := e.evaluationService.EvaluationResult(ctx, secutils.SanitizeForLog(request.TaskID))
|
2025-08-05 07:08:07 +00:00
|
|
|
if err != nil {
|
|
|
|
|
logger.ErrorWithFields(ctx, err, nil)
|
|
|
|
|
c.Error(errors.NewInternalServerError(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 08:30:54 +00:00
|
|
|
logger.Info(ctx, "Retrieved evaluation result successfully")
|
2025-08-05 07:08:07 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"data": result,
|
|
|
|
|
})
|
|
|
|
|
}
|