openai_image.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package dto
  2. import (
  3. "encoding/json"
  4. "one-api/types"
  5. "strings"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type ImageRequest struct {
  9. Model string `json:"model"`
  10. Prompt string `json:"prompt" binding:"required"`
  11. N uint `json:"n,omitempty"`
  12. Size string `json:"size,omitempty"`
  13. Quality string `json:"quality,omitempty"`
  14. ResponseFormat string `json:"response_format,omitempty"`
  15. Style json.RawMessage `json:"style,omitempty"`
  16. User json.RawMessage `json:"user,omitempty"`
  17. ExtraFields json.RawMessage `json:"extra_fields,omitempty"`
  18. Background json.RawMessage `json:"background,omitempty"`
  19. Moderation json.RawMessage `json:"moderation,omitempty"`
  20. OutputFormat json.RawMessage `json:"output_format,omitempty"`
  21. OutputCompression json.RawMessage `json:"output_compression,omitempty"`
  22. PartialImages json.RawMessage `json:"partial_images,omitempty"`
  23. // Stream bool `json:"stream,omitempty"`
  24. Watermark *bool `json:"watermark,omitempty"`
  25. }
  26. func (i *ImageRequest) GetTokenCountMeta() *types.TokenCountMeta {
  27. var sizeRatio = 1.0
  28. var qualityRatio = 1.0
  29. if strings.HasPrefix(i.Model, "dall-e") {
  30. // Size
  31. if i.Size == "256x256" {
  32. sizeRatio = 0.4
  33. } else if i.Size == "512x512" {
  34. sizeRatio = 0.45
  35. } else if i.Size == "1024x1024" {
  36. sizeRatio = 1
  37. } else if i.Size == "1024x1792" || i.Size == "1792x1024" {
  38. sizeRatio = 2
  39. }
  40. if i.Model == "dall-e-3" && i.Quality == "hd" {
  41. qualityRatio = 2.0
  42. if i.Size == "1024x1792" || i.Size == "1792x1024" {
  43. qualityRatio = 1.5
  44. }
  45. }
  46. }
  47. // not support token count for dalle
  48. return &types.TokenCountMeta{
  49. CombineText: i.Prompt,
  50. MaxTokens: 1584,
  51. ImagePriceRatio: sizeRatio * qualityRatio * float64(i.N),
  52. }
  53. }
  54. func (i *ImageRequest) IsStream(c *gin.Context) bool {
  55. return false
  56. }
  57. type ImageResponse struct {
  58. Data []ImageData `json:"data"`
  59. Created int64 `json:"created"`
  60. }
  61. type ImageData struct {
  62. Url string `json:"url"`
  63. B64Json string `json:"b64_json"`
  64. RevisedPrompt string `json:"revised_prompt"`
  65. }