| package dto |
|
|
| import ( |
| "encoding/json" |
| "reflect" |
| "strings" |
|
|
| "github.com/QuantumNous/new-api/common" |
| "github.com/QuantumNous/new-api/types" |
|
|
| "github.com/gin-gonic/gin" |
| ) |
|
|
| type ImageRequest struct { |
| Model string `json:"model"` |
| Prompt string `json:"prompt" binding:"required"` |
| N uint `json:"n,omitempty"` |
| Size string `json:"size,omitempty"` |
| Quality string `json:"quality,omitempty"` |
| ResponseFormat string `json:"response_format,omitempty"` |
| Style json.RawMessage `json:"style,omitempty"` |
| User json.RawMessage `json:"user,omitempty"` |
| ExtraFields json.RawMessage `json:"extra_fields,omitempty"` |
| Background json.RawMessage `json:"background,omitempty"` |
| Moderation json.RawMessage `json:"moderation,omitempty"` |
| OutputFormat json.RawMessage `json:"output_format,omitempty"` |
| OutputCompression json.RawMessage `json:"output_compression,omitempty"` |
| PartialImages json.RawMessage `json:"partial_images,omitempty"` |
| |
| Watermark *bool `json:"watermark,omitempty"` |
| Image json.RawMessage `json:"image,omitempty"` |
| |
| Extra map[string]json.RawMessage `json:"-"` |
| } |
|
|
| func (i *ImageRequest) UnmarshalJSON(data []byte) error { |
| |
| var rawMap map[string]json.RawMessage |
| if err := common.Unmarshal(data, &rawMap); err != nil { |
| return err |
| } |
|
|
| |
| knownFields := GetJSONFieldNames(reflect.TypeOf(*i)) |
|
|
| |
| type Alias ImageRequest |
| var known Alias |
| if err := common.Unmarshal(data, &known); err != nil { |
| return err |
| } |
| *i = ImageRequest(known) |
|
|
| |
| i.Extra = make(map[string]json.RawMessage) |
| for k, v := range rawMap { |
| if _, ok := knownFields[k]; !ok { |
| i.Extra[k] = v |
| } |
| } |
| return nil |
| } |
|
|
| |
| func (r ImageRequest) MarshalJSON() ([]byte, error) { |
| |
| type Alias ImageRequest |
| alias := Alias(r) |
| base, err := common.Marshal(alias) |
| if err != nil { |
| return nil, err |
| } |
|
|
| var baseMap map[string]json.RawMessage |
| if err := common.Unmarshal(base, &baseMap); err != nil { |
| return nil, err |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| return common.Marshal(baseMap) |
| } |
|
|
| func GetJSONFieldNames(t reflect.Type) map[string]struct{} { |
| fields := make(map[string]struct{}) |
| for i := 0; i < t.NumField(); i++ { |
| field := t.Field(i) |
|
|
| |
| if field.Anonymous { |
| continue |
| } |
|
|
| tag := field.Tag.Get("json") |
| if tag == "-" || tag == "" { |
| continue |
| } |
|
|
| |
| name := tag |
| if commaIdx := indexComma(tag); commaIdx != -1 { |
| name = tag[:commaIdx] |
| } |
| fields[name] = struct{}{} |
| } |
| return fields |
| } |
|
|
| func indexComma(s string) int { |
| for i := 0; i < len(s); i++ { |
| if s[i] == ',' { |
| return i |
| } |
| } |
| return -1 |
| } |
|
|
| func (i *ImageRequest) GetTokenCountMeta() *types.TokenCountMeta { |
| var sizeRatio = 1.0 |
| var qualityRatio = 1.0 |
|
|
| if strings.HasPrefix(i.Model, "dall-e") { |
| |
| if i.Size == "256x256" { |
| sizeRatio = 0.4 |
| } else if i.Size == "512x512" { |
| sizeRatio = 0.45 |
| } else if i.Size == "1024x1024" { |
| sizeRatio = 1 |
| } else if i.Size == "1024x1792" || i.Size == "1792x1024" { |
| sizeRatio = 2 |
| } |
|
|
| if i.Model == "dall-e-3" && i.Quality == "hd" { |
| qualityRatio = 2.0 |
| if i.Size == "1024x1792" || i.Size == "1792x1024" { |
| qualityRatio = 1.5 |
| } |
| } |
| } |
|
|
| |
| return &types.TokenCountMeta{ |
| CombineText: i.Prompt, |
| MaxTokens: 1584, |
| ImagePriceRatio: sizeRatio * qualityRatio * float64(i.N), |
| } |
| } |
|
|
| func (i *ImageRequest) IsStream(c *gin.Context) bool { |
| return false |
| } |
|
|
| func (i *ImageRequest) SetModelName(modelName string) { |
| if modelName != "" { |
| i.Model = modelName |
| } |
| } |
|
|
| type ImageResponse struct { |
| Data []ImageData `json:"data"` |
| Created int64 `json:"created"` |
| Extra any `json:"extra,omitempty"` |
| } |
| type ImageData struct { |
| Url string `json:"url,omitempty"` |
| B64Json string `json:"b64_json,omitempty"` |
| RevisedPrompt string `json:"revised_prompt,omitempty"` |
| } |
|
|