From f889f004661a86065bb3448e8d6551ec133bfc1b Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:01:11 -0700 Subject: [PATCH 01/10] Updated images.CreationRequest --- images/images.go | 56 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/images/images.go b/images/images.go index 95b2759..7abab53 100644 --- a/images/images.go +++ b/images/images.go @@ -25,9 +25,20 @@ const ( ) const ( - SmallImage string = "256x256" - MediumImage string = "512x512" - LargeImage string = "1024x1024" + Dalle2SmallImage = "256x256" + Dalle2MediumImage = "512x512" + Dalle2LargeImage = "1024x1024" + + Dalle3SquareImage = "1024x1024" + Dalle3LandscapeImage = "1792x1024" + Dalle3PortraitImage = "1024x1792" + + // Deprecated: Use Dalle2SmallImage instead. + SmallImage = Dalle2SmallImage + // Deprecated: Use Dalle2MediumImage instead. + MediumImage = Dalle2MediumImage + // Deprecated: Use Dalle2LargeImage instead. + LargeImage = Dalle2LargeImage ) const ( @@ -35,6 +46,21 @@ const ( ResponseFormatB64JSON = "b64_json" ) +const ( + ModelDalle2 = "dall-e-2" + ModelDalle3 = "dall-e-3" +) + +const ( + QualityStandard = "standard" + QualityHD = "hd" +) + +const ( + StyleVivid = "vivid" + StyleNatural = "natural" +) + // Response structure for the image API endpoint. type Response struct { Created uint64 `json:"created"` @@ -47,18 +73,36 @@ type Response struct { // Request structure for the image creation API endpoint. type CreationRequest struct { - // A text description of the desired image(s). The maximum length is 1000 characters. + // A text description of the desired image(s). + // The maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3. Prompt string `json:"prompt,omitempty"` + // The model to use for image generation. + Model string `json:"model,omitempty"` + // The number of images to generate. Must be between 1 and 10. + // For dall-e-3, only n=1 is supported. N *uint64 `json:"n,omitempty"` - // The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. - Size string `json:"size,omitempty"` + // The quality of the image that will be generated. + // "hd" creates images with finer details and greater consistency across the image. + // This param is only supported for dall-e-3. + Quality string `json:"quality,omitempty"` // The format in which the generated images are returned. Must be one of url or b64_json. ResponseFormat string `json:"response_format,omitempty"` + // The size of the generated images. + // Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. + // Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models. + Size string `json:"size,omitempty"` + + // The style of the generated images. Must be one of vivid or natural. + // Vivid causes the model to lean towards generating hyper-real and dramatic images. + // Natural causes the model to produce more natural, less hyper-real looking images. + // This param is only supported for dall-e-3. + Style string `json:"style,omitempty"` + // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. User string `json:"user,omitempty"` } From 57749940363f7d8b661e4d17cd20196c01036954 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:01:36 -0700 Subject: [PATCH 02/10] Made tools/build.sh more verbose --- tools/build.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/build.sh b/tools/build.sh index f824585..0b4fffa 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -7,7 +7,12 @@ pushd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null ./examples-build.sh pushd .. >/dev/null +echo "Formatting code..." go fmt ./... +echo "Running tests..." go test ./... +echo "Building $(basename "$(pwd)")..." go build ./... +echo "Vetting..." go vet ./... +echo "Done." From b772166c3e4e76daafc9fc87bcdf332dcef6f353 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:03:23 -0700 Subject: [PATCH 03/10] Updated images.Response --- images/images.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/images/images.go b/images/images.go index 7abab53..20a8fd7 100644 --- a/images/images.go +++ b/images/images.go @@ -65,8 +65,9 @@ const ( type Response struct { Created uint64 `json:"created"` Data []struct { - URL string `json:"url"` - B64JSON string `json:"b64_json"` + URL string `json:"url"` + B64JSON string `json:"b64_json"` + RevisedPrompt string `json:"revised_prompt"` } Error *common.ResponseError `json:"error,omitempty"` } From e2f1fd3b208e7178c206c2244be251023528a36a Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:11:02 -0700 Subject: [PATCH 04/10] Updated images.EditRequest --- images/images.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/images/images.go b/images/images.go index 20a8fd7..38f315f 100644 --- a/images/images.go +++ b/images/images.go @@ -156,6 +156,9 @@ type EditRequest struct { // any path information. ImageName string `json:"-"` + // A text description of the desired image(s). The maximum length is 1000 characters. + Prompt string `json:"prompt,omitempty"` + // An additional image whose fully transparent areas (e.g. where alpha is zero) // indicate where image should be edited. Must be a valid PNG file, less than 4MB, // and have the same dimensions as image. @@ -165,8 +168,8 @@ type EditRequest struct { // path information. MaskName string `json:"-"` - // A text description of the desired image(s). The maximum length is 1000 characters. - Prompt string `json:"prompt,omitempty"` + // The model to use for image generation. Only dall-e-2 is supported at this time. + Model string `json:"model,omitempty"` // The number of images to generate. Must be between 1 and 10. N *uint64 `json:"n,omitempty"` From 27d077532ebe3df10971e744c17ad46eb449ec72 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:14:06 -0700 Subject: [PATCH 05/10] Updated examples and tests to no longer use deprecated constants --- examples/images/images-example.go | 4 ++-- images/images_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/images/images-example.go b/examples/images/images-example.go index 28bdd01..2c3bd44 100644 --- a/examples/images/images-example.go +++ b/examples/images/images-example.go @@ -22,7 +22,7 @@ func create() (*images.Response, error) { fmt.Printf("Creating from prompt: %s\n", prompt) resp, _, err := images.MakeModeratedCreationRequest(&images.CreationRequest{ Prompt: prompt, - Size: images.SmallImage, + Size: images.Dalle2SmallImage, User: "https://github.com/TannerKvarfordt/gopenai", }, nil) if err != nil { @@ -39,7 +39,7 @@ func variation(imagename, image string) error { resp, err := images.MakeVariationRequest(&images.VariationRequest{ Image: image, ImageName: imagename, - Size: images.SmallImage, + Size: images.Dalle2SmallImage, User: "https://github.com/TannerKvarfordt/gopenai", }, nil) if err != nil { diff --git a/images/images_test.go b/images/images_test.go index f775901..25146a2 100644 --- a/images/images_test.go +++ b/images/images_test.go @@ -24,7 +24,7 @@ func create() (*images.Response, error) { fmt.Printf("Creating from prompt: %s\n", prompt) resp, err := images.MakeCreationRequest(&images.CreationRequest{ Prompt: prompt, - Size: images.SmallImage, + Size: images.Dalle2SmallImage, User: "https://github.com/TannerKvarfordt/gopenai", }, nil) if err != nil { @@ -44,7 +44,7 @@ func variation(imagename, image string) error { resp, err := images.MakeVariationRequest(&images.VariationRequest{ Image: image, ImageName: imagename, - Size: images.SmallImage, + Size: images.Dalle2SmallImage, User: "https://github.com/TannerKvarfordt/gopenai", }, nil) if err != nil { From 33f851948ac2e783fc1655a6275f7c7ed7df0579 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:22:16 -0700 Subject: [PATCH 06/10] Updated link to sashabaranov's project in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81b511b..266c8aa 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ If you use this library, you must conform to Open AI's [Usage Policies](https:// ## Other Language Bindings -For another great Go implementation, see [sashabaranov/go-gpt3](https://github.com/sashabaranov/go-gpt3). +For another great Go implementation, see [sashabaranov/go-openai](https://github.com/sashabaranov/go-openai). For other languages, see [Open AI's Website](https://beta.openai.com/docs/libraries/libraries). ## Contributing From fe93f0115ec5f7df7da77c7fc8a38064a788b781 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:28:41 -0700 Subject: [PATCH 07/10] Updated images.MakeEditRequest --- images/images.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/images/images.go b/images/images.go index 38f315f..df0d039 100644 --- a/images/images.go +++ b/images/images.go @@ -193,14 +193,15 @@ func MakeEditRequest(request *EditRequest, organizationID *string) (*Response, e buf := new(bytes.Buffer) writer := multipart.NewWriter(buf) + var err error + if len(request.Prompt) > 0 { - err := common.CreateFormField("prompt", request.Prompt, writer) + err = common.CreateFormField("prompt", request.Prompt, writer) if err != nil { return nil, err } } - var err error if request.N != nil { err = common.CreateFormField("n", request.N, writer) if err != nil { @@ -229,6 +230,13 @@ func MakeEditRequest(request *EditRequest, organizationID *string) (*Response, e } } + if len(request.Model) > 0 { + err = common.CreateFormField("model", request.Model, writer) + if err != nil { + return nil, err + } + } + if len(request.Image) > 0 { err = common.CreateFormFile("image", request.ImageName, request.Image, writer) if err != nil { From aa92ec0f74391630918d65998046902e60c31006 Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:32:34 -0700 Subject: [PATCH 08/10] Updated images.VariationRequest and images.MakeVariationRequest --- images/images.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/images/images.go b/images/images.go index df0d039..3e7e6b1 100644 --- a/images/images.go +++ b/images/images.go @@ -296,6 +296,9 @@ type VariationRequest struct { // any path information. ImageName string `json:"-"` + // The model to use for image generation. Only dall-e-2 is supported at this time. + Model string `json:"model,omitempty"` + // The number of images to generate. Must be between 1 and 10. N *uint64 `json:"n,omitempty"` @@ -319,6 +322,7 @@ func MakeVariationRequest(request *VariationRequest, organizationID *string) (*R writer := multipart.NewWriter(buf) var err error + if request.N != nil { err = common.CreateFormField("n", request.N, writer) if err != nil { @@ -354,6 +358,13 @@ func MakeVariationRequest(request *VariationRequest, organizationID *string) (*R } } + if len(request.Model) > 0 { + err = common.CreateFormField("model", request.Model, writer) + if err != nil { + return nil, err + } + } + writer.Close() r, err := common.MakeRequestWithForm[Response](buf, VariationEndpoint, http.MethodPost, writer.FormDataContentType(), organizationID) if err != nil { From c14c08ae51ed48354acb5c4729ccb0e3dd29ae3d Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:40:31 -0700 Subject: [PATCH 09/10] Updated images_test --- images/images_test.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/images/images_test.go b/images/images_test.go index 25146a2..18fa86b 100644 --- a/images/images_test.go +++ b/images/images_test.go @@ -18,14 +18,15 @@ func init() { authentication.SetAPIKey(key) } -func create() (*images.Response, error) { +func create(model, size string) (*images.Response, error) { const prompt = "A cute baby sea otter" fmt.Printf("Creating from prompt: %s\n", prompt) resp, err := images.MakeCreationRequest(&images.CreationRequest{ Prompt: prompt, - Size: images.Dalle2SmallImage, + Size: size, User: "https://github.com/TannerKvarfordt/gopenai", + Model: model, }, nil) if err != nil { return nil, err @@ -38,7 +39,7 @@ func create() (*images.Response, error) { return resp, nil } -func variation(imagename, image string) error { +func variation(model, imagename, image string) error { fmt.Printf("Generating a variation...") resp, err := images.MakeVariationRequest(&images.VariationRequest{ @@ -46,6 +47,7 @@ func variation(imagename, image string) error { ImageName: imagename, Size: images.Dalle2SmallImage, User: "https://github.com/TannerKvarfordt/gopenai", + Model: model, }, nil) if err != nil { return err @@ -58,13 +60,20 @@ func variation(imagename, image string) error { return nil } -func TestImages(t *testing.T) { - resp, err := create() +func TestImagesDalle2(t *testing.T) { + resp, err := create(images.ModelDalle2, images.Dalle2SmallImage) if err != nil { t.Fatal(err) } - err = variation("Original", resp.Data[0].URL) + err = variation(images.ModelDalle2, "Original", resp.Data[0].URL) + if err != nil { + t.Fatal(err) + } +} + +func TestImagesDalle3(t *testing.T) { + _, err := create(images.ModelDalle3, images.Dalle3SquareImage) if err != nil { t.Fatal(err) } From 7c4a776616a1eacca0fc4699eb1757b5a7fcc42f Mon Sep 17 00:00:00 2001 From: Tanner Kvarfordt Date: Mon, 6 Nov 2023 22:50:26 -0700 Subject: [PATCH 10/10] Updated images-example.go --- examples/images/images-example.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/images/images-example.go b/examples/images/images-example.go index 2c3bd44..90a0a93 100644 --- a/examples/images/images-example.go +++ b/examples/images/images-example.go @@ -16,14 +16,15 @@ func init() { authentication.SetAPIKey(key) } -func create() (*images.Response, error) { +func create(model, size string) (*images.Response, error) { const prompt = "A cute baby sea otter" - fmt.Printf("Creating from prompt: %s\n", prompt) + fmt.Printf("Creating from model=\"%s\", prompt=\"%s\"\n", model, prompt) resp, _, err := images.MakeModeratedCreationRequest(&images.CreationRequest{ Prompt: prompt, - Size: images.Dalle2SmallImage, + Size: size, User: "https://github.com/TannerKvarfordt/gopenai", + Model: model, }, nil) if err != nil { return nil, err @@ -51,7 +52,7 @@ func variation(imagename, image string) error { } func main() { - resp, err := create() + resp, err := create(images.ModelDalle2, images.Dalle2SmallImage) if err != nil { fmt.Println(err) return @@ -62,4 +63,10 @@ func main() { fmt.Println(err) return } + + _, err = create(images.ModelDalle3, images.Dalle3SquareImage) + if err != nil { + fmt.Println(err) + return + } }