-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimages.go
173 lines (147 loc) · 4.47 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
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
169
170
171
172
173
package openai
import (
"bytes"
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
)
type ImagesReuqest struct {
Prompt string `json:"prompt" form:"prompt"`
N int `json:"n,omitempty" form:"n,omitempty"`
Size string `json:"size,omitempty" form:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty" form:"response_format,omitempty"` // url or b64_json
User string `json:"user,omitempty" form:"user,omitempty"`
}
type ImagesResponse struct {
Created int64 `json:"created,omitempty"`
Data []ImagesResponseData `json:"data,omitempty"`
}
type ImagesResponseData struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
}
func (c *Client) CreateImage(ctx context.Context, request *ImagesReuqest) (response ImagesResponse, err error) {
req, err := c.requestFactory.Build(ctx, http.MethodPost, fullURL("images/generations"), request)
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
type ImagesEditReuqest struct {
Image string `json:"image"`
Mask string `json:"mask,omitempty"`
Prompt string `json:"prompt"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"` // url or b64_json
User string `json:"user,omitempty"`
}
func (c *Client) CreateImageEdits(ctx context.Context, request *ImagesEditReuqest) (response ImagesResponse, err error) {
buf := &bytes.Buffer{}
factory := c.formFactory(buf)
// load image
if err = factory.CreateFormFile("image", request.Image); err != nil {
errors.Wrap(err, "load origin image error")
return
}
// load mask
if request.Mask != "" {
if err = factory.CreateFormFile("mask", request.Mask); err != nil {
errors.Wrap(err, "load mask image error")
return
}
}
// write param
if err = factory.WriteField("prompt", request.Prompt); err != nil {
errors.Wrap(err, "write prompt error")
return
}
if request.N != 0 {
if err = factory.WriteField("n", fmt.Sprintf("%d", request.N)); err != nil {
errors.Wrap(err, "write n error")
return
}
}
if request.Size != "" {
if err = factory.WriteField("size", request.Size); err != nil {
errors.Wrap(err, "write size error")
return
}
}
if request.ResponseFormat != "" {
if err = factory.WriteField("response_format", request.ResponseFormat); err != nil {
errors.Wrap(err, "write response_format error")
return
}
}
if request.User != "" {
if err = factory.WriteField("user", request.User); err != nil {
errors.Wrap(err, "write user error")
return
}
}
if err = factory.Close(); err != nil {
errors.Wrap(err, "write close error")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL("images/edits"), buf)
if err != nil {
errors.Wrap(err, "NewRequestWithContext error")
return
}
err = c.sendRequest(req, &response)
return
}
type ImagesVariationReuqest struct {
Image string `json:"image"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"` // url or b64_json
User string `json:"user,omitempty"`
}
func (c *Client) CreateImageVariation(ctx context.Context, request *ImagesVariationReuqest) (response ImagesResponse, err error) {
buf := &bytes.Buffer{}
factory := c.formFactory(buf)
// load image
if err = factory.CreateFormFile("image", request.Image); err != nil {
errors.Wrap(err, "load origin image error")
return
}
if request.N != 0 {
if err = factory.WriteField("n", fmt.Sprintf("%d", request.N)); err != nil {
errors.Wrap(err, "write n error")
return
}
}
if request.Size != "" {
if err = factory.WriteField("size", request.Size); err != nil {
errors.Wrap(err, "write size error")
return
}
}
if request.ResponseFormat != "" {
if err = factory.WriteField("response_format", request.ResponseFormat); err != nil {
errors.Wrap(err, "write response_format error")
return
}
}
if request.User != "" {
if err = factory.WriteField("user", request.User); err != nil {
errors.Wrap(err, "write user error")
return
}
}
if err = factory.Close(); err != nil {
errors.Wrap(err, "write close error")
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL("images/edits"), buf)
if err != nil {
errors.Wrap(err, "NewRequestWithContext error")
return
}
err = c.sendRequest(req, &response)
return
}