- Added GetCollection endpoint to retrieve collections by ID, including error handling for missing IDs and uninitialized knowledge base. - Updated CreateCollectionConfig to use more descriptive field names for embedding provider and option IDs. - Refactored UpdateSegments function to improve validation and error handling, ensuring document IDs and segment texts are properly checked. - Introduced new request structures for updating votes, scores, and weights for segments, enhancing the API's capabilities. - Removed the outdated vote handling file to streamline the codebase.
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package kb
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/yaoapp/yao/kb"
|
|
"github.com/yaoapp/yao/openapi/response"
|
|
)
|
|
|
|
// Segment Voting, Scoring, Weighting Handlers
|
|
|
|
// UpdateVote updates votes for segments
|
|
func UpdateVote(c *gin.Context) {
|
|
// TODO: Implement update vote logic
|
|
c.JSON(http.StatusOK, gin.H{"message": "Vote updated"})
|
|
}
|
|
|
|
// UpdateScore updates scores for segments
|
|
func UpdateScore(c *gin.Context) {
|
|
// TODO: Implement update score logic
|
|
c.JSON(http.StatusOK, gin.H{"message": "Score updated"})
|
|
}
|
|
|
|
// UpdateWeight updates weights for segments
|
|
func UpdateWeight(c *gin.Context) {
|
|
var req UpdateWeightRequest
|
|
|
|
// Parse and bind JSON request
|
|
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 err := req.Validate(); err != nil {
|
|
errorResp := &response.ErrorResponse{
|
|
Code: response.ErrInvalidRequest.Code,
|
|
ErrorDescription: err.Error(),
|
|
}
|
|
response.RespondWithError(c, response.StatusBadRequest, errorResp)
|
|
return
|
|
}
|
|
|
|
// Check if kb.Instance is available
|
|
if kb.Instance == nil {
|
|
errorResp := &response.ErrorResponse{
|
|
Code: response.ErrServerError.Code,
|
|
ErrorDescription: "Knowledge base not initialized",
|
|
}
|
|
response.RespondWithError(c, response.StatusInternalServerError, errorResp)
|
|
return
|
|
}
|
|
|
|
// Perform update weight operation
|
|
updatedCount, err := kb.Instance.UpdateWeight(c.Request.Context(), req.Segments)
|
|
if err != nil {
|
|
errorResp := &response.ErrorResponse{
|
|
Code: response.ErrServerError.Code,
|
|
ErrorDescription: "Failed to update segment weights: " + err.Error(),
|
|
}
|
|
response.RespondWithError(c, response.StatusInternalServerError, errorResp)
|
|
return
|
|
}
|
|
|
|
// Return success response
|
|
result := gin.H{
|
|
"message": "Segment weights updated successfully",
|
|
"segments": req.Segments,
|
|
"updated_count": updatedCount,
|
|
}
|
|
|
|
response.RespondWithSuccess(c, response.StatusOK, result)
|
|
}
|