go-chatgpt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

161 lines
4.2 KiB

  1. package gogpt
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "mime/multipart"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. )
  11. // Image sizes defined by the OpenAI API.
  12. const (
  13. CreateImageSize256x256 = "256x256"
  14. CreateImageSize512x512 = "512x512"
  15. CreateImageSize1024x1024 = "1024x1024"
  16. )
  17. const (
  18. CreateImageResponseFormatURL = "url"
  19. CreateImageResponseFormatB64JSON = "b64_json"
  20. )
  21. // ImageRequest represents the request structure for the image API.
  22. type ImageRequest struct {
  23. Prompt string `json:"prompt,omitempty"`
  24. N int `json:"n,omitempty"`
  25. Size string `json:"size,omitempty"`
  26. ResponseFormat string `json:"response_format,omitempty"`
  27. User string `json:"user,omitempty"`
  28. }
  29. // ImageResponse represents a response structure for image API.
  30. type ImageResponse struct {
  31. Created int64 `json:"created,omitempty"`
  32. Data []ImageResponseDataInner `json:"data,omitempty"`
  33. }
  34. // ImageResponseData represents a response data structure for image API.
  35. type ImageResponseDataInner struct {
  36. URL string `json:"url,omitempty"`
  37. B64JSON string `json:"b64_json,omitempty"`
  38. }
  39. // CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.
  40. func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error) {
  41. urlSuffix := "/images/generations"
  42. req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
  43. if err != nil {
  44. return
  45. }
  46. err = c.sendRequest(req, &response)
  47. return
  48. }
  49. // ImageEditRequest represents the request structure for the image API.
  50. type ImageEditRequest struct {
  51. Image *os.File `json:"image,omitempty"`
  52. Mask *os.File `json:"mask,omitempty"`
  53. Prompt string `json:"prompt,omitempty"`
  54. N int `json:"n,omitempty"`
  55. Size string `json:"size,omitempty"`
  56. }
  57. // CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API.
  58. func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error) {
  59. body := &bytes.Buffer{}
  60. writer := multipart.NewWriter(body)
  61. // image
  62. image, err := writer.CreateFormFile("image", request.Image.Name())
  63. if err != nil {
  64. return
  65. }
  66. _, err = io.Copy(image, request.Image)
  67. if err != nil {
  68. return
  69. }
  70. // mask, it is optional
  71. if request.Mask != nil {
  72. mask, err2 := writer.CreateFormFile("mask", request.Mask.Name())
  73. if err2 != nil {
  74. return
  75. }
  76. _, err = io.Copy(mask, request.Mask)
  77. if err != nil {
  78. return
  79. }
  80. }
  81. err = writer.WriteField("prompt", request.Prompt)
  82. if err != nil {
  83. return
  84. }
  85. err = writer.WriteField("n", strconv.Itoa(request.N))
  86. if err != nil {
  87. return
  88. }
  89. err = writer.WriteField("size", request.Size)
  90. if err != nil {
  91. return
  92. }
  93. writer.Close()
  94. urlSuffix := "/images/edits"
  95. req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), body)
  96. if err != nil {
  97. return
  98. }
  99. req.Header.Set("Content-Type", writer.FormDataContentType())
  100. err = c.sendRequest(req, &response)
  101. return
  102. }
  103. // ImageVariRequest represents the request structure for the image API.
  104. type ImageVariRequest struct {
  105. Image *os.File `json:"image,omitempty"`
  106. N int `json:"n,omitempty"`
  107. Size string `json:"size,omitempty"`
  108. }
  109. // CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API.
  110. // Use abbreviations(vari for variation) because ci-lint has a single-line length limit ...
  111. func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error) {
  112. body := &bytes.Buffer{}
  113. writer := multipart.NewWriter(body)
  114. // image
  115. image, err := writer.CreateFormFile("image", request.Image.Name())
  116. if err != nil {
  117. return
  118. }
  119. _, err = io.Copy(image, request.Image)
  120. if err != nil {
  121. return
  122. }
  123. err = writer.WriteField("n", strconv.Itoa(request.N))
  124. if err != nil {
  125. return
  126. }
  127. err = writer.WriteField("size", request.Size)
  128. if err != nil {
  129. return
  130. }
  131. writer.Close()
  132. //https://platform.gogpt.com/docs/api-reference/images/create-variation
  133. urlSuffix := "/images/variations"
  134. req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), body)
  135. if err != nil {
  136. return
  137. }
  138. req.Header.Set("Content-Type", writer.FormDataContentType())
  139. err = c.sendRequest(req, &response)
  140. return
  141. }