Repository: sashabaranov/go-openai
Pull request #530 (2023-11-07 02:45:59)

Support new fields for /v1/images/generation API
**Describe the change**
Updating the [/v1/images/generation](https://platform.openai.com/docs/api-reference/images/create) api with the new changes announced at [devday](https://openai.com/blog/new-models-and-developer-products-announced-at-devday).

**Describe your solution**
Simply add new fields to `ImageRequest` & `ImageResponseDataInner` as well a bunch of new const's for supported options.

**Tests**
Added more fields to the existing `TestImages` test, as these are later unmarshaled. Although if you would like to see tests that simply assert marshalling I could add some, let me know.

Issue: https://github.com/sashabaranov/go-openai/issues/529


vuguet: could you  set the response on this way ? : 

```
	type ImageResponseDataInner struct {
		URL     string `json:"url,omitempty"`
		B64JSON string `json:"b64_json,omitempty"`
		RevisedPrompt string `json:"revised_prompt"`
	}
```

They now return a prompt in the response : 
![image](https://github.com/sashabaranov/go-openai/assets/40727903/eff51101-3a34-4d09-9c11-dad6c1085216)


vuguet: It is not related to your changes and sorry for so many comments but the linter issue is because of this : 
![image](https://github.com/sashabaranov/go-openai/assets/40727903/11468e40-50c4-495d-af94-6804b51cbdc8)

mikecutalo: Nice good catch let me update the response object. There is already a lint fix PR https://github.com/sashabaranov/go-openai/pull/522 thats been opened.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#530](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (924bd06) into [master](https://app.codecov.io/gh/sashabaranov/go-openai/commit/3063e676bf5932024d76be8e8d9e41df06d4e8cc?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3063e67) will **not change** coverage.
> Report is 1 commits behind head on master.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master     #530   +/-   ##
=======================================
  Coverage   97.85%   97.85%           
=======================================
  Files          20       20           
  Lines         978      978           
=======================================
  Hits          957      957           
  Misses         15       15           
  Partials        6        6           
```


| [Files](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [chat.go](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y2hhdC5nbw==) | `100.00% <ø> (ø)` | |
| [chat\_stream.go](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y2hhdF9zdHJlYW0uZ28=) | `100.00% <ø> (ø)` | |
| [image.go](https://app.codecov.io/gh/sashabaranov/go-openai/pull/530?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-aW1hZ2UuZ28=) | `92.59% <ø> (ø)` | |



Patch:
diff --git a/chat.go b/chat.go
index 0430318..609e0c3 100644
--- a/chat.go
+++ b/chat.go
@@ -89,18 +89,18 @@ type ChatCompletionResponseFormat struct {
 
 // ChatCompletionRequest represents a request structure for chat completion API.
 type ChatCompletionRequest struct {
-	Model            string                       `json:"model"`
-	Messages         []ChatCompletionMessage      `json:"messages"`
-	MaxTokens        int                          `json:"max_tokens,omitempty"`
-	Temperature      float32                      `json:"temperature,omitempty"`
-	TopP             float32                      `json:"top_p,omitempty"`
-	N                int                          `json:"n,omitempty"`
-	Stream           bool                         `json:"stream,omitempty"`
-	Stop             []string                     `json:"stop,omitempty"`
-	PresencePenalty  float32                      `json:"presence_penalty,omitempty"`
-	ResponseFormat   ChatCompletionResponseFormat `json:"response_format,omitempty"`
-	Seed             *int                         `json:"seed,omitempty"`
-	FrequencyPenalty float32                      `json:"frequency_penalty,omitempty"`
+	Model            string                        `json:"model"`
+	Messages         []ChatCompletionMessage       `json:"messages"`
+	MaxTokens        int                           `json:"max_tokens,omitempty"`
+	Temperature      float32                       `json:"temperature,omitempty"`
+	TopP             float32                       `json:"top_p,omitempty"`
+	N                int                           `json:"n,omitempty"`
+	Stream           bool                          `json:"stream,omitempty"`
+	Stop             []string                      `json:"stop,omitempty"`
+	PresencePenalty  float32                       `json:"presence_penalty,omitempty"`
+	ResponseFormat   *ChatCompletionResponseFormat `json:"response_format,omitempty"`
+	Seed             *int                          `json:"seed,omitempty"`
+	FrequencyPenalty float32                       `json:"frequency_penalty,omitempty"`
 	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
 	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
 	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
diff --git a/image.go b/image.go
index 4addcdb..4fe8b3a 100644
--- a/image.go
+++ b/image.go
@@ -13,6 +13,9 @@ const (
 	CreateImageSize256x256   = "256x256"
 	CreateImageSize512x512   = "512x512"
 	CreateImageSize1024x1024 = "1024x1024"
+	// dall-e-3 supported only.
+	CreateImageSize1792x1024 = "1792x1024"
+	CreateImageSize1024x1792 = "1024x1792"
 )
 
 const (
@@ -20,11 +23,29 @@ const (
 	CreateImageResponseFormatB64JSON = "b64_json"
 )
 
+const (
+	CreateImageModelDallE2 = "dall-e-2"
+	CreateImageModelDallE3 = "dall-e-3"
+)
+
+const (
+	CreateImageQualityHD       = "hd"
+	CreateImageQualityStandard = "standard"
+)
+
+const (
+	CreateImageStyleVivid   = "vivid"
+	CreateImageStyleNatural = "natural"
+)
+
 // ImageRequest represents the request structure for the image API.
 type ImageRequest struct {
 	Prompt         string `json:"prompt,omitempty"`
+	Model          string `json:"model,omitempty"`
 	N              int    `json:"n,omitempty"`
+	Quality        string `json:"quality,omitempty"`
 	Size           string `json:"size,omitempty"`
+	Style          string `json:"style,omitempty"`
 	ResponseFormat string `json:"response_format,omitempty"`
 	User           string `json:"user,omitempty"`
 }
@@ -39,8 +60,9 @@ type ImageResponse struct {
 
 // ImageResponseDataInner represents a response data structure for image API.
 type ImageResponseDataInner struct {
-	URL     string `json:"url,omitempty"`
-	B64JSON string `json:"b64_json,omitempty"`
+	URL           string `json:"url,omitempty"`
+	B64JSON       string `json:"b64_json,omitempty"`
+	RevisedPrompt string `json:"revised_prompt,omitempty"`
 }
 
 // CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.


Test patch:
diff --git a/image_api_test.go b/image_api_test.go
index 422f831..2eb46f2 100644
--- a/image_api_test.go
+++ b/image_api_test.go
@@ -19,7 +19,14 @@ func TestImages(t *testing.T) {
 	defer teardown()
 	server.RegisterHandler("/v1/images/generations", handleImageEndpoint)
 	_, err := client.CreateImage(context.Background(), openai.ImageRequest{
-		Prompt: "Lorem ipsum",
+		Prompt:         "Lorem ipsum",
+		Model:          openai.CreateImageModelDallE3,
+		N:              1,
+		Quality:        openai.CreateImageQualityHD,
+		Size:           openai.CreateImageSize1024x1024,
+		Style:          openai.CreateImageStyleVivid,
+		ResponseFormat: openai.CreateImageResponseFormatURL,
+		User:           "user",
 	})
 	checks.NoError(t, err, "CreateImage error")
 }