Files
ragflow/internal/handler/models.go
Jin Hai 2667995b25 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>
2026-06-26 15:36:01 +08:00

119 lines
2.6 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package handler
import (
"net/http"
"ragflow/internal/common"
"ragflow/internal/service"
"strconv"
"github.com/gin-gonic/gin"
)
// ProviderHandler provider handler
type ModelHandler struct {
modelProviderService *service.ModelProviderService
}
// NewProviderHandler create provider handler
func NewModelHandler(modelProviderService *service.ModelProviderService) *ModelHandler {
return &ModelHandler{
modelProviderService: modelProviderService,
}
}
func (h *ModelHandler) ListAllModels(c *gin.Context) {
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.CodeDataError,
"message": err.Error(),
"data": nil,
})
return
}
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,
})
}