mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-15 05:04:27 +08:00
New provider and models API and CLI (#13865)
### What problem does this PR solve? As title. ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Refactoring --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -21,11 +21,13 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"ragflow/internal/common"
|
||||
"ragflow/internal/dao"
|
||||
"ragflow/internal/logger"
|
||||
"ragflow/internal/server"
|
||||
"ragflow/internal/service"
|
||||
"ragflow/internal/utility"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -794,6 +796,115 @@ func (h *Handler) RestartService(c *gin.Context) {
|
||||
success(c, result, "")
|
||||
}
|
||||
|
||||
func (h *Handler) ListProviders(c *gin.Context) {
|
||||
|
||||
keywords := ""
|
||||
if queryKeywords := c.Query("available"); queryKeywords != "" {
|
||||
keywords = queryKeywords
|
||||
}
|
||||
|
||||
// convert keywords to small case
|
||||
keywords = strings.ToLower(keywords)
|
||||
if keywords == "true" {
|
||||
// list pool providers
|
||||
providers, err := dao.GetModelProviderManager().ListProviders()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeNotFound,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": providers,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ShowProvider(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
provider, err := dao.GetModelProviderManager().GetProviderByName(providerName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeNotFound,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": provider,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) ListModels(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
models, err := dao.GetModelProviderManager().ListModels(providerName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeNotFound,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": models,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) ShowModel(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
modelName := c.Param("model_name")
|
||||
if modelName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Model name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
model, err := dao.GetModelProviderManager().GetModelByName(providerName, modelName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeNotFound,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": model,
|
||||
})
|
||||
}
|
||||
|
||||
// GetVariables handle get variables
|
||||
// Python logic: if request body is empty, list all variables; otherwise get single variable by var_name from body
|
||||
func (h *Handler) GetVariables(c *gin.Context) {
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"ragflow/internal/handler"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -48,15 +46,6 @@ func (r *Router) Setup(engine *gin.Engine) {
|
||||
|
||||
admin.POST("/reports", r.handler.Reports)
|
||||
|
||||
// provider pool route group
|
||||
provider := admin.Group("providers")
|
||||
{
|
||||
provider.GET("/", handler.ListPoolProviders)
|
||||
provider.GET("/:provider_name", handler.ShowPoolProvider)
|
||||
provider.GET("/:provider_name/models", handler.ListPoolModels)
|
||||
provider.GET("/:provider_name/models/:model_name", handler.ShowPoolModel)
|
||||
}
|
||||
|
||||
// Protected routes
|
||||
protected := admin.Group("")
|
||||
protected.Use(r.handler.AuthMiddleware())
|
||||
@@ -136,6 +125,14 @@ func (r *Router) Setup(engine *gin.Engine) {
|
||||
// Log level
|
||||
protected.GET("/log_level", r.handler.GetLogLevel)
|
||||
protected.PUT("/log_level", r.handler.SetLogLevel)
|
||||
|
||||
provider := protected.Group("/providers")
|
||||
{
|
||||
provider.GET("/", r.handler.ListProviders)
|
||||
provider.GET("/:provider_name", r.handler.ShowProvider)
|
||||
provider.GET("/:provider_name/models", r.handler.ListModels)
|
||||
provider.GET("/:provider_name/models/:model_name", r.handler.ShowModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user