186 lines
5.5 KiB
Go
186 lines
5.5 KiB
Go
package app
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"git.echol.cn/loser/ai_proxy/server/global"
|
|
"git.echol.cn/loser/ai_proxy/server/model/app/request"
|
|
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
|
"git.echol.cn/loser/ai_proxy/server/service"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type AiProviderApi struct{}
|
|
|
|
var aiProviderService = service.ServiceGroupApp.AppServiceGroup.AiProviderService
|
|
|
|
// CreateAiProvider 创建AI提供商
|
|
// @Tags AiProvider
|
|
// @Summary 创建AI提供商
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.CreateAiProviderRequest true "提供商信息"
|
|
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "创建成功"
|
|
// @Router /app/provider [post]
|
|
func (a *AiProviderApi) CreateAiProvider(c *gin.Context) {
|
|
var req request.CreateAiProviderRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
provider, err := aiProviderService.CreateAiProvider(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("创建提供商失败!", zap.Error(err))
|
|
response.FailWithMessage("创建提供商失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(provider, c)
|
|
}
|
|
|
|
// DeleteAiProvider 删除AI提供商
|
|
// @Tags AiProvider
|
|
// @Summary 删除AI提供商
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param id path uint true "提供商ID"
|
|
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
|
// @Router /app/provider/:id [delete]
|
|
func (a *AiProviderApi) DeleteAiProvider(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
|
|
err := aiProviderService.DeleteAiProvider(uint(id))
|
|
if err != nil {
|
|
global.GVA_LOG.Error("删除提供商失败!", zap.Error(err))
|
|
response.FailWithMessage("删除提供商失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// UpdateAiProvider 更新AI提供商
|
|
// @Tags AiProvider
|
|
// @Summary 更新AI提供商
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.UpdateAiProviderRequest true "提供商信息"
|
|
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "更新成功"
|
|
// @Router /app/provider [put]
|
|
func (a *AiProviderApi) UpdateAiProvider(c *gin.Context) {
|
|
var req request.UpdateAiProviderRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
provider, err := aiProviderService.UpdateAiProvider(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("更新提供商失败!", zap.Error(err))
|
|
response.FailWithMessage("更新提供商失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(provider, c)
|
|
}
|
|
|
|
// GetAiProvider 获取AI提供商详情
|
|
// @Tags AiProvider
|
|
// @Summary 获取AI提供商详情
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param id path uint true "提供商ID"
|
|
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "获取成功"
|
|
// @Router /app/provider/:id [get]
|
|
func (a *AiProviderApi) GetAiProvider(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
|
|
provider, err := aiProviderService.GetAiProvider(uint(id))
|
|
if err != nil {
|
|
global.GVA_LOG.Error("获取提供商失败!", zap.Error(err))
|
|
response.FailWithMessage("获取提供商失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(provider, c)
|
|
}
|
|
|
|
// GetAiProviderList 获取AI提供商列表
|
|
// @Tags AiProvider
|
|
// @Summary 获取AI提供商列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{data=[]app.AiProvider,msg=string} "获取成功"
|
|
// @Router /app/provider/list [get]
|
|
func (a *AiProviderApi) GetAiProviderList(c *gin.Context) {
|
|
list, err := aiProviderService.GetAiProviderList()
|
|
if err != nil {
|
|
global.GVA_LOG.Error("获取提供商列表失败!", zap.Error(err))
|
|
response.FailWithMessage("获取提供商列表失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(list, c)
|
|
}
|
|
|
|
// TestConnection 测试连接
|
|
// @Tags AiProvider
|
|
// @Summary 测试AI提供商连接
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.TestConnectionRequest true "连接信息"
|
|
// @Success 200 {object} response.Response{data=response.TestConnectionResponse,msg=string} "测试成功"
|
|
// @Router /app/provider/test [post]
|
|
func (a *AiProviderApi) TestConnection(c *gin.Context) {
|
|
var req request.TestConnectionRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
result, err := aiProviderService.TestConnection(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("测试连接失败!", zap.Error(err))
|
|
response.FailWithMessage("测试连接失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(result, c)
|
|
}
|
|
|
|
// GetModels 获取模型列表
|
|
// @Tags AiProvider
|
|
// @Summary 获取AI提供商的模型列表
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.GetModelsRequest true "提供商信息"
|
|
// @Success 200 {object} response.Response{data=[]response.ModelInfo,msg=string} "获取成功"
|
|
// @Router /app/provider/models [post]
|
|
func (a *AiProviderApi) GetModels(c *gin.Context) {
|
|
var req request.GetModelsRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
models, err := aiProviderService.GetModels(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("获取模型列表失败!", zap.Error(err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(models, c)
|
|
}
|