-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimages.go
133 lines (117 loc) · 5.29 KB
/
images.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package openai
import (
"context"
"encoding/json"
"github.com/fabiustech/openai/images"
"github.com/fabiustech/openai/routes"
)
// CreateImageRequest contains all relevant fields for requests to the images/generations endpoint.
type CreateImageRequest struct {
// Prompt is a text description of the desired image(s). The maximum length is 1000 characters.
Prompt string `json:"prompt"`
// N specifies the number of images to generate. Must be between 1 and 10.
// Defaults to 1.
N int `json:"n,omitempty"`
// Size specifies the size of the generated images. Must be one of images.Size256x256, images.Size512x512, or
// images.Size1024x1024.
// Defaults to images.Size1024x1024.
Size images.Size `json:"size,omitempty"`
// ResponseFormat specifies the format in which the generated images are returned. Must be one of images.FormatURL
// or images.FormatB64JSON.
// Defaults to images.FormatURL.
ResponseFormat images.Format `json:"response_format,omitempty"`
// User specifies a unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse:
// https://beta.openai.com/docs/guides/safety-best-practices/end-user-ids.
User string `json:"user,omitempty"`
}
// EditImageRequest contains all relevant fields for requests to the images/edits endpoint.
type EditImageRequest struct {
// Image is 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 `json:"image"`
// Mask is 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 `json:"mask,omitempty"`
// Prompt is a text description of the desired image(s). The maximum length is 1000 characters.
Prompt string `json:"prompt"`
// N specifies the number of images to generate. Must be between 1 and 10.
// Defaults to 1.
N int `json:"n,omitempty"`
// Size specifies the size of the generated images. Must be one of images.Size256x256, images.Size512x512, or
// images.Size1024x1024.
// Defaults to images.Size1024x1024.
Size images.Size `json:"size,omitempty"`
// ResponseFormat specifies the format in which the generated images are returned. Must be one of images.FormatURL
// or images.FormatB64JSON.
// Defaults to images.FormatURL.
ResponseFormat images.Format `json:"response_format,omitempty"`
// User specifies a unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse:
// https://beta.openai.com/docs/guides/safety-best-practices/end-user-ids.
User string `json:"user,omitempty"`
}
// VariationImageRequest contains all relevant fields for requests to the images/variations endpoint.
type VariationImageRequest struct {
// Image is the image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
Image string `json:"image"`
// N specifies the number of images to generate. Must be between 1 and 10.
// Defaults to 1.
N int `json:"n,omitempty"`
// Size specifies the size of the generated images. Must be one of images.Size256x256, images.Size512x512, or
// images.Size1024x1024.
// Defaults to images.Size1024x1024.
Size images.Size `json:"size,omitempty"`
// ResponseFormat specifies the format in which the generated images are returned. Must be one of images.FormatURL
// or images.FormatB64JSON.
// Defaults to images.FormatURL.
ResponseFormat images.Format `json:"response_format,omitempty"`
// User specifies a unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse:
// https://beta.openai.com/docs/guides/safety-best-practices/end-user-ids.
User string `json:"user,omitempty"`
}
// ImageResponse represents a response structure for image API.
type ImageResponse struct {
Created uint64 `json:"created,omitempty"`
Data []*ImageData `json:"data,omitempty"`
}
// ImageData represents a response data structure for image API.
// Only one field will be non-nil.
type ImageData struct {
URL *string `json:"url,omitempty"`
B64JSON *string `json:"b64_json,omitempty"`
}
// CreateImage creates an image (or images) given a prompt.
func (c *Client) CreateImage(ctx context.Context, ir *CreateImageRequest) (*ImageResponse, error) {
var b, err = c.post(ctx, routes.ImageGenerations, ir)
if err != nil {
return nil, err
}
var resp = &ImageResponse{}
if err = json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}
// EditImage creates an edited or extended image (or images) given an original image and a prompt.
func (c *Client) EditImage(ctx context.Context, eir *EditImageRequest) (*ImageResponse, error) {
var b, err = c.post(ctx, routes.ImageEdits, eir)
if err != nil {
return nil, err
}
var resp = &ImageResponse{}
if err = json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}
// ImageVariation creates a variation (or variations) of a given image.
func (c *Client) ImageVariation(ctx context.Context, vir *VariationImageRequest) (*ImageResponse, error) {
var b, err = c.post(ctx, routes.ImageVariations, vir)
if err != nil {
return nil, err
}
var resp = &ImageResponse{}
if err = json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}