openai_image.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 string `json:"style,omitempty"`
  16. User string `json:"user,omitempty"`
  17. ExtraFields json.RawMessage `json:"extra_fields,omitempty"`
  18. Background string `json:"background,omitempty"`
  19. Moderation string `json:"moderation,omitempty"`
  20. OutputFormat string `json:"output_format,omitempty"`
  21. Watermark *bool `json:"watermark,omitempty"`
  22. }
  23. func (i *ImageRequest) GetTokenCountMeta() *types.TokenCountMeta {
  24. var sizeRatio = 1.0
  25. var qualityRatio = 1.0
  26. if strings.HasPrefix(i.Model, "dall-e") {
  27. // Size
  28. if i.Size == "256x256" {
  29. sizeRatio = 0.4
  30. } else if i.Size == "512x512" {
  31. sizeRatio = 0.45
  32. } else if i.Size == "1024x1024" {
  33. sizeRatio = 1
  34. } else if i.Size == "1024x1792" || i.Size == "1792x1024" {
  35. sizeRatio = 2
  36. }
  37. if i.Model == "dall-e-3" && i.Quality == "hd" {
  38. qualityRatio = 2.0
  39. if i.Size == "1024x1792" || i.Size == "1792x1024" {
  40. qualityRatio = 1.5
  41. }
  42. }
  43. }
  44. // not support token count for dalle
  45. return &types.TokenCountMeta{
  46. CombineText: i.Prompt,
  47. MaxTokens: 1584,
  48. ImagePriceRatio: sizeRatio * qualityRatio * float64(i.N),
  49. }
  50. }
  51. func (i *ImageRequest) IsStream(c *gin.Context) bool {
  52. return false
  53. }
  54. type ImageResponse struct {
  55. Data []ImageData `json:"data"`
  56. Created int64 `json:"created"`
  57. }
  58. type ImageData struct {
  59. Url string `json:"url"`
  60. B64Json string `json:"b64_json"`
  61. RevisedPrompt string `json:"revised_prompt"`
  62. }