Updated ImageRequest struct and OpenAIModels,
added new Dall-E models and size ratios
This commit is contained in:
parent
bc7c9105f4
commit
8361fbf4ef
@ -5,6 +5,19 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var DalleSizeRatios = map[string]map[string]float64{
|
||||||
|
"dall-e-2": {
|
||||||
|
"256x256": 1,
|
||||||
|
"512x512": 1.125,
|
||||||
|
"1024x1024": 1.25,
|
||||||
|
},
|
||||||
|
"dall-e-3": {
|
||||||
|
"1024x1024": 1,
|
||||||
|
"1024×1792": 2,
|
||||||
|
"1792×1024": 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// ModelRatio
|
// ModelRatio
|
||||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
||||||
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
|
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
|
||||||
@ -41,7 +54,8 @@ var ModelRatio = map[string]float64{
|
|||||||
"text-search-ada-doc-001": 10,
|
"text-search-ada-doc-001": 10,
|
||||||
"text-moderation-stable": 0.1,
|
"text-moderation-stable": 0.1,
|
||||||
"text-moderation-latest": 0.1,
|
"text-moderation-latest": 0.1,
|
||||||
"dall-e": 8,
|
"dall-e-2": 8, // $0.016 - $0.020 / image
|
||||||
|
"dall-e-3": 20, // ¥0.040 - ¥0.120 / image
|
||||||
"claude-instant-1": 0.815, // $1.63 / 1M tokens
|
"claude-instant-1": 0.815, // $1.63 / 1M tokens
|
||||||
"claude-2": 5.51, // $11.02 / 1M tokens
|
"claude-2": 5.51, // $11.02 / 1M tokens
|
||||||
"ERNIE-Bot": 0.8572, // ¥0.012 / 1k tokens
|
"ERNIE-Bot": 0.8572, // ¥0.012 / 1k tokens
|
||||||
|
@ -55,12 +55,21 @@ func init() {
|
|||||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
||||||
openAIModels = []OpenAIModels{
|
openAIModels = []OpenAIModels{
|
||||||
{
|
{
|
||||||
Id: "dall-e",
|
Id: "dall-e-2",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1677649963,
|
Created: 1677649963,
|
||||||
OwnedBy: "openai",
|
OwnedBy: "openai",
|
||||||
Permission: permission,
|
Permission: permission,
|
||||||
Root: "dall-e",
|
Root: "dall-e-2",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "dall-e-3",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "dall-e-3",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
||||||
imageModel := "dall-e"
|
imageModel := "dall-e-2"
|
||||||
|
requestSize := "1024x1024"
|
||||||
|
|
||||||
tokenId := c.GetInt("token_id")
|
tokenId := c.GetInt("token_id")
|
||||||
channelType := c.GetInt("channel")
|
channelType := c.GetInt("channel")
|
||||||
@ -24,6 +25,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
|
|||||||
group := c.GetString("group")
|
group := c.GetString("group")
|
||||||
|
|
||||||
var imageRequest ImageRequest
|
var imageRequest ImageRequest
|
||||||
|
|
||||||
if consumeQuota {
|
if consumeQuota {
|
||||||
err := common.UnmarshalBodyReusable(c, &imageRequest)
|
err := common.UnmarshalBodyReusable(c, &imageRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -31,21 +33,21 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Model validation
|
||||||
|
if imageRequest.Model != "" || imageRequest.Model != "dall-e-3" {
|
||||||
|
imageModel = "dall-e-2"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size validation
|
||||||
|
if imageRequest.Size != "" {
|
||||||
|
requestSize = imageRequest.Size
|
||||||
|
}
|
||||||
|
|
||||||
// Prompt validation
|
// Prompt validation
|
||||||
if imageRequest.Prompt == "" {
|
if imageRequest.Prompt == "" {
|
||||||
return errorWrapper(errors.New("prompt is required"), "required_field_missing", http.StatusBadRequest)
|
return errorWrapper(errors.New("prompt is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not "256x256", "512x512", or "1024x1024"
|
|
||||||
if imageRequest.Size != "" && imageRequest.Size != "256x256" && imageRequest.Size != "512x512" && imageRequest.Size != "1024x1024" {
|
|
||||||
return errorWrapper(errors.New("size must be one of 256x256, 512x512, or 1024x1024"), "invalid_field_value", http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
// N should between 1 and 10
|
|
||||||
if imageRequest.N != 0 && (imageRequest.N < 1 || imageRequest.N > 10) {
|
|
||||||
return errorWrapper(errors.New("n must be between 1 and 10"), "invalid_field_value", http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
// map model name
|
// map model name
|
||||||
modelMapping := c.GetString("model_mapping")
|
modelMapping := c.GetString("model_mapping")
|
||||||
isModelMapped := false
|
isModelMapped := false
|
||||||
@ -83,14 +85,17 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
|
|||||||
userQuota, err := model.CacheGetUserQuota(userId)
|
userQuota, err := model.CacheGetUserQuota(userId)
|
||||||
|
|
||||||
sizeRatio := 1.0
|
sizeRatio := 1.0
|
||||||
// Size
|
|
||||||
if imageRequest.Size == "256x256" {
|
if ratios, ok := common.DalleSizeRatios[imageModel]; ok {
|
||||||
sizeRatio = 1
|
if ratio, ok := ratios[requestSize]; ok {
|
||||||
} else if imageRequest.Size == "512x512" {
|
sizeRatio = ratio
|
||||||
sizeRatio = 1.125
|
|
||||||
} else if imageRequest.Size == "1024x1024" {
|
if imageRequest.Quality == "hd" {
|
||||||
sizeRatio = 1.25
|
sizeRatio = ratio * 2
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
quota := int(ratio*sizeRatio*1000) * imageRequest.N
|
quota := int(ratio*sizeRatio*1000) * imageRequest.N
|
||||||
|
|
||||||
if consumeQuota && userQuota-quota < 0 {
|
if consumeQuota && userQuota-quota < 0 {
|
||||||
|
@ -78,9 +78,13 @@ type TextRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ImageRequest struct {
|
type ImageRequest struct {
|
||||||
Prompt string `json:"prompt"`
|
Model string `json:"model"`
|
||||||
|
Prompt string `json:"prompt" binding:"required"`
|
||||||
N int `json:"n"`
|
N int `json:"n"`
|
||||||
Size string `json:"size"`
|
Size string `json:"size"`
|
||||||
|
Quality string `json:"quality"`
|
||||||
|
ResponseFormat string `json:"response_format"`
|
||||||
|
Style string `json:"style"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AudioResponse struct {
|
type AudioResponse struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user