mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-15 04:33:42 +08:00
🎨 结构中继控制器
This commit is contained in:
91
controller/relay-chat.go
Normal file
91
controller/relay-chat.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayChat(c *gin.Context) {
|
||||
|
||||
var chatRequest types.ChatCompletionRequest
|
||||
if err := common.UnmarshalBodyReusable(c, &chatRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, chatRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[chatRequest.Model] != "" {
|
||||
chatRequest.Model = modelMap[chatRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeChatCompletions)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
chatProvider, ok := provider.(providersBase.ChatInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := common.CountTokenMessages(chatRequest.Messages, chatRequest.Model)
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, chatRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = chatProvider.ChatAction(&chatRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
91
controller/relay-completions.go
Normal file
91
controller/relay-completions.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayCompletions(c *gin.Context) {
|
||||
|
||||
var completionRequest types.CompletionRequest
|
||||
if err := common.UnmarshalBodyReusable(c, &completionRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, completionRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[completionRequest.Model] != "" {
|
||||
completionRequest.Model = modelMap[completionRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeCompletions)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
completionProvider, ok := provider.(providersBase.CompletionInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := common.CountTokenInput(completionRequest.Prompt, completionRequest.Model)
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, completionRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = completionProvider.CompleteAction(&completionRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
96
controller/relay-embeddings.go
Normal file
96
controller/relay-embeddings.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayEmbeddings(c *gin.Context) {
|
||||
|
||||
var embeddingsRequest types.EmbeddingRequest
|
||||
if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
|
||||
embeddingsRequest.Model = c.Param("model")
|
||||
}
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &embeddingsRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, embeddingsRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[embeddingsRequest.Model] != "" {
|
||||
embeddingsRequest.Model = modelMap[embeddingsRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeEmbeddings)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
embeddingsProvider, ok := provider.(providersBase.EmbeddingsInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := common.CountTokenInput(embeddingsRequest.Input, embeddingsRequest.Model)
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, embeddingsRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = embeddingsProvider.EmbeddingsAction(&embeddingsRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
@@ -25,16 +25,16 @@ func relayHelper(c *gin.Context, relayMode int) *types.OpenAIErrorWithStatusCode
|
||||
// 获取 Provider
|
||||
provider := providers.GetProvider(channelType, c)
|
||||
if provider == nil {
|
||||
return types.ErrorWrapper(errors.New("channel not found"), "channel_not_found", http.StatusNotImplemented)
|
||||
return common.ErrorWrapper(errors.New("channel not found"), "channel_not_found", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
if !provider.SupportAPI(relayMode) {
|
||||
return types.ErrorWrapper(errors.New("channel does not support this API"), "channel_not_support_api", http.StatusNotImplemented)
|
||||
return common.ErrorWrapper(errors.New("channel does not support this API"), "channel_not_support_api", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
return types.ErrorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
|
||||
return common.ErrorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
quotaInfo := &QuotaInfo{
|
||||
@@ -70,7 +70,7 @@ func relayHelper(c *gin.Context, relayMode int) *types.OpenAIErrorWithStatusCode
|
||||
case common.RelayModeImagesVariations:
|
||||
usage, openAIErrorWithStatusCode = handleImageEdits(c, provider, modelMap, quotaInfo, group, "variation")
|
||||
default:
|
||||
return types.ErrorWrapper(errors.New("invalid relay mode"), "invalid_relay_mode", http.StatusBadRequest)
|
||||
return common.ErrorWrapper(errors.New("invalid relay mode"), "invalid_relay_mode", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if openAIErrorWithStatusCode != nil {
|
||||
@@ -105,16 +105,16 @@ func handleChatCompletions(c *gin.Context, provider providers_base.ProviderInter
|
||||
|
||||
chatProvider, ok := provider.(providers_base.ChatInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &chatRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if chatRequest.Messages == nil || len(chatRequest.Messages) == 0 {
|
||||
return nil, types.ErrorWrapper(errors.New("field messages is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field messages is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[chatRequest.Model] != "" {
|
||||
@@ -138,16 +138,16 @@ func handleCompletions(c *gin.Context, provider providers_base.ProviderInterface
|
||||
isModelMapped := false
|
||||
completionProvider, ok := provider.(providers_base.CompletionInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &completionRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if completionRequest.Prompt == "" {
|
||||
return nil, types.ErrorWrapper(errors.New("field prompt is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field prompt is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[completionRequest.Model] != "" {
|
||||
@@ -171,16 +171,16 @@ func handleEmbeddings(c *gin.Context, provider providers_base.ProviderInterface,
|
||||
isModelMapped := false
|
||||
embeddingsProvider, ok := provider.(providers_base.EmbeddingsInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &embeddingsRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if embeddingsRequest.Input == "" {
|
||||
return nil, types.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[embeddingsRequest.Model] != "" {
|
||||
@@ -204,16 +204,16 @@ func handleModerations(c *gin.Context, provider providers_base.ProviderInterface
|
||||
isModelMapped := false
|
||||
moderationProvider, ok := provider.(providers_base.ModerationInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &moderationRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if moderationRequest.Input == "" {
|
||||
return nil, types.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if moderationRequest.Model == "" {
|
||||
@@ -241,16 +241,16 @@ func handleSpeech(c *gin.Context, provider providers_base.ProviderInterface, mod
|
||||
isModelMapped := false
|
||||
speechProvider, ok := provider.(providers_base.SpeechInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &speechRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if speechRequest.Input == "" {
|
||||
return nil, types.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[speechRequest.Model] != "" {
|
||||
@@ -274,17 +274,17 @@ func handleTranscriptions(c *gin.Context, provider providers_base.ProviderInterf
|
||||
isModelMapped := false
|
||||
speechProvider, ok := provider.(providers_base.TranscriptionsInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &audioRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if audioRequest.File == nil {
|
||||
fmt.Println(audioRequest)
|
||||
return nil, types.ErrorWrapper(errors.New("field file is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field file is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[audioRequest.Model] != "" {
|
||||
@@ -308,17 +308,17 @@ func handleTranslations(c *gin.Context, provider providers_base.ProviderInterfac
|
||||
isModelMapped := false
|
||||
speechProvider, ok := provider.(providers_base.TranslationInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &audioRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if audioRequest.File == nil {
|
||||
fmt.Println(audioRequest)
|
||||
return nil, types.ErrorWrapper(errors.New("field file is required"), "required_field_missing", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(errors.New("field file is required"), "required_field_missing", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if modelMap != nil && modelMap[audioRequest.Model] != "" {
|
||||
@@ -342,12 +342,12 @@ func handleImageGenerations(c *gin.Context, provider providers_base.ProviderInte
|
||||
isModelMapped := false
|
||||
imageGenerationsProvider, ok := provider.(providers_base.ImageGenerationsInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &imageRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if imageRequest.Model == "" {
|
||||
@@ -368,7 +368,7 @@ func handleImageGenerations(c *gin.Context, provider providers_base.ProviderInte
|
||||
}
|
||||
promptTokens, err := common.CountTokenImage(imageRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "count_token_image_failed", http.StatusInternalServerError)
|
||||
return nil, common.ErrorWrapper(err, "count_token_image_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
quotaInfo.modelName = imageRequest.Model
|
||||
@@ -390,18 +390,18 @@ func handleImageEdits(c *gin.Context, provider providers_base.ProviderInterface,
|
||||
if imageType == "edit" {
|
||||
imageEditsProvider, ok = provider.(providers_base.ImageEditsInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
} else {
|
||||
imageVariations, ok = provider.(providers_base.ImageVariationsInterface)
|
||||
if !ok {
|
||||
return nil, types.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
return nil, common.ErrorWrapper(errors.New("channel not implemented"), "channel_not_implemented", http.StatusNotImplemented)
|
||||
}
|
||||
}
|
||||
|
||||
err := common.UnmarshalBodyReusable(c, &imageEditRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
return nil, common.ErrorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if imageEditRequest.Model == "" {
|
||||
@@ -418,7 +418,7 @@ func handleImageEdits(c *gin.Context, provider providers_base.ProviderInterface,
|
||||
}
|
||||
promptTokens, err := common.CountTokenImage(imageEditRequest)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "count_token_image_failed", http.StatusInternalServerError)
|
||||
return nil, common.ErrorWrapper(err, "count_token_image_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
quotaInfo.modelName = imageEditRequest.Model
|
||||
|
||||
109
controller/relay-image-edits.go
Normal file
109
controller/relay-image-edits.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayImageEdits(c *gin.Context) {
|
||||
|
||||
var imageEditRequest types.ImageEditRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &imageEditRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if imageEditRequest.Prompt == "" {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, "field prompt is required")
|
||||
return
|
||||
}
|
||||
|
||||
if imageEditRequest.Model == "" {
|
||||
imageEditRequest.Model = "dall-e-2"
|
||||
}
|
||||
|
||||
if imageEditRequest.Size == "" {
|
||||
imageEditRequest.Size = "1024x1024"
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, imageEditRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[imageEditRequest.Model] != "" {
|
||||
imageEditRequest.Model = modelMap[imageEditRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesEdits)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
imageEditsProvider, ok := provider.(providersBase.ImageEditsInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens, err := common.CountTokenImage(imageEditRequest)
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, imageEditRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = imageEditsProvider.ImageEditsAction(&imageEditRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
108
controller/relay-image-generations.go
Normal file
108
controller/relay-image-generations.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayImageGenerations(c *gin.Context) {
|
||||
|
||||
var imageRequest types.ImageRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &imageRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if imageRequest.Model == "" {
|
||||
imageRequest.Model = "dall-e-2"
|
||||
}
|
||||
|
||||
if imageRequest.Size == "" {
|
||||
imageRequest.Size = "1024x1024"
|
||||
}
|
||||
|
||||
if imageRequest.Quality == "" {
|
||||
imageRequest.Quality = "standard"
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, imageRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[imageRequest.Model] != "" {
|
||||
imageRequest.Model = modelMap[imageRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesGenerations)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
imageGenerationsProvider, ok := provider.(providersBase.ImageGenerationsInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens, err := common.CountTokenImage(imageRequest)
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, imageRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = imageGenerationsProvider.ImageGenerationsAction(&imageRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
104
controller/relay-image-variationsy.go
Normal file
104
controller/relay-image-variationsy.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayImageVariations(c *gin.Context) {
|
||||
|
||||
var imageEditRequest types.ImageEditRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &imageEditRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if imageEditRequest.Model == "" {
|
||||
imageEditRequest.Model = "dall-e-2"
|
||||
}
|
||||
|
||||
if imageEditRequest.Size == "" {
|
||||
imageEditRequest.Size = "1024x1024"
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, imageEditRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[imageEditRequest.Model] != "" {
|
||||
imageEditRequest.Model = modelMap[imageEditRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesVariations)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
imageVariations, ok := provider.(providersBase.ImageVariationsInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens, err := common.CountTokenImage(imageEditRequest)
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, imageEditRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = imageVariations.ImageVariationsAction(&imageEditRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
96
controller/relay-moderations.go
Normal file
96
controller/relay-moderations.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayModerations(c *gin.Context) {
|
||||
|
||||
var moderationRequest types.ModerationRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &moderationRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if moderationRequest.Model == "" {
|
||||
moderationRequest.Model = "text-moderation-stable"
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, moderationRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[moderationRequest.Model] != "" {
|
||||
moderationRequest.Model = modelMap[moderationRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeModerations)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
moderationProvider, ok := provider.(providersBase.ModerationInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := common.CountTokenInput(moderationRequest.Input, moderationRequest.Model)
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, moderationRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = moderationProvider.ModerationAction(&moderationRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
92
controller/relay-speech.go
Normal file
92
controller/relay-speech.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelaySpeech(c *gin.Context) {
|
||||
|
||||
var speechRequest types.SpeechAudioRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &speechRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, speechRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[speechRequest.Model] != "" {
|
||||
speechRequest.Model = modelMap[speechRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioSpeech)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
speechProvider, ok := provider.(providersBase.SpeechInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := len(speechRequest.Input)
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, speechRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = speechProvider.SpeechAction(&speechRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
92
controller/relay-transcriptions.go
Normal file
92
controller/relay-transcriptions.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayTranscriptions(c *gin.Context) {
|
||||
|
||||
var audioRequest types.AudioRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &audioRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, audioRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[audioRequest.Model] != "" {
|
||||
audioRequest.Model = modelMap[audioRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioTranscription)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
transcriptionsProvider, ok := provider.(providersBase.TranscriptionsInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := 0
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, audioRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = transcriptionsProvider.TranscriptionsAction(&audioRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
92
controller/relay-translations.go
Normal file
92
controller/relay-translations.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RelayTranslations(c *gin.Context) {
|
||||
|
||||
var audioRequest types.AudioRequest
|
||||
|
||||
if err := common.UnmarshalBodyReusable(c, &audioRequest); err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
channel, pass := fetchChannel(c, audioRequest.Model)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入渠道信息
|
||||
setChannelToContext(c, channel)
|
||||
|
||||
// 解析模型映射
|
||||
var isModelMapped bool
|
||||
modelMap, err := parseModelMapping(c.GetString("model_mapping"))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if modelMap != nil && modelMap[audioRequest.Model] != "" {
|
||||
audioRequest.Model = modelMap[audioRequest.Model]
|
||||
isModelMapped = true
|
||||
}
|
||||
|
||||
// 获取供应商
|
||||
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioTranslation)
|
||||
if pass {
|
||||
return
|
||||
}
|
||||
translationProvider, ok := provider.(providersBase.TranslationInterface)
|
||||
if !ok {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取Input Tokens
|
||||
promptTokens := 0
|
||||
|
||||
var quotaInfo *QuotaInfo
|
||||
var errWithCode *types.OpenAIErrorWithStatusCode
|
||||
var usage *types.Usage
|
||||
quotaInfo, errWithCode = generateQuotaInfo(c, audioRequest.Model, promptTokens)
|
||||
if errWithCode != nil {
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
}
|
||||
|
||||
usage, errWithCode = translationProvider.TranslationAction(&audioRequest, isModelMapped, promptTokens)
|
||||
|
||||
// 如果报错,则退还配额
|
||||
if errWithCode != nil {
|
||||
tokenId := c.GetInt("token_id")
|
||||
if quotaInfo.HandelStatus {
|
||||
go func(ctx context.Context) {
|
||||
// return pre-consumed quota
|
||||
err := model.PostConsumeTokenQuota(tokenId, -quotaInfo.preConsumedQuota)
|
||||
if err != nil {
|
||||
common.LogError(ctx, "error return pre-consumed quota: "+err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
errorHelper(c, errWithCode)
|
||||
return
|
||||
} else {
|
||||
tokenName := c.GetString("token_name")
|
||||
// 如果没有报错,则消费配额
|
||||
go func(ctx context.Context) {
|
||||
err = quotaInfo.completedQuotaConsumption(usage, tokenName, ctx)
|
||||
if err != nil {
|
||||
common.LogError(ctx, err.Error())
|
||||
}
|
||||
}(c.Request.Context())
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,104 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
"one-api/providers"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func GetValidFieldName(err error, obj interface{}) string {
|
||||
getObj := reflect.TypeOf(obj)
|
||||
if errs, ok := err.(validator.ValidationErrors); ok {
|
||||
for _, e := range errs {
|
||||
if f, exist := getObj.Elem().FieldByName(e.Field()); exist {
|
||||
return f.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func fetchChannel(c *gin.Context, modelName string) (*model.Channel, bool) {
|
||||
channelId, ok := c.Get("channelId")
|
||||
if ok {
|
||||
return fetchChannelById(c, channelId.(int))
|
||||
}
|
||||
return fetchChannelByModel(c, modelName)
|
||||
|
||||
}
|
||||
|
||||
func fetchChannelById(c *gin.Context, channelId any) (*model.Channel, bool) {
|
||||
id, err := strconv.Atoi(channelId.(string))
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
||||
return nil, true
|
||||
}
|
||||
channel, err := model.GetChannelById(id, true)
|
||||
if err != nil {
|
||||
common.AbortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
||||
return nil, true
|
||||
}
|
||||
if channel.Status != common.ChannelStatusEnabled {
|
||||
common.AbortWithMessage(c, http.StatusForbidden, "该渠道已被禁用")
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return channel, false
|
||||
}
|
||||
|
||||
func fetchChannelByModel(c *gin.Context, modelName string) (*model.Channel, bool) {
|
||||
group := c.GetString("group")
|
||||
channel, err := model.CacheGetRandomSatisfiedChannel(group, modelName)
|
||||
if err != nil {
|
||||
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", group, modelName)
|
||||
if channel != nil {
|
||||
common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id))
|
||||
message = "数据库一致性已被破坏,请联系管理员"
|
||||
}
|
||||
common.AbortWithMessage(c, http.StatusServiceUnavailable, message)
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return channel, false
|
||||
}
|
||||
|
||||
func getProvider(c *gin.Context, channelType int, relayMode int) (providersBase.ProviderInterface, bool) {
|
||||
provider := providers.GetProvider(channelType, c)
|
||||
if provider == nil {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not found")
|
||||
return nil, true
|
||||
}
|
||||
|
||||
if !provider.SupportAPI(relayMode) {
|
||||
common.AbortWithMessage(c, http.StatusNotImplemented, "channel does not support this API")
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return provider, false
|
||||
}
|
||||
|
||||
func setChannelToContext(c *gin.Context, channel *model.Channel) {
|
||||
c.Set("channel", channel.Type)
|
||||
c.Set("channel_id", channel.Id)
|
||||
c.Set("channel_name", channel.Name)
|
||||
c.Set("model_mapping", channel.GetModelMapping())
|
||||
c.Set("api_key", channel.Key)
|
||||
c.Set("base_url", channel.GetBaseURL())
|
||||
switch channel.Type {
|
||||
case common.ChannelTypeAzure:
|
||||
c.Set("api_version", channel.Other)
|
||||
case common.ChannelTypeXunfei:
|
||||
c.Set("api_version", channel.Other)
|
||||
case common.ChannelTypeAIProxyLibrary:
|
||||
c.Set("library_id", channel.Other)
|
||||
}
|
||||
}
|
||||
|
||||
func shouldDisableChannel(err *types.OpenAIError, statusCode int) bool {
|
||||
if !common.AutomaticDisableChannelEnabled {
|
||||
return false
|
||||
@@ -68,6 +163,26 @@ type QuotaInfo struct {
|
||||
userId int
|
||||
channelId int
|
||||
tokenId int
|
||||
HandelStatus bool
|
||||
}
|
||||
|
||||
func generateQuotaInfo(c *gin.Context, modelName string, promptTokens int) (*QuotaInfo, *types.OpenAIErrorWithStatusCode) {
|
||||
quotaInfo := &QuotaInfo{
|
||||
modelName: modelName,
|
||||
promptTokens: promptTokens,
|
||||
userId: c.GetInt("id"),
|
||||
channelId: c.GetInt("channel_id"),
|
||||
tokenId: c.GetInt("token_id"),
|
||||
HandelStatus: false,
|
||||
}
|
||||
quotaInfo.initQuotaInfo(c.GetString("group"))
|
||||
|
||||
errWithCode := quotaInfo.preQuotaConsumption()
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
return quotaInfo, nil
|
||||
}
|
||||
|
||||
func (q *QuotaInfo) initQuotaInfo(groupName string) {
|
||||
@@ -89,16 +204,16 @@ func (q *QuotaInfo) initQuotaInfo(groupName string) {
|
||||
func (q *QuotaInfo) preQuotaConsumption() *types.OpenAIErrorWithStatusCode {
|
||||
userQuota, err := model.CacheGetUserQuota(q.userId)
|
||||
if err != nil {
|
||||
return types.ErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
|
||||
return common.ErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if userQuota < q.preConsumedQuota {
|
||||
return types.ErrorWrapper(errors.New("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
|
||||
return common.ErrorWrapper(errors.New("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
|
||||
}
|
||||
|
||||
err = model.CacheDecreaseUserQuota(q.userId, q.preConsumedQuota)
|
||||
if err != nil {
|
||||
return types.ErrorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError)
|
||||
return common.ErrorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if userQuota > 100*q.preConsumedQuota {
|
||||
@@ -111,8 +226,9 @@ func (q *QuotaInfo) preQuotaConsumption() *types.OpenAIErrorWithStatusCode {
|
||||
if q.preConsumedQuota > 0 {
|
||||
err := model.PreConsumeTokenQuota(q.tokenId, q.preConsumedQuota)
|
||||
if err != nil {
|
||||
return types.ErrorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden)
|
||||
return common.ErrorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden)
|
||||
}
|
||||
q.HandelStatus = true
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -6,73 +6,10 @@ import (
|
||||
"one-api/common"
|
||||
"one-api/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Relay(c *gin.Context) {
|
||||
defer c.Request.Body.Close()
|
||||
var err *types.OpenAIErrorWithStatusCode
|
||||
|
||||
relayMode := common.RelayModeUnknown
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/v1/chat/completions") {
|
||||
relayMode = common.RelayModeChatCompletions
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/completions") {
|
||||
relayMode = common.RelayModeCompletions
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
|
||||
relayMode = common.RelayModeEmbeddings
|
||||
} else if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
|
||||
relayMode = common.RelayModeEmbeddings
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
||||
relayMode = common.RelayModeModerations
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
|
||||
relayMode = common.RelayModeAudioSpeech
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
|
||||
relayMode = common.RelayModeAudioTranscription
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
|
||||
relayMode = common.RelayModeAudioTranslation
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
||||
relayMode = common.RelayModeImagesGenerations
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
|
||||
relayMode = common.RelayModeImagesEdits
|
||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/variations") {
|
||||
relayMode = common.RelayModeImagesVariations
|
||||
}
|
||||
// } else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
|
||||
// relayMode = RelayModeEdits
|
||||
|
||||
err = relayHelper(c, relayMode)
|
||||
|
||||
if err != nil {
|
||||
requestId := c.GetString(common.RequestIdKey)
|
||||
retryTimesStr := c.Query("retry")
|
||||
retryTimes, _ := strconv.Atoi(retryTimesStr)
|
||||
if retryTimesStr == "" {
|
||||
retryTimes = common.RetryTimes
|
||||
}
|
||||
if retryTimes > 0 {
|
||||
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s?retry=%d", c.Request.URL.Path, retryTimes-1))
|
||||
} else {
|
||||
if err.StatusCode == http.StatusTooManyRequests {
|
||||
err.OpenAIError.Message = "当前分组上游负载已饱和,请稍后再试"
|
||||
}
|
||||
err.OpenAIError.Message = common.MessageWithRequestId(err.OpenAIError.Message, requestId)
|
||||
c.JSON(err.StatusCode, gin.H{
|
||||
"error": err.OpenAIError,
|
||||
})
|
||||
}
|
||||
channelId := c.GetInt("channel_id")
|
||||
common.LogError(c.Request.Context(), fmt.Sprintf("relay error (channel #%d): %s", channelId, err.Message))
|
||||
// https://platform.openai.com/docs/guides/error-codes/api-errors
|
||||
if shouldDisableChannel(&err.OpenAIError, err.StatusCode) {
|
||||
channelId := c.GetInt("channel_id")
|
||||
channelName := c.GetString("channel_name")
|
||||
disableChannel(channelId, channelName, err.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RelayNotImplemented(c *gin.Context) {
|
||||
err := types.OpenAIError{
|
||||
Message: "API not implemented",
|
||||
@@ -96,3 +33,31 @@ func RelayNotFound(c *gin.Context) {
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
func errorHelper(c *gin.Context, err *types.OpenAIErrorWithStatusCode) {
|
||||
requestId := c.GetString(common.RequestIdKey)
|
||||
retryTimesStr := c.Query("retry")
|
||||
retryTimes, _ := strconv.Atoi(retryTimesStr)
|
||||
if retryTimesStr == "" {
|
||||
retryTimes = common.RetryTimes
|
||||
}
|
||||
if retryTimes > 0 {
|
||||
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s?retry=%d", c.Request.URL.Path, retryTimes-1))
|
||||
} else {
|
||||
if err.StatusCode == http.StatusTooManyRequests {
|
||||
err.OpenAIError.Message = "当前分组上游负载已饱和,请稍后再试"
|
||||
}
|
||||
err.OpenAIError.Message = common.MessageWithRequestId(err.OpenAIError.Message, requestId)
|
||||
c.JSON(err.StatusCode, gin.H{
|
||||
"error": err.OpenAIError,
|
||||
})
|
||||
}
|
||||
channelId := c.GetInt("channel_id")
|
||||
common.LogError(c.Request.Context(), fmt.Sprintf("relay error (channel #%d): %s", channelId, err.Message))
|
||||
// https://platform.openai.com/docs/guides/error-codes/api-errors
|
||||
if shouldDisableChannel(&err.OpenAIError, err.StatusCode) {
|
||||
channelId := c.GetInt("channel_id")
|
||||
channelName := c.GetString("channel_name")
|
||||
disableChannel(channelId, channelName, err.Message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user