-
Notifications
You must be signed in to change notification settings - Fork 7
/
images.go
61 lines (52 loc) · 1.65 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
package openai
import "context"
// Well-known responseformats
const (
ImageResponseFormatURL = "url"
ImageResponseFormatB64JSON = "b64_json"
ImageSize256 = "256x256"
ImageSize512 = "512x512"
ImageSize1024 = "1024x1024"
ImageSize1024x1792 = "1024x1792"
ImageSize1792x1024 = "1792x1024"
ImageQualityStandard = "standard"
ImageQualityHD = "hd"
ImageModelDallE2 = "dall-e-2"
ImageModelDallE3 = "dall-e-3"
)
// GenerateImage calls the images/generations API and returns the API response
// or any error.
func (c *Client) GenerateImage(ctx context.Context, p ImgReq) (*ImageRes, error) {
var res ImageRes
err := c.c.R().Post("images/generations").JSON(p).Do(ctx).JSON(&res)
if err != nil {
return nil, err
}
for i := range res.Data {
res.Data[i].c = c
}
return &res, nil
}
// ImgReq is the input type for image generation.
type ImgReq struct {
Prompt string `json:"prompt,omitempty"`
Model string `json:"model,omitempty"`
N *int `json:"n,omitempty"`
Size *string `json:"size,omitempty"`
ResponseFormat *string `json:"response_format,omitempty"`
User *string `json:"user,omitempty"`
Quality *string `json:"quality,omitempty"`
}
// ImageRes is returned by the Image generation.
type ImageRes struct {
Created int `json:"created"`
Data []Images `json:"data"`
}
// Images are returned as apart of the Image generation calls, and will contain
// either a URL to the image generated, or the bytes as `.Image` if the input
// specified `b64_json` as the ResponseFormat.
type Images struct {
c *Client
URL string `json:"url"`
Image []byte `json:"b64_json"`
}