-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
263 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright (c) 2022 0x9ef. All rights reserved. | ||
// Use of this source code is governed by an MIT license | ||
// that can be found in the LICENSE file. | ||
package openai | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type ImageCreateOptions struct { | ||
Prompt string `json:"prompt" binding:"required"` | ||
// The number of images to generate. | ||
// Must be between 1 and 10. | ||
N int `json:"n,omitempty" binding:"omitempty,min=1,max=10"` | ||
// The size of the generated images. | ||
// Must be one of 256x256, 512x512, or 1024x1024. | ||
Size string `json:"size,omitempty" binding:"omitempty,oneof=256x256 512x512 1024x1024"` | ||
// The format in which the generated images are returned. | ||
// Must be one of url or b64_json | ||
ResponseFormat string `json:"response_format,omitempty" binding:"omitempty,oneof=url b64_json"` | ||
} | ||
|
||
type ImageCreateResponse struct { | ||
Created int `json:"created"` | ||
Data []struct { | ||
Url string `json:"url"` | ||
} `json:"data"` | ||
} | ||
|
||
// ImageCreate given a prompt and/or an input image, the model will generate a new image. | ||
// | ||
// Docs: https://beta.openai.com/docs/api-reference/images/create | ||
func (e *Engine) ImageCreate(ctx context.Context, opts *ImageCreateOptions) (*ImageCreateResponse, error) { | ||
if err := e.validate.StructCtx(ctx, opts); err != nil { | ||
return nil, err | ||
} | ||
url := e.apiBaseURL + "/images/generations" | ||
r, err := marshalJson(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := e.newReq(ctx, http.MethodPost, url, "json", r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := e.doReq(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var jsonResp ImageCreateResponse | ||
if err := unmarshal(resp, &jsonResp); err != nil { | ||
return nil, err | ||
} | ||
return &jsonResp, nil | ||
} | ||
|
||
type ImageEditOptions struct { | ||
// The image to edit. Must be a valid PNG file, less than 4MB, and square. | ||
// If mask is not provided, image must have transparency, which will be used as the mask. | ||
Image string `binding:"required"` | ||
// 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. | ||
Mask string | ||
// A text description of the desired image(s). The maximum length is 1000 characters. | ||
Prompt string `binding:"required,max=1000"` | ||
// The number of images to generate. | ||
// Must be between 1 and 10. | ||
N int `binding:"omitempty,min=1,max=10"` | ||
// The size of the generated images. | ||
// Must be one of 256x256, 512x512, or 1024x1024. | ||
Size string `binding:"omitempty,oneof=256x256 512x512 1024x1024"` | ||
// The format in which the generated images are returned. | ||
// Must be one of url or b64_json | ||
ResponseFormat string `binding:"omitempty,oneof=url b64_json"` | ||
} | ||
|
||
type ImageEditResponse struct { | ||
Created int `json:"created"` | ||
Data []struct { | ||
Url string `json:"url"` | ||
} `json:"data"` | ||
} | ||
|
||
// ImageEdit creates an edited or extended image given an original image and a prompt. | ||
// | ||
// Docs: https://beta.openai.com/docs/api-reference/images/create-edit | ||
func (e *Engine) ImageEdit(ctx context.Context, opts *ImageEditOptions) (*ImageEditResponse, error) { | ||
if err := e.validate.StructCtx(ctx, opts); err != nil { | ||
return nil, err | ||
} | ||
uri := e.apiBaseURL + "/images/edits" | ||
postValues := url.Values{ | ||
"image": []string{opts.Image}, | ||
"mask": []string{opts.Mask}, | ||
"prompt": []string{opts.Prompt}, | ||
"n": []string{strconv.Itoa(opts.N)}, | ||
"size": []string{opts.Size}, | ||
"response_format": []string{opts.ResponseFormat}, | ||
} | ||
req, err := e.newReq(ctx, http.MethodPost, uri, "formData", strings.NewReader(postValues.Encode())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := e.doReq(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var jsonResp ImageEditResponse | ||
if err := unmarshal(resp, &jsonResp); err != nil { | ||
return nil, err | ||
} | ||
return &jsonResp, nil | ||
} | ||
|
||
type ImageVariationOptions struct { | ||
// The image to edit. Must be a valid PNG file, less than 4MB, and square. | ||
// If mask is not provided, image must have transparency, which will be used as the mask. | ||
Image string `binding:"required"` | ||
// The number of images to generate. | ||
// Must be between 1 and 10. | ||
N int `binding:"omitempty,min=1,max=10"` | ||
// The size of the generated images. | ||
// Must be one of 256x256, 512x512, or 1024x1024. | ||
Size string `binding:"omitempty,oneof=256x256 512x512 1024x1024"` | ||
// The format in which the generated images are returned. | ||
// Must be one of url or b64_json | ||
ResponseFormat string `binding:"omitempty,oneof=url b64_json"` | ||
} | ||
|
||
type ImageVariationResponse struct { | ||
Created int `json:"created"` | ||
Data []struct { | ||
Url string `json:"url"` | ||
} `json:"data"` | ||
} | ||
|
||
// ImageVariation creates a variation of a given image. | ||
// | ||
// Docs: https://beta.openai.com/docs/api-reference/images/create-variation | ||
func (e *Engine) ImageVariation(ctx context.Context, opts *ImageVariationOptions) (*ImageCreateResponse, error) { | ||
if err := e.validate.StructCtx(ctx, opts); err != nil { | ||
return nil, err | ||
} | ||
uri := e.apiBaseURL + "/images/variations" | ||
postValues := url.Values{ | ||
"model": []string{opts.Image}, | ||
"n": []string{strconv.Itoa(opts.N)}, | ||
"size": []string{opts.Size}, | ||
"response_format": []string{opts.ResponseFormat}, | ||
} | ||
req, err := e.newReq(ctx, http.MethodPost, uri, "formData", strings.NewReader(postValues.Encode())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := e.doReq(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var jsonResp ImageCreateResponse | ||
if err := unmarshal(resp, &jsonResp); err != nil { | ||
return nil, err | ||
} | ||
return &jsonResp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2022 0x9ef. All rights reserved. | ||
// Use of this source code is governed by an MIT license | ||
// that can be found in the LICENSE file. | ||
package openai | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestImageCreate(t *testing.T) { | ||
e := New(os.Getenv("OPENAI_KEY")) | ||
r, err := e.ImageCreate(context.Background(), &ImageCreateOptions{ | ||
Prompt: "Write a little bit of Wikipedia. What is that?", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if b, err := json.MarshalIndent(r, "", " "); err != nil { | ||
log.Fatal(err) | ||
} else { | ||
log.Println(string(b)) | ||
} | ||
} | ||
|
||
func TestImageEdit(t *testing.T) { | ||
e := New(os.Getenv("OPENAI_KEY")) | ||
r, err := e.ImageEdit(context.Background(), &ImageEditOptions{ | ||
Image: "000test.png", | ||
Prompt: "Write a little bit of Wikipedia. What is that?", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if b, err := json.MarshalIndent(r, "", " "); err != nil { | ||
log.Fatal(err) | ||
} else { | ||
log.Println(string(b)) | ||
} | ||
} | ||
|
||
func TestImageVariation(t *testing.T) { | ||
e := New(os.Getenv("OPENAI_KEY")) | ||
r, err := e.ImageVariation(context.Background(), &ImageVariationOptions{ | ||
Image: "000test.png", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if b, err := json.MarshalIndent(r, "", " "); err != nil { | ||
log.Fatal(err) | ||
} else { | ||
log.Println(string(b)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters