2024-01-28 11:13:11 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
|
|
"github.com/songquanpeng/one-api/model"
|
|
|
|
"github.com/songquanpeng/one-api/relay/channel/openai"
|
|
|
|
"github.com/songquanpeng/one-api/relay/constant"
|
2024-02-17 16:15:31 +00:00
|
|
|
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/util"
|
2024-01-28 11:13:11 +00:00
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func getAndValidateTextRequest(c *gin.Context, relayMode int) (*relaymodel.GeneralOpenAIRequest, error) {
|
|
|
|
textRequest := &relaymodel.GeneralOpenAIRequest{}
|
2024-01-28 11:13:11 +00:00
|
|
|
err := common.UnmarshalBodyReusable(c, textRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if relayMode == constant.RelayModeModerations && textRequest.Model == "" {
|
|
|
|
textRequest.Model = "text-moderation-latest"
|
|
|
|
}
|
|
|
|
if relayMode == constant.RelayModeEmbeddings && textRequest.Model == "" {
|
|
|
|
textRequest.Model = c.Param("model")
|
|
|
|
}
|
|
|
|
err = util.ValidateTextRequest(textRequest, relayMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return textRequest, nil
|
|
|
|
}
|
|
|
|
|
2024-03-03 11:30:11 +00:00
|
|
|
func getImageRequest(c *gin.Context, relayMode int) (*openai.ImageRequest, error) {
|
|
|
|
imageRequest := &openai.ImageRequest{}
|
|
|
|
err := common.UnmarshalBodyReusable(c, imageRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if imageRequest.N == 0 {
|
|
|
|
imageRequest.N = 1
|
|
|
|
}
|
|
|
|
if imageRequest.Size == "" {
|
|
|
|
imageRequest.Size = "1024x1024"
|
|
|
|
}
|
|
|
|
if imageRequest.Model == "" {
|
|
|
|
imageRequest.Model = "dall-e-2"
|
|
|
|
}
|
|
|
|
return imageRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateImageRequest(imageRequest *openai.ImageRequest, meta *util.RelayMeta) *relaymodel.ErrorWithStatusCode {
|
|
|
|
// model validation
|
|
|
|
_, hasValidSize := constant.DalleSizeRatios[imageRequest.Model][imageRequest.Size]
|
|
|
|
if !hasValidSize {
|
|
|
|
return openai.ErrorWrapper(errors.New("size not supported for this image model"), "size_not_supported", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
// check prompt length
|
|
|
|
if imageRequest.Prompt == "" {
|
|
|
|
return openai.ErrorWrapper(errors.New("prompt is required"), "prompt_missing", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
if len(imageRequest.Prompt) > constant.DalleImagePromptLengthLimitations[imageRequest.Model] {
|
|
|
|
return openai.ErrorWrapper(errors.New("prompt is too long"), "prompt_too_long", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
// Number of generated images validation
|
|
|
|
if !isWithinRange(imageRequest.Model, imageRequest.N) {
|
|
|
|
// channel not azure
|
|
|
|
if meta.ChannelType != common.ChannelTypeAzure {
|
|
|
|
return openai.ErrorWrapper(errors.New("invalid value of n"), "n_not_within_range", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getImageCostRatio(imageRequest *openai.ImageRequest) (float64, error) {
|
|
|
|
if imageRequest == nil {
|
|
|
|
return 0, errors.New("imageRequest is nil")
|
|
|
|
}
|
|
|
|
imageCostRatio, hasValidSize := constant.DalleSizeRatios[imageRequest.Model][imageRequest.Size]
|
|
|
|
if !hasValidSize {
|
|
|
|
return 0, fmt.Errorf("size not supported for this image model: %s", imageRequest.Size)
|
|
|
|
}
|
|
|
|
if imageRequest.Quality == "hd" && imageRequest.Model == "dall-e-3" {
|
|
|
|
if imageRequest.Size == "1024x1024" {
|
|
|
|
imageCostRatio *= 2
|
|
|
|
} else {
|
|
|
|
imageCostRatio *= 1.5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return imageCostRatio, nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func getPromptTokens(textRequest *relaymodel.GeneralOpenAIRequest, relayMode int) int {
|
2024-01-28 11:13:11 +00:00
|
|
|
switch relayMode {
|
|
|
|
case constant.RelayModeChatCompletions:
|
|
|
|
return openai.CountTokenMessages(textRequest.Messages, textRequest.Model)
|
|
|
|
case constant.RelayModeCompletions:
|
|
|
|
return openai.CountTokenInput(textRequest.Prompt, textRequest.Model)
|
|
|
|
case constant.RelayModeModerations:
|
|
|
|
return openai.CountTokenInput(textRequest.Input, textRequest.Model)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func getPreConsumedQuota(textRequest *relaymodel.GeneralOpenAIRequest, promptTokens int, ratio float64) int {
|
2024-01-28 11:13:11 +00:00
|
|
|
preConsumedTokens := config.PreConsumedQuota
|
|
|
|
if textRequest.MaxTokens != 0 {
|
|
|
|
preConsumedTokens = promptTokens + textRequest.MaxTokens
|
|
|
|
}
|
|
|
|
return int(float64(preConsumedTokens) * ratio)
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func preConsumeQuota(ctx context.Context, textRequest *relaymodel.GeneralOpenAIRequest, promptTokens int, ratio float64, meta *util.RelayMeta) (int, *relaymodel.ErrorWithStatusCode) {
|
2024-01-28 11:13:11 +00:00
|
|
|
preConsumedQuota := getPreConsumedQuota(textRequest, promptTokens, ratio)
|
|
|
|
|
|
|
|
userQuota, err := model.CacheGetUserQuota(meta.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return preConsumedQuota, openai.ErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if userQuota-preConsumedQuota < 0 {
|
|
|
|
return preConsumedQuota, openai.ErrorWrapper(errors.New("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
err = model.CacheDecreaseUserQuota(meta.UserId, preConsumedQuota)
|
|
|
|
if err != nil {
|
|
|
|
return preConsumedQuota, openai.ErrorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if userQuota > 100*preConsumedQuota {
|
|
|
|
// in this case, we do not pre-consume quota
|
|
|
|
// because the user has enough quota
|
|
|
|
preConsumedQuota = 0
|
|
|
|
logger.Info(ctx, fmt.Sprintf("user %d has enough quota %d, trusted and no need to pre-consume", meta.UserId, userQuota))
|
|
|
|
}
|
|
|
|
if preConsumedQuota > 0 {
|
|
|
|
err := model.PreConsumeTokenQuota(meta.TokenId, preConsumedQuota)
|
|
|
|
if err != nil {
|
|
|
|
return preConsumedQuota, openai.ErrorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return preConsumedQuota, nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *util.RelayMeta, textRequest *relaymodel.GeneralOpenAIRequest, ratio float64, preConsumedQuota int, modelRatio float64, groupRatio float64) {
|
2024-01-28 11:13:11 +00:00
|
|
|
if usage == nil {
|
|
|
|
logger.Error(ctx, "usage is nil, which is unexpected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
quota := 0
|
|
|
|
completionRatio := common.GetCompletionRatio(textRequest.Model)
|
|
|
|
promptTokens := usage.PromptTokens
|
|
|
|
completionTokens := usage.CompletionTokens
|
|
|
|
quota = int(math.Ceil((float64(promptTokens) + float64(completionTokens)*completionRatio) * ratio))
|
|
|
|
if ratio != 0 && quota <= 0 {
|
|
|
|
quota = 1
|
|
|
|
}
|
|
|
|
totalTokens := promptTokens + completionTokens
|
|
|
|
if totalTokens == 0 {
|
|
|
|
// in this case, must be some error happened
|
|
|
|
// we cannot just return, because we may have to return the pre-consumed quota
|
|
|
|
quota = 0
|
|
|
|
}
|
|
|
|
quotaDelta := quota - preConsumedQuota
|
|
|
|
err := model.PostConsumeTokenQuota(meta.TokenId, quotaDelta)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, "error consuming token remain quota: "+err.Error())
|
|
|
|
}
|
|
|
|
err = model.CacheUpdateUserQuota(meta.UserId)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, "error update user quota cache: "+err.Error())
|
|
|
|
}
|
2024-03-03 13:46:45 +00:00
|
|
|
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
|
|
|
|
model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent)
|
|
|
|
model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota)
|
|
|
|
model.UpdateChannelUsedQuota(meta.ChannelId, quota)
|
2024-01-28 11:13:11 +00:00
|
|
|
}
|