openai_image.go 4.5 KB

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