CaIon 2 лет назад
Родитель
Сommit
6e744bd1a3

+ 1 - 0
common/constants.go

@@ -217,4 +217,5 @@ var ChannelBaseURLs = []string{
 	"https://api.aiproxy.io",            // 21
 	"https://fastgpt.run/api/openapi",   // 22
 	"https://hunyuan.cloud.tencent.com", //23
+	"",                                  //24
 }

+ 1 - 1
controller/billing.go

@@ -71,7 +71,7 @@ func GetUsage(c *gin.Context) {
 	if err != nil {
 		openAIError := OpenAIError{
 			Message: err.Error(),
-			Type:    "one_api_error",
+			Type:    "new_api_error",
 		}
 		c.JSON(200, gin.H{
 			"error": openAIError,

+ 3 - 0
controller/relay-image.go

@@ -56,6 +56,9 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
 		if imageRequest.Size != "" && imageRequest.Size != "1024x1024" && imageRequest.Size != "1024x1792" && imageRequest.Size != "1792x1024" {
 			return errorWrapper(errors.New("size must be one of 256x256, 512x512, or 1024x1024, dall-e-3 1024x1792 or 1792x1024"), "invalid_field_value", http.StatusBadRequest)
 		}
+		if imageRequest.N != 1 {
+			return errorWrapper(errors.New("n must be 1"), "invalid_field_value", http.StatusBadRequest)
+		}
 	}
 
 	// N should between 1 and 10

+ 7 - 3
controller/relay-utils.go

@@ -70,7 +70,7 @@ func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int {
 	return len(tokenEncoder.Encode(text, nil, nil))
 }
 
-func getImageToken(imageUrl MessageImageUrl) (int, error) {
+func getImageToken(imageUrl *MessageImageUrl) (int, error) {
 	if imageUrl.Detail == "low" {
 		return 85, nil
 	}
@@ -90,8 +90,11 @@ func getImageToken(imageUrl MessageImageUrl) (int, error) {
 	if config.Width == 0 || config.Height == 0 {
 		return 0, errors.New(fmt.Sprintf("fail to decode image config: %s", imageUrl.Url))
 	}
+	// TODO: 适配官方auto计费
 	if config.Width < 512 && config.Height < 512 {
 		if imageUrl.Detail == "auto" || imageUrl.Detail == "" {
+			// 如果图片尺寸小于512,强制使用low
+			imageUrl.Detail = "low"
 			return 85, nil
 		}
 	}
@@ -157,7 +160,7 @@ func countTokenMessages(messages []Message, model string) (int, error) {
 		} else {
 			for _, m := range arrayContent {
 				if m.Type == "image_url" {
-					imageTokenNum, err := getImageToken(m.ImageUrl)
+					imageTokenNum, err := getImageToken(&m.ImageUrl)
 					if err != nil {
 						return 0, err
 					}
@@ -204,13 +207,14 @@ func errorWrapper(err error, code string, statusCode int) *OpenAIErrorWithStatus
 	text := err.Error()
 	// 定义一个正则表达式匹配URL
 	if strings.Contains(text, "Post") {
+		common.SysLog(fmt.Sprintf("error: %s", text))
 		text = "请求上游地址失败"
 	}
 	//避免暴露内部错误
 
 	openAIError := OpenAIError{
 		Message: text,
-		Type:    "one_api_error",
+		Type:    "new_api_error",
 		Code:    code,
 	}
 	return &OpenAIErrorWithStatusCode{

+ 1 - 1
controller/relay.go

@@ -331,7 +331,7 @@ func RelayMidjourney(c *gin.Context) {
 func RelayNotImplemented(c *gin.Context) {
 	err := OpenAIError{
 		Message: "API not implemented",
-		Type:    "one_api_error",
+		Type:    "new_api_error",
 		Param:   "",
 		Code:    "api_not_implemented",
 	}

+ 1 - 1
middleware/utils.go

@@ -9,7 +9,7 @@ func abortWithMessage(c *gin.Context, statusCode int, message string) {
 	c.JSON(statusCode, gin.H{
 		"error": gin.H{
 			"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
-			"type":    "one_api_error",
+			"type":    "new_api_error",
 		},
 	})
 	c.Abort()