mirror of
https://github.com/linux-do/new-api.git
synced 2025-11-10 16:13:42 +08:00
修改错误提示
This commit is contained in:
@@ -217,4 +217,5 @@ var ChannelBaseURLs = []string{
|
|||||||
"https://api.aiproxy.io", // 21
|
"https://api.aiproxy.io", // 21
|
||||||
"https://fastgpt.run/api/openapi", // 22
|
"https://fastgpt.run/api/openapi", // 22
|
||||||
"https://hunyuan.cloud.tencent.com", //23
|
"https://hunyuan.cloud.tencent.com", //23
|
||||||
|
"", //24
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func GetUsage(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
openAIError := OpenAIError{
|
openAIError := OpenAIError{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Type: "one_api_error",
|
Type: "new_api_error",
|
||||||
}
|
}
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"error": openAIError,
|
"error": openAIError,
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
|
|||||||
if imageRequest.Size != "" && imageRequest.Size != "1024x1024" && imageRequest.Size != "1024x1792" && imageRequest.Size != "1792x1024" {
|
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)
|
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
|
// N should between 1 and 10
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int {
|
|||||||
return len(tokenEncoder.Encode(text, nil, nil))
|
return len(tokenEncoder.Encode(text, nil, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageToken(imageUrl MessageImageUrl) (int, error) {
|
func getImageToken(imageUrl *MessageImageUrl) (int, error) {
|
||||||
if imageUrl.Detail == "low" {
|
if imageUrl.Detail == "low" {
|
||||||
return 85, nil
|
return 85, nil
|
||||||
}
|
}
|
||||||
@@ -90,8 +90,11 @@ func getImageToken(imageUrl MessageImageUrl) (int, error) {
|
|||||||
if config.Width == 0 || config.Height == 0 {
|
if config.Width == 0 || config.Height == 0 {
|
||||||
return 0, errors.New(fmt.Sprintf("fail to decode image config: %s", imageUrl.Url))
|
return 0, errors.New(fmt.Sprintf("fail to decode image config: %s", imageUrl.Url))
|
||||||
}
|
}
|
||||||
|
// TODO: 适配官方auto计费
|
||||||
if config.Width < 512 && config.Height < 512 {
|
if config.Width < 512 && config.Height < 512 {
|
||||||
if imageUrl.Detail == "auto" || imageUrl.Detail == "" {
|
if imageUrl.Detail == "auto" || imageUrl.Detail == "" {
|
||||||
|
// 如果图片尺寸小于512,强制使用low
|
||||||
|
imageUrl.Detail = "low"
|
||||||
return 85, nil
|
return 85, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,7 +160,7 @@ func countTokenMessages(messages []Message, model string) (int, error) {
|
|||||||
} else {
|
} else {
|
||||||
for _, m := range arrayContent {
|
for _, m := range arrayContent {
|
||||||
if m.Type == "image_url" {
|
if m.Type == "image_url" {
|
||||||
imageTokenNum, err := getImageToken(m.ImageUrl)
|
imageTokenNum, err := getImageToken(&m.ImageUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -204,13 +207,14 @@ func errorWrapper(err error, code string, statusCode int) *OpenAIErrorWithStatus
|
|||||||
text := err.Error()
|
text := err.Error()
|
||||||
// 定义一个正则表达式匹配URL
|
// 定义一个正则表达式匹配URL
|
||||||
if strings.Contains(text, "Post") {
|
if strings.Contains(text, "Post") {
|
||||||
|
common.SysLog(fmt.Sprintf("error: %s", text))
|
||||||
text = "请求上游地址失败"
|
text = "请求上游地址失败"
|
||||||
}
|
}
|
||||||
//避免暴露内部错误
|
//避免暴露内部错误
|
||||||
|
|
||||||
openAIError := OpenAIError{
|
openAIError := OpenAIError{
|
||||||
Message: text,
|
Message: text,
|
||||||
Type: "one_api_error",
|
Type: "new_api_error",
|
||||||
Code: code,
|
Code: code,
|
||||||
}
|
}
|
||||||
return &OpenAIErrorWithStatusCode{
|
return &OpenAIErrorWithStatusCode{
|
||||||
|
|||||||
@@ -331,7 +331,7 @@ func RelayMidjourney(c *gin.Context) {
|
|||||||
func RelayNotImplemented(c *gin.Context) {
|
func RelayNotImplemented(c *gin.Context) {
|
||||||
err := OpenAIError{
|
err := OpenAIError{
|
||||||
Message: "API not implemented",
|
Message: "API not implemented",
|
||||||
Type: "one_api_error",
|
Type: "new_api_error",
|
||||||
Param: "",
|
Param: "",
|
||||||
Code: "api_not_implemented",
|
Code: "api_not_implemented",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ func abortWithMessage(c *gin.Context, statusCode int, message string) {
|
|||||||
c.JSON(statusCode, gin.H{
|
c.JSON(statusCode, gin.H{
|
||||||
"error": gin.H{
|
"error": gin.H{
|
||||||
"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
|
"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
|
||||||
"type": "one_api_error",
|
"type": "new_api_error",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
|||||||
Reference in New Issue
Block a user