yao/openapi/kb/score.go
Max faafa4f90e Refactor segment extraction functions and update API routes
- Renamed segment extraction functions to better reflect their purpose, changing `ExtractSegmentEntities` to `ExtractSegmentGraph` and `ExtractSegmentEntitiesAsync` to `ExtractSegmentGraphAsync`.
- Updated API routes to use the new function names for segment extraction, enhancing clarity and consistency.
- Introduced new request structures for batch updates of scores and weights, improving the API's capabilities for segment management.
- Removed the outdated `store.go` file to streamline the codebase.
2025-08-21 09:08:14 +08:00

79 lines
2.2 KiB
Go

package kb
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
"github.com/yaoapp/yao/openapi/response"
)
// Score Management Handlers
// UpdateScores updates scores for multiple segments in batch
func UpdateScores(c *gin.Context) {
// Extract docID from URL path
docID := c.Param("docID")
if docID == "" {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: "Document ID is required",
}
response.RespondWithError(c, response.StatusBadRequest, errorResp)
return
}
// Parse request body for batch score updates
var req UpdateScoresRequest
if err := c.ShouldBindJSON(&req); err != nil {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: "Invalid request format: " + err.Error(),
}
response.RespondWithError(c, response.StatusBadRequest, errorResp)
return
}
// Validate request
if len(req.Scores) == 0 {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: "At least one score update is required",
}
response.RespondWithError(c, response.StatusBadRequest, errorResp)
return
}
// Validate each score entry
for i, score := range req.Scores {
if strings.TrimSpace(score.ID) == "" {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: fmt.Sprintf("scores[%d].id is required", i),
}
response.RespondWithError(c, response.StatusBadRequest, errorResp)
return
}
if score.Score < 0 {
errorResp := &response.ErrorResponse{
Code: response.ErrInvalidRequest.Code,
ErrorDescription: fmt.Sprintf("scores[%d].score cannot be negative", i),
}
response.RespondWithError(c, response.StatusBadRequest, errorResp)
return
}
}
// TODO: Implement document permission validation for docID
// TODO: Implement batch update scores logic
// TODO: Call kb.Instance.UpdateScores(c.Request.Context(), docID, req.Scores)
result := gin.H{
"message": "Scores updated successfully",
"document_id": docID,
"scores": req.Scores,
"updated_count": len(req.Scores),
}
response.RespondWithSuccess(c, response.StatusOK, result)
}