add: add images api

This commit is contained in:
Martial BE
2023-12-01 17:20:22 +08:00
parent 5b70ee3407
commit 9dd92bbddd
19 changed files with 296 additions and 12 deletions

View File

@@ -117,7 +117,7 @@ func SendRequest(req *http.Request, response any, outputResp bool) (*http.Respon
// 将响应体重新写入 resp.Body
resp.Body = io.NopCloser(&buf)
} else {
err = DecodeResponse(resp.Body, nil)
err = DecodeResponse(resp.Body, response)
}
if err != nil {
return nil, types.ErrorWrapper(err, "decode_response_failed", http.StatusInternalServerError)

View File

@@ -222,6 +222,8 @@ const (
RelayModeEmbeddings
RelayModeModerations
RelayModeImagesGenerations
RelayModeImagesEdit
RelayModeImagesVariations
RelayModeEdits
RelayModeAudioSpeech
RelayModeAudioTranscription

View File

@@ -1,6 +1,7 @@
package common
import (
"errors"
"fmt"
"strings"
@@ -107,3 +108,21 @@ func CountTokenText(text string, model string) int {
tokenEncoder := getTokenEncoder(model)
return getTokenNum(tokenEncoder, text)
}
func CountTokenImage(imageRequest types.ImageRequest) (int, error) {
imageCostRatio, hasValidSize := DalleSizeRatios[imageRequest.Model][imageRequest.Size]
if hasValidSize {
if imageRequest.Quality == "hd" && imageRequest.Model == "dall-e-3" {
if imageRequest.Size == "1024x1024" {
imageCostRatio *= 2
} else {
imageCostRatio *= 1.5
}
}
} else {
return 0, errors.New("size not supported for this image model")
}
return int(imageCostRatio*1000) * imageRequest.N, nil
}