openai_image.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package dto
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/types"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type ImageRequest struct {
  11. Model string `json:"model"`
  12. Prompt string `json:"prompt" binding:"required"`
  13. N uint `json:"n,omitempty"`
  14. Size string `json:"size,omitempty"`
  15. Quality string `json:"quality,omitempty"`
  16. ResponseFormat string `json:"response_format,omitempty"`
  17. Style json.RawMessage `json:"style,omitempty"`
  18. User json.RawMessage `json:"user,omitempty"`
  19. ExtraFields json.RawMessage `json:"extra_fields,omitempty"`
  20. Background json.RawMessage `json:"background,omitempty"`
  21. Moderation json.RawMessage `json:"moderation,omitempty"`
  22. OutputFormat json.RawMessage `json:"output_format,omitempty"`
  23. OutputCompression json.RawMessage `json:"output_compression,omitempty"`
  24. PartialImages json.RawMessage `json:"partial_images,omitempty"`
  25. // Stream bool `json:"stream,omitempty"`
  26. Watermark *bool `json:"watermark,omitempty"`
  27. Image json.RawMessage `json:"image,omitempty"`
  28. // 用匿名参数接收额外参数
  29. Extra map[string]json.RawMessage `json:"-"`
  30. }
  31. func (i *ImageRequest) UnmarshalJSON(data []byte) error {
  32. // 先解析成 map[string]interface{}
  33. var rawMap map[string]json.RawMessage
  34. if err := common.Unmarshal(data, &rawMap); err != nil {
  35. return err
  36. }
  37. // 用 struct tag 获取所有已定义字段名
  38. knownFields := GetJSONFieldNames(reflect.TypeOf(*i))
  39. // 再正常解析已定义字段
  40. type Alias ImageRequest
  41. var known Alias
  42. if err := common.Unmarshal(data, &known); err != nil {
  43. return err
  44. }
  45. *i = ImageRequest(known)
  46. // 提取多余字段
  47. i.Extra = make(map[string]json.RawMessage)
  48. for k, v := range rawMap {
  49. if _, ok := knownFields[k]; !ok {
  50. i.Extra[k] = v
  51. }
  52. }
  53. return nil
  54. }
  55. // 序列化时需要重新把字段平铺
  56. func (r ImageRequest) MarshalJSON() ([]byte, error) {
  57. // 将已定义字段转为 map
  58. type Alias ImageRequest
  59. alias := Alias(r)
  60. base, err := common.Marshal(alias)
  61. if err != nil {
  62. return nil, err
  63. }
  64. var baseMap map[string]json.RawMessage
  65. if err := common.Unmarshal(base, &baseMap); err != nil {
  66. return nil, err
  67. }
  68. // 不能合并ExtraFields!!!!!!!!
  69. // 合并 ExtraFields
  70. //for k, v := range r.Extra {
  71. // if _, exists := baseMap[k]; !exists {
  72. // baseMap[k] = v
  73. // }
  74. //}
  75. return common.Marshal(baseMap)
  76. }
  77. func GetJSONFieldNames(t reflect.Type) map[string]struct{} {
  78. fields := make(map[string]struct{})
  79. for i := 0; i < t.NumField(); i++ {
  80. field := t.Field(i)
  81. // 跳过匿名字段(例如 ExtraFields)
  82. if field.Anonymous {
  83. continue
  84. }
  85. tag := field.Tag.Get("json")
  86. if tag == "-" || tag == "" {
  87. continue
  88. }
  89. // 取逗号前字段名(排除 omitempty 等)
  90. name := tag
  91. if commaIdx := indexComma(tag); commaIdx != -1 {
  92. name = tag[:commaIdx]
  93. }
  94. fields[name] = struct{}{}
  95. }
  96. return fields
  97. }
  98. func indexComma(s string) int {
  99. for i := 0; i < len(s); i++ {
  100. if s[i] == ',' {
  101. return i
  102. }
  103. }
  104. return -1
  105. }
  106. func (i *ImageRequest) GetTokenCountMeta() *types.TokenCountMeta {
  107. var sizeRatio = 1.0
  108. var qualityRatio = 1.0
  109. if strings.HasPrefix(i.Model, "dall-e") {
  110. // Size
  111. if i.Size == "256x256" {
  112. sizeRatio = 0.4
  113. } else if i.Size == "512x512" {
  114. sizeRatio = 0.45
  115. } else if i.Size == "1024x1024" {
  116. sizeRatio = 1
  117. } else if i.Size == "1024x1792" || i.Size == "1792x1024" {
  118. sizeRatio = 2
  119. }
  120. if i.Model == "dall-e-3" && i.Quality == "hd" {
  121. qualityRatio = 2.0
  122. if i.Size == "1024x1792" || i.Size == "1792x1024" {
  123. qualityRatio = 1.5
  124. }
  125. }
  126. }
  127. // not support token count for dalle
  128. return &types.TokenCountMeta{
  129. CombineText: i.Prompt,
  130. MaxTokens: 1584,
  131. ImagePriceRatio: sizeRatio * qualityRatio * float64(i.N),
  132. }
  133. }
  134. func (i *ImageRequest) IsStream(c *gin.Context) bool {
  135. return false
  136. }
  137. func (i *ImageRequest) SetModelName(modelName string) {
  138. if modelName != "" {
  139. i.Model = modelName
  140. }
  141. }
  142. type ImageResponse struct {
  143. Data []ImageData `json:"data"`
  144. Created int64 `json:"created"`
  145. Extra any `json:"extra,omitempty"`
  146. }
  147. type ImageData struct {
  148. Url string `json:"url"`
  149. B64Json string `json:"b64_json"`
  150. RevisedPrompt string `json:"revised_prompt"`
  151. }