-
Notifications
You must be signed in to change notification settings - Fork 2
/
images.go
146 lines (117 loc) · 3.78 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
package gochatgptsdk
import (
"fmt"
"net/http"
)
func (c *chatgpt) ImagesGenerations(b ModelImages) (*ModelImagesResponse[DataURL], *ErrorResponse) {
// POST https://api.openai.com/v1/images/generations
endpointImagesGeneratios := fmt.Sprintf("%s/images/generations", ChatGPTAPIV1)
result := ModelImagesResponse[DataURL]{}
errorResponse := ErrorResponse{}
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errorResponse).
Post(endpointImagesGeneratios)
if err != nil {
return nil, &errorResponse
}
// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errorResponse
}
return &result, nil
}
func (c *chatgpt) ImagesGenerationsB64JSON(b ModelImages) (*ModelImagesResponse[DataB64JSON], *ErrorResponse) {
// POST https/api.openai.com/v1/images/generations
endpointImagesGeneratios := fmt.Sprintf("%s/images/generations", ChatGPTAPIV1)
result := ModelImagesResponse[DataB64JSON]{}
errorResponse := ErrorResponse{}
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errorResponse).
Post(endpointImagesGeneratios)
if err != nil {
return nil, &errorResponse
}
// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errorResponse
}
return &result, nil
}
func (c *chatgpt) ImagesVariations(b ModelImagesVariations) (*ModelImagesResponse[DataURL], *ErrorResponse) {
// POST https/api.openai.com/v1/images/variations
endpointImagesVariations := fmt.Sprintf("%s/images/variations", ChatGPTAPIV1)
result := ModelImagesResponse[DataURL]{}
errResponse := ErrorResponse{}
// set default images variations
d := defaultImagesVariations(b, ResponseFormatURL)
resp, err := DoRequest(c.OpenAIKey).
SetFile("image", *&d.Image).
SetFormData(map[string]string{
"n": d.N,
"size": d.Size,
"response_format": d.ResponseFormat,
"user": d.User,
}).
SetResult(&result).
SetError(&errResponse).
Post(endpointImagesVariations)
if err != nil {
return nil, &errResponse
}
// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}
return &result, nil
}
func (c *chatgpt) ImagesVariationsB64JSON(b ModelImagesVariations) (*ModelImagesResponse[DataB64JSON], *ErrorResponse) {
// POST https/api.openai.com/v1/images/variations
endpointImagesVariations := fmt.Sprintf("%s/images/variations", ChatGPTAPIV1)
result := ModelImagesResponse[DataB64JSON]{}
errResponse := ErrorResponse{}
// set default images variations
d := defaultImagesVariations(b, ResponseFormatB64JSON)
resp, err := DoRequest(c.OpenAIKey).
SetFile("image", *&d.Image).
SetFormData(map[string]string{
"n": d.N,
"size": d.Size,
"response_format": d.ResponseFormat,
"user": d.User,
}).
SetResult(&result).
SetError(&errResponse).
Post(endpointImagesVariations)
if err != nil {
return nil, &errResponse
}
// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}
return &result, nil
}
func defaultImagesVariations(b ModelImagesVariations, f string) *ModelImagesVariations {
// to prevent nil pointer, we need to check if n empty, fill with default value
if b.N == "" {
b.N = "1"
}
// to prevent nil pointer, we need to check if size empty, fill with default value
if b.Size == "" {
b.Size = Size1024
}
// to prevent nil pointer, we need to check if response_format empty, fill with default value
if b.ResponseFormat == "" {
b.ResponseFormat = f
}
// to prevent nil pointer, we need to check if user empty, fill with default value
if b.User == "" {
b.User = "user"
}
*&b.Image = b.Image
return &b
}