Go CLI: Fix show model and list models (#16380)

### What problem does this PR solve?

```
RAGFlow(api/default)> show model 'WiseDiag-Z1 Think';

RAGFlow(api/default)> list models;

RAGFlow(admin)> show model 'WiseDiag-Z1 Think';

RAGFlow(admin)> list models;
```

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-26 15:36:01 +08:00
committed by GitHub
parent 0de8f3e127
commit 2667995b25
9 changed files with 154 additions and 139 deletions

View File

@@ -20,6 +20,7 @@ import (
"net/http"
"ragflow/internal/common"
"ragflow/internal/service"
"strconv"
"github.com/gin-gonic/gin"
)
@@ -36,63 +37,82 @@ func NewModelHandler(modelProviderService *service.ModelProviderService) *ModelH
}
}
type ShowModelRequest struct {
ModelName *string `json:"model_name"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
func (h *ModelHandler) ListAllModels(c *gin.Context) {
var req ShowModelRequest
if err := c.ShouldBindJSON(&req); err != nil {
println("JSON bind error: %v (type: %T)", err, err)
page := 0
if v := c.Query("page"); v != "" {
if p, err := strconv.Atoi(v); err == nil && p > 0 {
page = p
}
}
pageSize := 0
if v := c.Query("page_size"); v != "" {
if ps, err := strconv.Atoi(v); err == nil && ps > 0 {
pageSize = ps
}
}
// list tenant models
models, err := h.modelProviderService.ListAllModels(page, pageSize)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeBadRequest,
"code": common.CodeDataError,
"message": err.Error(),
"data": nil,
})
return
}
if req.ModelName == nil {
// list models
page := req.Page
pageSize := req.PageSize
// list tenant models
models, err := h.modelProviderService.ListAllModels(page, pageSize)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": err.Error(),
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": models,
})
} else {
// show model
model, err := h.modelProviderService.ShowModel(*req.ModelName)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": err.Error(),
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": model,
})
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": models,
})
return
}
func (h *ModelHandler) ShowModel(c *gin.Context) {
encodedModelName := c.Param("model_name")
if encodedModelName == "" {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"message": "Encoded model name is empty",
})
return
}
decodedModelName, err := common.DecodeFromBase64(encodedModelName)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
if decodedModelName == "" {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"message": "Decoded model name is empty",
})
return
}
// Get model
model, err := h.modelProviderService.ShowModel(decodedModelName)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": err.Error(),
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"message": "success",
"data": model,
})
}