-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_generation.go
100 lines (79 loc) · 1.91 KB
/
image_generation.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
package zhipu
import (
"context"
"github.com/go-resty/resty/v2"
)
// ImageGenerationService creates a new image generation
type ImageGenerationService struct {
client *Client
model string
prompt string
userID string
}
var (
_ BatchSupport = &ImageGenerationService{}
)
// ImageGenerationResponse is the response of the ImageGenerationService
type ImageGenerationResponse struct {
Created int64 `json:"created"`
Data []URLItem `json:"data"`
}
// NewImageGenerationService creates a new ImageGenerationService
func NewImageGenerationService(client *Client) *ImageGenerationService {
return &ImageGenerationService{
client: client,
}
}
func (s *ImageGenerationService) BatchMethod() string {
return "POST"
}
func (s *ImageGenerationService) BatchURL() string {
return BatchEndpointV4ImagesGenerations
}
func (s *ImageGenerationService) BatchBody() any {
return s.buildBody()
}
// SetModel sets the model parameter
func (s *ImageGenerationService) SetModel(model string) *ImageGenerationService {
s.model = model
return s
}
// SetPrompt sets the prompt parameter
func (s *ImageGenerationService) SetPrompt(prompt string) *ImageGenerationService {
s.prompt = prompt
return s
}
// SetUserID sets the userID parameter
func (s *ImageGenerationService) SetUserID(userID string) *ImageGenerationService {
s.userID = userID
return s
}
func (s *ImageGenerationService) buildBody() M {
body := M{
"model": s.model,
"prompt": s.prompt,
}
if s.userID != "" {
body["user_id"] = s.userID
}
return body
}
func (s *ImageGenerationService) Do(ctx context.Context) (res ImageGenerationResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
body := s.buildBody()
if resp, err = s.client.request(ctx).
SetBody(body).
SetResult(&res).
SetError(&apiError).
Post("images/generations"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}