openai_image.go 4.3 KB

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