forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
168 lines (140 loc) · 4.27 KB
/
image.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package openai
import (
"bytes"
"context"
"net/http"
"os"
"strconv"
)
// Image sizes defined by the OpenAI API.
const (
CreateImageSize256x256 = "256x256"
CreateImageSize512x512 = "512x512"
CreateImageSize1024x1024 = "1024x1024"
)
const (
CreateImageResponseFormatURL = "url"
CreateImageResponseFormatB64JSON = "b64_json"
)
// ImageRequest represents the request structure for the image API.
type ImageRequest struct {
Prompt string `json:"prompt,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
}
// ImageResponse represents a response structure for image API.
type ImageResponse struct {
Created int64 `json:"created,omitempty"`
Data []ImageResponseDataInner `json:"data,omitempty"`
}
// ImageResponseDataInner represents a response data structure for image API.
type ImageResponseDataInner struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
}
// CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.
func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error) {
urlSuffix := "/images/generations"
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
// ImageEditRequest represents the request structure for the image API.
type ImageEditRequest struct {
Image *os.File `json:"image,omitempty"`
Mask *os.File `json:"mask,omitempty"`
Prompt string `json:"prompt,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
}
// CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API.
func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error) {
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)
// image
err = builder.CreateFormFile("image", request.Image)
if err != nil {
return
}
// mask, it is optional
if request.Mask != nil {
err = builder.CreateFormFile("mask", request.Mask)
if err != nil {
return
}
}
err = builder.WriteField("prompt", request.Prompt)
if err != nil {
return
}
err = builder.WriteField("n", strconv.Itoa(request.N))
if err != nil {
return
}
err = builder.WriteField("size", request.Size)
if err != nil {
return
}
err = builder.WriteField("response_format", request.ResponseFormat)
if err != nil {
return
}
err = builder.Close()
if err != nil {
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/images/edits"),
withBody(body), withContentType(builder.FormDataContentType()))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
// ImageVariRequest represents the request structure for the image API.
type ImageVariRequest struct {
Image *os.File `json:"image,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
}
// CreateVariImage - API call to create an image variation. This is the main endpoint of the DALL-E API.
// Use abbreviations(vari for variation) because ci-lint has a single-line length limit ...
func (c *Client) CreateVariImage(ctx context.Context, request ImageVariRequest) (response ImageResponse, err error) {
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)
// image
err = builder.CreateFormFile("image", request.Image)
if err != nil {
return
}
err = builder.WriteField("n", strconv.Itoa(request.N))
if err != nil {
return
}
err = builder.WriteField("size", request.Size)
if err != nil {
return
}
err = builder.WriteField("response_format", request.ResponseFormat)
if err != nil {
return
}
err = builder.Close()
if err != nil {
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/images/variations"),
withBody(body), withContentType(builder.FormDataContentType()))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}