2024-02-29 01:08:18 +08:00
|
|
|
|
package relay
|
2023-06-19 10:28:55 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-15 12:30:06 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
2023-10-22 17:50:52 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-19 10:28:55 +08:00
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
2023-07-15 12:30:06 +08:00
|
|
|
|
"one-api/common"
|
2024-05-23 23:59:55 +08:00
|
|
|
|
"one-api/constant"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
"one-api/dto"
|
2023-07-15 12:30:06 +08:00
|
|
|
|
"one-api/model"
|
2024-02-29 16:21:25 +08:00
|
|
|
|
relaycommon "one-api/relay/common"
|
|
|
|
|
|
"one-api/service"
|
2023-11-09 17:08:32 +08:00
|
|
|
|
"strings"
|
2023-06-19 10:28:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
func getAndValidImageRequest(c *gin.Context, info *relaycommon.RelayInfo) (*dto.ImageRequest, error) {
|
|
|
|
|
|
imageRequest := &dto.ImageRequest{}
|
|
|
|
|
|
err := common.UnmarshalBodyReusable(c, imageRequest)
|
2024-03-13 22:30:10 +08:00
|
|
|
|
if err != nil {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return nil, err
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if imageRequest.Prompt == "" {
|
|
|
|
|
|
return nil, errors.New("prompt is required")
|
2023-11-09 17:08:32 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if strings.Contains(imageRequest.Size, "×") {
|
|
|
|
|
|
return nil, errors.New("size an unexpected error occurred in the parameter, please use 'x' instead of the multiplication sign '×'")
|
2023-11-10 17:08:33 +08:00
|
|
|
|
}
|
2023-11-27 14:24:28 +08:00
|
|
|
|
if imageRequest.N == 0 {
|
|
|
|
|
|
imageRequest.N = 1
|
|
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if imageRequest.Size == "" {
|
|
|
|
|
|
imageRequest.Size = "1024x1024"
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if imageRequest.Model == "" {
|
|
|
|
|
|
imageRequest.Model = "dall-e-2"
|
2024-05-23 23:59:55 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if imageRequest.Quality == "" {
|
|
|
|
|
|
imageRequest.Quality = "standard"
|
2023-11-09 17:08:32 +08:00
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
// Not "256x256", "512x512", or "1024x1024"
|
2023-11-10 17:08:33 +08:00
|
|
|
|
if imageRequest.Model == "dall-e-2" || imageRequest.Model == "dall-e" {
|
|
|
|
|
|
if imageRequest.Size != "" && imageRequest.Size != "256x256" && imageRequest.Size != "512x512" && imageRequest.Size != "1024x1024" {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return nil, errors.New("size must be one of 256x256, 512x512, or 1024x1024, dall-e-3 1024x1792 or 1792x1024")
|
2023-11-10 17:08:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if imageRequest.Model == "dall-e-3" {
|
|
|
|
|
|
if imageRequest.Size != "" && imageRequest.Size != "1024x1024" && imageRequest.Size != "1024x1792" && imageRequest.Size != "1792x1024" {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return nil, errors.New("size must be one of 256x256, 512x512, or 1024x1024, dall-e-3 1024x1792 or 1792x1024")
|
2023-11-10 17:08:33 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
//if imageRequest.N != 1 {
|
|
|
|
|
|
// return nil, errors.New("n must be 1")
|
|
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
// N should between 1 and 10
|
|
|
|
|
|
//if imageRequest.N != 0 && (imageRequest.N < 1 || imageRequest.N > 10) {
|
|
|
|
|
|
// return service.OpenAIErrorWrapper(errors.New("n must be between 1 and 10"), "invalid_field_value", http.StatusBadRequest)
|
|
|
|
|
|
//}
|
|
|
|
|
|
if constant.ShouldCheckPromptSensitive() {
|
|
|
|
|
|
err := service.CheckSensitiveInput(imageRequest.Prompt)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2023-12-01 01:29:13 +08:00
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return imageRequest, nil
|
|
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
func ImageHelper(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusCode {
|
|
|
|
|
|
relayInfo := relaycommon.GenRelayInfo(c)
|
|
|
|
|
|
|
|
|
|
|
|
imageRequest, err := getAndValidImageRequest(c, relayInfo)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.LogError(c, fmt.Sprintf("getAndValidImageRequest failed: %s", err.Error()))
|
|
|
|
|
|
return service.OpenAIErrorWrapper(err, "invalid_image_request", http.StatusBadRequest)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// map model name
|
|
|
|
|
|
modelMapping := c.GetString("model_mapping")
|
|
|
|
|
|
if modelMapping != "" {
|
|
|
|
|
|
modelMap := make(map[string]string)
|
|
|
|
|
|
err := json.Unmarshal([]byte(modelMapping), &modelMap)
|
|
|
|
|
|
if err != nil {
|
2024-02-29 16:21:25 +08:00
|
|
|
|
return service.OpenAIErrorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2023-11-09 17:08:32 +08:00
|
|
|
|
if modelMap[imageRequest.Model] != "" {
|
|
|
|
|
|
imageRequest.Model = modelMap[imageRequest.Model]
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
relayInfo.UpstreamModelName = imageRequest.Model
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-05-13 16:04:02 +08:00
|
|
|
|
modelPrice, success := common.GetModelPrice(imageRequest.Model, true)
|
|
|
|
|
|
if !success {
|
|
|
|
|
|
modelRatio := common.GetModelRatio(imageRequest.Model)
|
|
|
|
|
|
// modelRatio 16 = modelPrice $0.04
|
|
|
|
|
|
// per 1 modelRatio = $0.04 / 16
|
|
|
|
|
|
modelPrice = 0.0025 * modelRatio
|
|
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
|
|
|
|
|
|
groupRatio := common.GetGroupRatio(relayInfo.Group)
|
|
|
|
|
|
userQuota, err := model.CacheGetUserQuota(relayInfo.UserId)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
|
|
|
|
|
sizeRatio := 1.0
|
|
|
|
|
|
// Size
|
|
|
|
|
|
if imageRequest.Size == "256x256" {
|
2024-05-13 16:04:02 +08:00
|
|
|
|
sizeRatio = 0.4
|
2023-07-15 12:30:06 +08:00
|
|
|
|
} else if imageRequest.Size == "512x512" {
|
2024-05-13 16:04:02 +08:00
|
|
|
|
sizeRatio = 0.45
|
2023-07-15 12:30:06 +08:00
|
|
|
|
} else if imageRequest.Size == "1024x1024" {
|
2024-05-13 16:04:02 +08:00
|
|
|
|
sizeRatio = 1
|
2023-11-09 17:08:32 +08:00
|
|
|
|
} else if imageRequest.Size == "1024x1792" || imageRequest.Size == "1792x1024" {
|
2024-05-13 16:04:02 +08:00
|
|
|
|
sizeRatio = 2
|
2023-11-09 17:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qualityRatio := 1.0
|
|
|
|
|
|
if imageRequest.Model == "dall-e-3" && imageRequest.Quality == "hd" {
|
|
|
|
|
|
qualityRatio = 2.0
|
2024-05-31 15:10:01 +08:00
|
|
|
|
if imageRequest.Size == "1024x1792" || imageRequest.Size == "1792x1024" {
|
2023-11-09 17:08:32 +08:00
|
|
|
|
qualityRatio = 1.5
|
|
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2023-11-09 17:08:32 +08:00
|
|
|
|
|
2024-07-26 18:51:34 +08:00
|
|
|
|
imageRatio := modelPrice * sizeRatio * qualityRatio * float64(imageRequest.N)
|
|
|
|
|
|
quota := int(imageRatio * groupRatio * common.QuotaPerUnit)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-03-13 22:30:10 +08:00
|
|
|
|
if userQuota-quota < 0 {
|
2024-08-09 18:34:51 +08:00
|
|
|
|
return service.OpenAIErrorWrapperLocal(errors.New("pre-consumed quota is more than user quota"), "insufficient_user_quota", http.StatusForbidden)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
adaptor := GetAdaptor(relayInfo.ApiType)
|
|
|
|
|
|
if adaptor == nil {
|
|
|
|
|
|
return service.OpenAIErrorWrapperLocal(fmt.Errorf("invalid api type: %d", relayInfo.ApiType), "invalid_api_type", http.StatusBadRequest)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
adaptor.Init(relayInfo)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
var requestBody io.Reader
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
convertedRequest, err := adaptor.ConvertImageRequest(c, relayInfo, *imageRequest)
|
2023-06-19 10:28:55 +08:00
|
|
|
|
if err != nil {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return service.OpenAIErrorWrapperLocal(err, "convert_request_failed", http.StatusInternalServerError)
|
2023-06-19 10:28:55 +08:00
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
jsonData, err := json.Marshal(convertedRequest)
|
2023-06-19 10:28:55 +08:00
|
|
|
|
if err != nil {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
return service.OpenAIErrorWrapperLocal(err, "json_marshal_failed", http.StatusInternalServerError)
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
requestBody = bytes.NewBuffer(jsonData)
|
2023-11-27 19:21:16 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
statusCodeMappingStr := c.GetString("status_code_mapping")
|
|
|
|
|
|
resp, err := adaptor.DoRequest(c, relayInfo, requestBody)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return service.OpenAIErrorWrapper(err, "do_request_failed", http.StatusInternalServerError)
|
2023-11-27 19:21:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
if resp != nil {
|
|
|
|
|
|
relayInfo.IsStream = relayInfo.IsStream || strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream")
|
2024-03-13 22:30:10 +08:00
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-07-17 23:50:37 +08:00
|
|
|
|
openaiErr := service.RelayErrorHandler(resp)
|
|
|
|
|
|
// reset status code 重置状态码
|
|
|
|
|
|
service.ResetStatusCode(openaiErr, statusCodeMappingStr)
|
|
|
|
|
|
return openaiErr
|
2023-07-15 12:30:06 +08:00
|
|
|
|
}
|
2023-06-19 10:28:55 +08:00
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
_, openaiErr := adaptor.DoResponse(c, resp, relayInfo)
|
|
|
|
|
|
if openaiErr != nil {
|
|
|
|
|
|
// reset status code 重置状态码
|
|
|
|
|
|
service.ResetStatusCode(openaiErr, statusCodeMappingStr)
|
|
|
|
|
|
return openaiErr
|
2023-06-19 10:28:55 +08:00
|
|
|
|
}
|
2023-07-15 12:30:06 +08:00
|
|
|
|
|
2024-07-17 23:50:37 +08:00
|
|
|
|
usage := &dto.Usage{
|
2024-07-18 17:12:28 +08:00
|
|
|
|
PromptTokens: imageRequest.N,
|
|
|
|
|
|
TotalTokens: imageRequest.N,
|
2023-06-19 10:28:55 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
|
|
|
|
|
|
quality := "standard"
|
|
|
|
|
|
if imageRequest.Quality == "hd" {
|
|
|
|
|
|
quality = "hd"
|
2023-06-19 10:28:55 +08:00
|
|
|
|
}
|
2024-07-17 23:50:37 +08:00
|
|
|
|
|
2024-07-18 00:41:31 +08:00
|
|
|
|
logContent := fmt.Sprintf("大小 %s, 品质 %s", imageRequest.Size, quality)
|
2024-07-26 18:51:34 +08:00
|
|
|
|
postConsumeQuota(c, relayInfo, imageRequest.Model, usage, 0, 0, userQuota, 0, groupRatio, imageRatio, true, logContent)
|
2024-07-17 23:50:37 +08:00
|
|
|
|
|
2023-06-19 10:28:55 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|