openai_image.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package dto
  2. import (
  3. "encoding/json"
  4. "one-api/common"
  5. "one-api/types"
  6. "reflect"
  7. "strings"
  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 := json.Marshal(alias)
  60. if err != nil {
  61. return nil, err
  62. }
  63. var baseMap map[string]json.RawMessage
  64. if err := json.Unmarshal(base, &baseMap); err != nil {
  65. return nil, err
  66. }
  67. // 合并 ExtraFields
  68. for k, v := range r.Extra {
  69. baseMap[k] = v
  70. }
  71. return json.Marshal(baseMap)
  72. }
  73. func GetJSONFieldNames(t reflect.Type) map[string]struct{} {
  74. fields := make(map[string]struct{})
  75. for i := 0; i < t.NumField(); i++ {
  76. field := t.Field(i)
  77. // 跳过匿名字段(例如 ExtraFields)
  78. if field.Anonymous {
  79. continue
  80. }
  81. tag := field.Tag.Get("json")
  82. if tag == "-" || tag == "" {
  83. continue
  84. }
  85. // 取逗号前字段名(排除 omitempty 等)
  86. name := tag
  87. if commaIdx := indexComma(tag); commaIdx != -1 {
  88. name = tag[:commaIdx]
  89. }
  90. fields[name] = struct{}{}
  91. }
  92. return fields
  93. }
  94. func indexComma(s string) int {
  95. for i := 0; i < len(s); i++ {
  96. if s[i] == ',' {
  97. return i
  98. }
  99. }
  100. return -1
  101. }
  102. func (i *ImageRequest) GetTokenCountMeta() *types.TokenCountMeta {
  103. var sizeRatio = 1.0
  104. var qualityRatio = 1.0
  105. if strings.HasPrefix(i.Model, "dall-e") {
  106. // Size
  107. if i.Size == "256x256" {
  108. sizeRatio = 0.4
  109. } else if i.Size == "512x512" {
  110. sizeRatio = 0.45
  111. } else if i.Size == "1024x1024" {
  112. sizeRatio = 1
  113. } else if i.Size == "1024x1792" || i.Size == "1792x1024" {
  114. sizeRatio = 2
  115. }
  116. if i.Model == "dall-e-3" && i.Quality == "hd" {
  117. qualityRatio = 2.0
  118. if i.Size == "1024x1792" || i.Size == "1792x1024" {
  119. qualityRatio = 1.5
  120. }
  121. }
  122. }
  123. // not support token count for dalle
  124. return &types.TokenCountMeta{
  125. CombineText: i.Prompt,
  126. MaxTokens: 1584,
  127. ImagePriceRatio: sizeRatio * qualityRatio * float64(i.N),
  128. }
  129. }
  130. func (i *ImageRequest) IsStream(c *gin.Context) bool {
  131. return false
  132. }
  133. func (i *ImageRequest) SetModelName(modelName string) {
  134. if modelName != "" {
  135. i.Model = modelName
  136. }
  137. }
  138. type ImageResponse struct {
  139. Data []ImageData `json:"data"`
  140. Created int64 `json:"created"`
  141. Extra any `json:"extra,omitempty"`
  142. }
  143. type ImageData struct {
  144. Url string `json:"url"`
  145. B64Json string `json:"b64_json"`
  146. RevisedPrompt string `json:"revised_prompt"`
  147. }