🎨 优化模型配置 && 新增apikey功能 && 完善通用接口
This commit is contained in:
@@ -5,52 +5,49 @@ import (
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"git.echol.cn/loser/ai_proxy/server/service"
|
||||
"git.echol.cn/loser/ai_proxy/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AiPresetBindingApi struct{}
|
||||
type AiApiKeyApi struct{}
|
||||
|
||||
var aiPresetBindingService = service.ServiceGroupApp.AppServiceGroup.AiPresetBindingService
|
||||
|
||||
// CreateAiPresetBinding 创建绑定
|
||||
// @Tags AiPresetBinding
|
||||
// @Summary 创建绑定
|
||||
// CreateAiApiKey 创建API密钥
|
||||
// @Tags AiApiKey
|
||||
// @Summary 创建API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body app.AiPresetBinding true "绑定信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||||
// @Router /aiPresetBinding/createAiPresetBinding [post]
|
||||
func (a *AiPresetBindingApi) CreateAiPresetBinding(c *gin.Context) {
|
||||
var binding app.AiPresetBinding
|
||||
err := c.ShouldBindJSON(&binding)
|
||||
// @Param data body app.AiApiKey true "API密钥信息"
|
||||
// @Success 200 {object} response.Response{data=app.AiApiKey,msg=string} "创建成功"
|
||||
// @Router /aiApiKey/createAiApiKey [post]
|
||||
func (a *AiApiKeyApi) CreateAiApiKey(c *gin.Context) {
|
||||
var apiKey app.AiApiKey
|
||||
err := c.ShouldBindJSON(&apiKey)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
binding.UserID = utils.GetUserID(c)
|
||||
apiKey.UserID = utils.GetUserID(c)
|
||||
|
||||
if err := aiPresetBindingService.CreateAiPresetBinding(&binding); err != nil {
|
||||
if err := aiApiKeyService.CreateAiApiKey(&apiKey); err != nil {
|
||||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("创建成功", c)
|
||||
response.OkWithData(apiKey, c)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAiPresetBinding 删除绑定
|
||||
// @Tags AiPresetBinding
|
||||
// @Summary 删除绑定
|
||||
// DeleteAiApiKey 删除API密钥
|
||||
// @Tags AiApiKey
|
||||
// @Summary 删除API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetById true "ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /aiPresetBinding/deleteAiPresetBinding [delete]
|
||||
func (a *AiPresetBindingApi) DeleteAiPresetBinding(c *gin.Context) {
|
||||
// @Router /aiApiKey/deleteAiApiKey [delete]
|
||||
func (a *AiApiKeyApi) DeleteAiApiKey(c *gin.Context) {
|
||||
var reqId request.GetById
|
||||
err := c.ShouldBindJSON(&reqId)
|
||||
if err != nil {
|
||||
@@ -59,7 +56,7 @@ func (a *AiPresetBindingApi) DeleteAiPresetBinding(c *gin.Context) {
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if err := aiPresetBindingService.DeleteAiPresetBinding(reqId.Uint(), userID); err != nil {
|
||||
if err := aiApiKeyService.DeleteAiApiKey(reqId.Uint(), userID); err != nil {
|
||||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||||
response.FailWithMessage("删除失败", c)
|
||||
} else {
|
||||
@@ -67,25 +64,25 @@ func (a *AiPresetBindingApi) DeleteAiPresetBinding(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAiPresetBinding 更新绑定
|
||||
// @Tags AiPresetBinding
|
||||
// @Summary 更新绑定
|
||||
// UpdateAiApiKey 更新API密钥
|
||||
// @Tags AiApiKey
|
||||
// @Summary 更新API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body app.AiPresetBinding true "绑定信息"
|
||||
// @Param data body app.AiApiKey true "API密钥信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||||
// @Router /aiPresetBinding/updateAiPresetBinding [put]
|
||||
func (a *AiPresetBindingApi) UpdateAiPresetBinding(c *gin.Context) {
|
||||
var binding app.AiPresetBinding
|
||||
err := c.ShouldBindJSON(&binding)
|
||||
// @Router /aiApiKey/updateAiApiKey [put]
|
||||
func (a *AiApiKeyApi) UpdateAiApiKey(c *gin.Context) {
|
||||
var apiKey app.AiApiKey
|
||||
err := c.ShouldBindJSON(&apiKey)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if err := aiPresetBindingService.UpdateAiPresetBinding(&binding, userID); err != nil {
|
||||
if err := aiApiKeyService.UpdateAiApiKey(&apiKey, userID); err != nil {
|
||||
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
||||
response.FailWithMessage("更新失败", c)
|
||||
} else {
|
||||
@@ -93,16 +90,16 @@ func (a *AiPresetBindingApi) UpdateAiPresetBinding(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// FindAiPresetBinding 查询绑定
|
||||
// @Tags AiPresetBinding
|
||||
// @Summary 查询绑定
|
||||
// FindAiApiKey 查询API密钥
|
||||
// @Tags AiApiKey
|
||||
// @Summary 查询API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data query request.GetById true "ID"
|
||||
// @Success 200 {object} response.Response{data=app.AiPresetBinding,msg=string} "查询成功"
|
||||
// @Router /aiPresetBinding/findAiPresetBinding [get]
|
||||
func (a *AiPresetBindingApi) FindAiPresetBinding(c *gin.Context) {
|
||||
// @Success 200 {object} response.Response{data=app.AiApiKey,msg=string} "查询成功"
|
||||
// @Router /aiApiKey/findAiApiKey [get]
|
||||
func (a *AiApiKeyApi) FindAiApiKey(c *gin.Context) {
|
||||
var reqId request.GetById
|
||||
err := c.ShouldBindQuery(&reqId)
|
||||
if err != nil {
|
||||
@@ -111,24 +108,24 @@ func (a *AiPresetBindingApi) FindAiPresetBinding(c *gin.Context) {
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if binding, err := aiPresetBindingService.GetAiPresetBinding(reqId.Uint(), userID); err != nil {
|
||||
if apiKey, err := aiApiKeyService.GetAiApiKey(reqId.Uint(), userID); err != nil {
|
||||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||||
response.FailWithMessage("查询失败", c)
|
||||
} else {
|
||||
response.OkWithData(binding, c)
|
||||
response.OkWithData(apiKey, c)
|
||||
}
|
||||
}
|
||||
|
||||
// GetAiPresetBindingList 获取绑定列表
|
||||
// @Tags AiPresetBinding
|
||||
// @Summary 获取绑定列表
|
||||
// GetAiApiKeyList 获取API密钥列表
|
||||
// @Tags AiApiKey
|
||||
// @Summary 获取API密钥列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data query request.PageInfo true "分页参数"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||||
// @Router /aiPresetBinding/getAiPresetBindingList [get]
|
||||
func (a *AiPresetBindingApi) GetAiPresetBindingList(c *gin.Context) {
|
||||
// @Router /aiApiKey/getAiApiKeyList [get]
|
||||
func (a *AiApiKeyApi) GetAiApiKeyList(c *gin.Context) {
|
||||
var pageInfo request.PageInfo
|
||||
err := c.ShouldBindQuery(&pageInfo)
|
||||
if err != nil {
|
||||
@@ -137,7 +134,7 @@ func (a *AiPresetBindingApi) GetAiPresetBindingList(c *gin.Context) {
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if list, total, err := aiPresetBindingService.GetAiPresetBindingList(pageInfo, userID); err != nil {
|
||||
if list, total, err := aiApiKeyService.GetAiApiKeyList(pageInfo, userID); err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
} else {
|
||||
177
server/api/v1/app/ai_model.go
Normal file
177
server/api/v1/app/ai_model.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"git.echol.cn/loser/ai_proxy/server/service"
|
||||
"git.echol.cn/loser/ai_proxy/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AiModelApi struct{}
|
||||
|
||||
var aiModelService = service.ServiceGroupApp.AppServiceGroup.AiModelService
|
||||
|
||||
// CreateAiModel 创建模型
|
||||
// @Tags AiModel
|
||||
// @Summary 创建模型
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body app.AiModel true "模型信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||||
// @Router /aiModel/createAiModel [post]
|
||||
func (a *AiModelApi) CreateAiModel(c *gin.Context) {
|
||||
var model app.AiModel
|
||||
err := c.ShouldBindJSON(&model)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
model.UserID = utils.GetUserID(c)
|
||||
|
||||
if err := aiModelService.CreateAiModel(&model); err != nil {
|
||||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAiModel 删除模型
|
||||
// @Tags AiModel
|
||||
// @Summary 删除模型
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetById true "ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /aiModel/deleteAiModel [delete]
|
||||
func (a *AiModelApi) DeleteAiModel(c *gin.Context) {
|
||||
var reqId request.GetById
|
||||
err := c.ShouldBindJSON(&reqId)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if err := aiModelService.DeleteAiModel(reqId.Uint(), userID); err != nil {
|
||||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||||
response.FailWithMessage("删除失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAiModel 更新模型
|
||||
// @Tags AiModel
|
||||
// @Summary 更新模型
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body app.AiModel true "模型信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||||
// @Router /aiModel/updateAiModel [put]
|
||||
func (a *AiModelApi) UpdateAiModel(c *gin.Context) {
|
||||
var model app.AiModel
|
||||
err := c.ShouldBindJSON(&model)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if err := aiModelService.UpdateAiModel(&model, userID); err != nil {
|
||||
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
||||
response.FailWithMessage("更新失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// FindAiModel 查询模型
|
||||
// @Tags AiModel
|
||||
// @Summary 查询模型
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data query request.GetById true "ID"
|
||||
// @Success 200 {object} response.Response{data=app.AiModel,msg=string} "查询成功"
|
||||
// @Router /aiModel/findAiModel [get]
|
||||
func (a *AiModelApi) FindAiModel(c *gin.Context) {
|
||||
var reqId request.GetById
|
||||
err := c.ShouldBindQuery(&reqId)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if model, err := aiModelService.GetAiModel(reqId.Uint(), userID); err != nil {
|
||||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||||
response.FailWithMessage("查询失败", c)
|
||||
} else {
|
||||
response.OkWithData(model, c)
|
||||
}
|
||||
}
|
||||
|
||||
// GetAiModelList 获取模型列表
|
||||
// @Tags AiModel
|
||||
// @Summary 获取模型列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data query request.PageInfo true "分页参数"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||||
// @Router /aiModel/getAiModelList [get]
|
||||
func (a *AiModelApi) GetAiModelList(c *gin.Context) {
|
||||
var pageInfo request.PageInfo
|
||||
err := c.ShouldBindQuery(&pageInfo)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if list, total, err := aiModelService.GetAiModelList(pageInfo, userID); err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// SyncProviderModels 同步提供商模型
|
||||
// @Tags AiModel
|
||||
// @Summary 同步提供商模型
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetById true "提供商ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "同步成功"
|
||||
// @Router /aiModel/syncProviderModels [post]
|
||||
func (a *AiModelApi) SyncProviderModels(c *gin.Context) {
|
||||
var reqId request.GetById
|
||||
err := c.ShouldBindJSON(&reqId)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
if err := aiModelService.SyncProviderModels(reqId.Uint(), userID); err != nil {
|
||||
global.GVA_LOG.Error("同步失败!", zap.Error(err))
|
||||
response.FailWithMessage("同步失败: "+err.Error(), c)
|
||||
} else {
|
||||
response.OkWithMessage("同步成功", c)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/request"
|
||||
@@ -150,7 +153,7 @@ func (a *AiPresetApi) GetAiPresetList(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// ImportAiPreset 导入预设
|
||||
// ImportAiPreset 导入预设(JSON粘贴)
|
||||
// @Tags AiPreset
|
||||
// @Summary 导入预设(支持SillyTavern格式)
|
||||
// @Security ApiKeyAuth
|
||||
@@ -160,17 +163,75 @@ func (a *AiPresetApi) GetAiPresetList(c *gin.Context) {
|
||||
// @Success 200 {object} response.Response{msg=string} "导入成功"
|
||||
// @Router /aiPreset/importAiPreset [post]
|
||||
func (a *AiPresetApi) ImportAiPreset(c *gin.Context) {
|
||||
var preset app.AiPreset
|
||||
err := c.ShouldBindJSON(&preset)
|
||||
var rawData map[string]interface{}
|
||||
err := c.ShouldBindJSON(&rawData)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
preset, err := aiPresetService.ParseImportedPreset(rawData)
|
||||
if err != nil {
|
||||
response.FailWithMessage("解析预设失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
preset.UserID = utils.GetUserID(c)
|
||||
|
||||
if err := aiPresetService.CreateAiPreset(&preset); err != nil {
|
||||
if err := aiPresetService.CreateAiPreset(preset); err != nil {
|
||||
global.GVA_LOG.Error("导入失败!", zap.Error(err))
|
||||
response.FailWithMessage("导入失败", c)
|
||||
response.FailWithMessage("导入失败:"+err.Error(), c)
|
||||
} else {
|
||||
response.OkWithMessage("导入成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// ImportAiPresetFile 导入预设文件
|
||||
// @Tags AiPreset
|
||||
// @Summary 通过文件导入预设(支持SillyTavern格式)
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "预设JSON文件"
|
||||
// @Success 200 {object} response.Response{msg=string} "导入成功"
|
||||
// @Router /aiPreset/importAiPresetFile [post]
|
||||
func (a *AiPresetApi) ImportAiPresetFile(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
response.FailWithMessage("文件上传失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查文件类型
|
||||
if file.Header.Get("Content-Type") != "application/json" && !strings.HasSuffix(file.Filename, ".json") {
|
||||
response.FailWithMessage("只支持JSON文件", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
fileContent, err := file.Open()
|
||||
if err != nil {
|
||||
response.FailWithMessage("文件读取失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
defer fileContent.Close()
|
||||
|
||||
// 解析JSON
|
||||
var rawData map[string]interface{}
|
||||
if err := json.NewDecoder(fileContent).Decode(&rawData); err != nil {
|
||||
response.FailWithMessage("JSON解析失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
preset, err := aiPresetService.ParseImportedPreset(rawData)
|
||||
if err != nil {
|
||||
response.FailWithMessage("解析预设失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
preset.UserID = utils.GetUserID(c)
|
||||
|
||||
if err := aiPresetService.CreateAiPreset(preset); err != nil {
|
||||
global.GVA_LOG.Error("导入失败!", zap.Error(err))
|
||||
response.FailWithMessage("导入失败:"+err.Error(), c)
|
||||
} else {
|
||||
response.OkWithMessage("导入成功", c)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app"
|
||||
"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"
|
||||
"git.echol.cn/loser/ai_proxy/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -13,11 +15,11 @@ import (
|
||||
type AiProxyApi struct{}
|
||||
|
||||
var aiProxyService = service.ServiceGroupApp.AppServiceGroup.AiProxyService
|
||||
var aiApiKeyService = service.ServiceGroupApp.AppServiceGroup.AiApiKeyService
|
||||
|
||||
// ChatCompletions OpenAI兼容的聊天补全接口
|
||||
// @Tags AiProxy
|
||||
// @Summary 聊天补全(OpenAI兼容)
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.ChatCompletionRequest true "聊天请求"
|
||||
@@ -31,16 +33,46 @@ func (a *AiProxyApi) ChatCompletions(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
userId := utils.GetUserID(c)
|
||||
// 获取 API Key 信息
|
||||
apiKeyInfo, exists := c.Get("ai_api_key")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未找到API密钥信息"})
|
||||
return
|
||||
}
|
||||
apiKey := apiKeyInfo.(*app.AiApiKey)
|
||||
|
||||
// 验证模型权限
|
||||
if req.Model != "" && !aiApiKeyService.CheckModelPermission(apiKey, req.Model) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此模型: " + req.Model,
|
||||
"type": "invalid_request_error",
|
||||
"code": "model_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证预设权限
|
||||
if req.PresetName != "" && !aiApiKeyService.CheckPresetPermission(apiKey, req.PresetName) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此预设: " + req.PresetName,
|
||||
"type": "invalid_request_error",
|
||||
"code": "preset_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 处理流式响应
|
||||
if req.Stream {
|
||||
aiProxyService.ProcessChatCompletionStream(c, userId, &req)
|
||||
aiProxyService.ProcessChatCompletionStream(c, &req)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理普通响应
|
||||
resp, err := aiProxyService.ProcessChatCompletion(c.Request.Context(), userId, &req)
|
||||
resp, err := aiProxyService.ProcessChatCompletion(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("处理聊天请求失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
@@ -49,3 +81,95 @@ func (a *AiProxyApi) ChatCompletions(c *gin.Context) {
|
||||
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
|
||||
// ListModels 获取可用模型列表
|
||||
// @Tags AiProxy
|
||||
// @Summary 获取可用模型列表(OpenAI兼容)
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.ModelListResponse "模型列表"
|
||||
// @Router /v1/models [get]
|
||||
func (a *AiProxyApi) ListModels(c *gin.Context) {
|
||||
// 获取 API Key 信息
|
||||
apiKeyInfo, exists := c.Get("ai_api_key")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未找到API密钥信息"})
|
||||
return
|
||||
}
|
||||
apiKey := apiKeyInfo.(*app.AiApiKey)
|
||||
|
||||
// 获取可用模型列表
|
||||
models, err := aiProxyService.GetAvailableModels(apiKey)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取模型列表失败!", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, models)
|
||||
}
|
||||
|
||||
// ClaudeMessages Claude API兼容的消息接口
|
||||
// @Tags AiProxy
|
||||
// @Summary 消息接口(Claude兼容)
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.ClaudeMessageRequest true "消息请求"
|
||||
// @Success 200 {object} response.ClaudeMessageResponse "消息响应"
|
||||
// @Router /v1/messages [post]
|
||||
func (a *AiProxyApi) ClaudeMessages(c *gin.Context) {
|
||||
var req request.ClaudeMessageRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取 API Key 信息
|
||||
apiKeyInfo, exists := c.Get("ai_api_key")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未找到API密钥信息"})
|
||||
return
|
||||
}
|
||||
apiKey := apiKeyInfo.(*app.AiApiKey)
|
||||
|
||||
// 验证模型权限
|
||||
if req.Model != "" && !aiApiKeyService.CheckModelPermission(apiKey, req.Model) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此模型: " + req.Model,
|
||||
"type": "invalid_request_error",
|
||||
"code": "model_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证预设权限
|
||||
if req.PresetName != "" && !aiApiKeyService.CheckPresetPermission(apiKey, req.PresetName) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此预设: " + req.PresetName,
|
||||
"type": "invalid_request_error",
|
||||
"code": "preset_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 处理流式响应
|
||||
if req.Stream {
|
||||
aiProxyService.ProcessClaudeMessageStream(c, &req)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理普通响应
|
||||
resp, err := aiProxyService.ProcessClaudeMessage(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("处理Claude消息请求失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
|
||||
@@ -4,5 +4,6 @@ type ApiGroup struct {
|
||||
AiProxyApi
|
||||
AiPresetApi
|
||||
AiProviderApi
|
||||
AiPresetBindingApi
|
||||
AiApiKeyApi
|
||||
AiModelApi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user