-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
301 lines (251 loc) · 8.09 KB
/
client.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package zhipu
import (
"context"
"errors"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/golang-jwt/jwt/v5"
)
const (
envAPIKey = "ZHIPUAI_API_KEY"
envBaseURL = "ZHIPUAI_BASE_URL"
envDebug = "ZHIPUAI_DEBUG"
defaultBaseURL = "https://open.bigmodel.cn/api/paas/v4"
)
var (
// ErrAPIKeyMissing is the error when the api key is missing
ErrAPIKeyMissing = errors.New("zhipu: api key is missing")
// ErrAPIKeyMalformed is the error when the api key is malformed
ErrAPIKeyMalformed = errors.New("zhipu: api key is malformed")
)
type clientOptions struct {
baseURL string
apiKey string
client *http.Client
resty *resty.Client
debug *bool
}
// ClientOption is a function that configures the client
type ClientOption func(opts *clientOptions)
// WithAPIKey set the api key of the client
func WithAPIKey(apiKey string) ClientOption {
return func(opts *clientOptions) {
opts.apiKey = apiKey
}
}
// WithBaseURL set the base url of the client
func WithBaseURL(baseURL string) ClientOption {
return func(opts *clientOptions) {
opts.baseURL = baseURL
}
}
// WithRestyClient set the resty client of the client
func WithRestyClient(client *resty.Client) ClientOption {
return func(opts *clientOptions) {
opts.resty = client
}
}
// WithHTTPClient set the http client of the client
func WithHTTPClient(client *http.Client) ClientOption {
return func(opts *clientOptions) {
opts.client = client
}
}
// WithDebug set the debug mode of the client
func WithDebug(debug bool) ClientOption {
return func(opts *clientOptions) {
opts.debug = new(bool)
*opts.debug = debug
}
}
// Client is the client for zhipu ai platform
type Client struct {
client *resty.Client
debug bool
keyID string
keySecret []byte
}
func (c *Client) createJWT() string {
timestamp := time.Now().UnixMilli()
exp := timestamp + time.Hour.Milliseconds()*24*7
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"api_key": c.keyID,
"timestamp": timestamp,
"exp": exp,
})
t.Header = map[string]interface{}{
"alg": "HS256",
"sign_type": "SIGN",
}
token, err := t.SignedString(c.keySecret)
if err != nil {
panic(err)
}
return token
}
// request creates a new resty request with the jwt token and context
func (c *Client) request(ctx context.Context) *resty.Request {
return c.client.R().SetContext(ctx).SetHeader("Authorization", c.createJWT())
}
// NewClient creates a new client
// It will read the api key from the environment variable ZHIPUAI_API_KEY
// It will read the base url from the environment variable ZHIPUAI_BASE_URL
func NewClient(optFns ...ClientOption) (client *Client, err error) {
var opts clientOptions
for _, optFn := range optFns {
optFn(&opts)
}
// base url
if opts.baseURL == "" {
opts.baseURL = strings.TrimSpace(os.Getenv(envBaseURL))
}
if opts.baseURL == "" {
opts.baseURL = defaultBaseURL
}
// api key
if opts.apiKey == "" {
opts.apiKey = strings.TrimSpace(os.Getenv(envAPIKey))
}
if opts.apiKey == "" {
err = ErrAPIKeyMissing
return
}
// debug
if opts.debug == nil {
if debugStr := strings.TrimSpace(os.Getenv(envDebug)); debugStr != "" {
if debug, err1 := strconv.ParseBool(debugStr); err1 == nil {
opts.debug = &debug
}
}
}
keyComponents := strings.SplitN(opts.apiKey, ".", 2)
if len(keyComponents) != 2 {
err = ErrAPIKeyMalformed
return
}
client = &Client{
keyID: keyComponents[0],
keySecret: []byte(keyComponents[1]),
}
if opts.resty != nil {
client.client = opts.resty
} else if opts.client != nil {
client.client = resty.NewWithClient(opts.client)
} else {
client.client = resty.New()
}
client.client = client.client.SetBaseURL(opts.baseURL)
if opts.debug != nil {
client.client.SetDebug(*opts.debug)
client.debug = *opts.debug
}
return
}
// BatchCreate creates a new BatchCreateService.
func (c *Client) BatchCreate() *BatchCreateService {
return NewBatchCreateService(c)
}
// BatchGet creates a new BatchGetService.
func (c *Client) BatchGet(batchID string) *BatchGetService {
return NewBatchGetService(c).SetBatchID(batchID)
}
// BatchCancel creates a new BatchCancelService.
func (c *Client) BatchCancel(batchID string) *BatchCancelService {
return NewBatchCancelService(c).SetBatchID(batchID)
}
// BatchList creates a new BatchListService.
func (c *Client) BatchList() *BatchListService {
return NewBatchListService(c)
}
// ChatCompletion creates a new ChatCompletionService.
func (c *Client) ChatCompletion(model string) *ChatCompletionService {
return NewChatCompletionService(c).SetModel(model)
}
// Embedding embeds a list of text into a vector space.
func (c *Client) Embedding(model string) *EmbeddingService {
return NewEmbeddingService(c).SetModel(model)
}
// FileCreate creates a new FileCreateService.
func (c *Client) FileCreate(purpose string) *FileCreateService {
return NewFileCreateService(c).SetPurpose(purpose)
}
// FileEditService creates a new FileEditService.
func (c *Client) FileEdit(documentID string) *FileEditService {
return NewFileEditService(c).SetDocumentID(documentID)
}
// FileList creates a new FileListService.
func (c *Client) FileList(purpose string) *FileListService {
return NewFileListService(c).SetPurpose(purpose)
}
// FileDeleteService creates a new FileDeleteService.
func (c *Client) FileDelete(documentID string) *FileDeleteService {
return NewFileDeleteService(c).SetDocumentID(documentID)
}
// FileGetService creates a new FileGetService.
func (c *Client) FileGet(documentID string) *FileGetService {
return NewFileGetService(c).SetDocumentID(documentID)
}
// FileDownload creates a new FileDownloadService.
func (c *Client) FileDownload(fileID string) *FileDownloadService {
return NewFileDownloadService(c).SetFileID(fileID)
}
// FineTuneCreate creates a new fine tune create service
func (c *Client) FineTuneCreate(model string) *FineTuneCreateService {
return NewFineTuneCreateService(c).SetModel(model)
}
// FineTuneEventList creates a new fine tune event list service
func (c *Client) FineTuneEventList(jobID string) *FineTuneEventListService {
return NewFineTuneEventListService(c).SetJobID(jobID)
}
// FineTuneGet creates a new fine tune get service
func (c *Client) FineTuneGet(jobID string) *FineTuneGetService {
return NewFineTuneGetService(c).SetJobID(jobID)
}
// FineTuneList creates a new fine tune list service
func (c *Client) FineTuneList() *FineTuneListService {
return NewFineTuneListService(c)
}
// FineTuneDelete creates a new fine tune delete service
func (c *Client) FineTuneDelete(jobID string) *FineTuneDeleteService {
return NewFineTuneDeleteService(c).SetJobID(jobID)
}
// FineTuneCancel creates a new fine tune cancel service
func (c *Client) FineTuneCancel(jobID string) *FineTuneCancelService {
return NewFineTuneCancelService(c).SetJobID(jobID)
}
// ImageGeneration creates a new image generation service
func (c *Client) ImageGeneration(model string) *ImageGenerationService {
return NewImageGenerationService(c).SetModel(model)
}
// KnowledgeCreate creates a new knowledge create service
func (c *Client) KnowledgeCreate() *KnowledgeCreateService {
return NewKnowledgeCreateService(c)
}
// KnowledgeEdit creates a new knowledge edit service
func (c *Client) KnowledgeEdit(knowledgeID string) *KnowledgeEditService {
return NewKnowledgeEditService(c).SetKnowledgeID(knowledgeID)
}
// KnowledgeList list all the knowledge
func (c *Client) KnowledgeList() *KnowledgeListService {
return NewKnowledgeListService(c)
}
// KnowledgeDelete creates a new knowledge delete service
func (c *Client) KnowledgeDelete(knowledgeID string) *KnowledgeDeleteService {
return NewKnowledgeDeleteService(c).SetKnowledgeID(knowledgeID)
}
// KnowledgeGet creates a new knowledge get service
func (c *Client) KnowledgeCapacity() *KnowledgeCapacityService {
return NewKnowledgeCapacityService(c)
}
// VideoGeneration creates a new video generation service
func (c *Client) VideoGeneration(model string) *VideoGenerationService {
return NewVideoGenerationService(c).SetModel(model)
}
// AsyncResult creates a new async result get service
func (c *Client) AsyncResult(id string) *AsyncResultService {
return NewAsyncResultService(c).SetID(id)
}