forked from Godzab/go-gpt3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
362 lines (302 loc) · 10.6 KB
/
models.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gpt3
import (
"fmt"
"os"
)
const (
DAVINCI = "davinci"
CURIE = "curie"
BABBAGE = "babbage"
BABBAGE_INSTRUCT_BETA = "text-babbage-001"
ADA = "ada"
ADA_INSTRUCT_BETA = "text-ada-001"
CURIE_INSTRUCT_BETA = "text-curie-001"
DAVINCI_INSTRUCT_BETA = "text-davinci-001"
// CURSING_FILTER_V6 Content filters moderate output and input to the api to
//avoid negative content generation
CURSING_FILTER_V6 = "cursing-filter-v6"
CONTENT_FILTER_DEV = "content-filter-dev"
CONTENT_FILTER_ALPHA_C4 = "content-filter-alpha-c4"
// DAVINCI_CODEX Codex engines for code generation.
//Davinci Codex is more capable, particularly for translating natural language to code
DAVINCI_CODEX = "davinci-codex"
// CUSHMAN_CODEX Cushman Codex is almost as capable, but slightly faster.
//This speed advantage may be preferable for real-time applications.
CUSHMAN_CODEX = "cushman-codex"
)
//
const (
getRequest = "GET"
postRequest = "POST"
)
const (
ANSWERS = "answers"
SEARCH = "search"
CLASSIFICATIONS = "classifications"
)
type RequestConfig struct {
endpointVersion, baseUrl, engine string
}
type Request interface {
attachResponse() Response
getRequestMeta(config RequestConfig) (string, string)
}
type Response interface {
GetBody() Response
}
type Document struct {
Document int `json:"document"`
Object string `json:"object,omitempty"`
Score float64 `json:"score,omitempty"`
Text string `json:"text"`
}
type File struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
}
type Choices struct {
Text string `json:"text"`
Index int `json:"index"`
Logprobs LogprobResult `json:"logprobs"`
FinishReason string `json:"finish_reason"`
}
type LogprobResult struct {
Tokens []string `json:"tokens"`
TokenLogprobs []float32 `json:"token_logprobs"`
TopLogprobs []map[string]float32 `json:"top_logprobs"`
TextOffset []int `json:"text_offset"`
}
type SearchData struct {
Document
Object string `json:"object"`
Score float64 `json:"score"`
}
type ClassificationExamples struct {
Document
Label string `json:"label"`
}
type Engine struct {
ID string `json:"id"`
Object string `json:"object"`
Owner string `json:"owner"`
Ready bool `json:"ready"`
}
// Files models
type FilesRequest struct{}
type FilesResponse struct {
Data []File `json:"data"`
Object string `json:"object"`
}
func (r *FilesRequest) attachResponse() Response {
resp := &FilesResponse{}
return resp
}
func (r *FilesResponse) GetBody() Response {
return r
}
func (r *FilesRequest) getRequestMeta(config RequestConfig) (string, string) {
return getRequest, fmt.Sprintf("%s/%s/files", config.baseUrl, config.endpointVersion)
}
// File models
type FileRequest struct {
File os.File `json:"file"`
Purpose string `json:"purpose"`
}
type FileResponse struct {
File
}
func (r *FileRequest) attachResponse() Response {
resp := &FileResponse{}
return resp
}
func (r *FileResponse) GetBody() Response {
return r
}
func (r *FileRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/files", config.baseUrl, config.endpointVersion)
}
// CompletionRequest Completion model structures
type CompletionRequest struct {
Prompt string `json:"prompt"`
MaxTokens int `json:"max_tokens"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stream bool `json:"stream"`
Logprobs int `json:"logprobs,omitempty"`
Stop []string `json:"stop,omitempty"`
Echo bool `json:"echo,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
BestOf float32 `json:"best_of,omitempty"`
LogitBias map[string]int8 `json:"logit_bias,omitempty"`
}
type CompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `json:"model"`
Choices []Choices `json:"choices"`
}
func (r *CompletionRequest) attachResponse() Response {
resp := &CompletionResponse{}
return resp
}
func (r *CompletionRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/engines/%s/completions", config.baseUrl, config.endpointVersion, config.engine)
}
func (r *CompletionResponse) GetBody() Response {
return r
}
//ContentFilterRequest Content filter model structures
type ContentFilterRequest struct {
Prompt string `json:"prompt"`
MaxTokens int `json:"max_tokens"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Logprobs int `json:"logprobs,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
}
func (r *ContentFilterRequest) attachResponse() Response {
resp := &CompletionResponse{}
return resp
}
func (r *ContentFilterRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/engines/content-filter-alpha-c4/completions", config.baseUrl, config.endpointVersion)
}
// SearchRequest Search Model structures
type SearchRequest struct {
target string
Documents []string `json:"documents,omitempty"`
Query string `json:"query"`
File string `json:"file,omitempty"`
ReturnMetadata bool `json:"return_metadata"`
MaxRerank int32 `json:"max_rerank,omitempty"`
}
type SearchResponse struct {
Data []SearchData `json:"data"`
Object string `json:"object"`
}
func (r *SearchRequest) attachResponse() Response {
resp := &SearchResponse{}
return resp
}
func (r *SearchRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/engines/%s/search", config.baseUrl, config.endpointVersion, config.engine)
}
func (r *SearchResponse) GetBody() Response {
return r
}
type EnginesRequest struct{}
type EnginesResponse struct {
Data []interface{} `json:"data"`
Object string `json:"object"`
}
func (e EnginesResponse) GetBody() Response {
return e
}
func (r *EnginesRequest) attachResponse() Response {
resp := &EnginesResponse{}
return resp
}
func (r *EnginesRequest) getRequestMeta(config RequestConfig) (string, string) {
return getRequest, fmt.Sprintf("%s/%s/engines", config.baseUrl, config.endpointVersion)
}
// ClassificationRequest Classification Model structures
type ClassificationRequest struct {
Examples [][]string `json:"examples"`
Labels []string `json:"labels"`
Query string `json:"query"`
File string `json:"file"`
SearchModel string `json:"search_model"`
Model string `json:"model"`
Temperature float32 `json:"temperature"`
Logprobs interface{} `json:"logprobs,omitempty"`
MaxExamples int32 `json:"max_examples"`
LogitBias map[string]int8 `json:"logit_bias,omitempty"`
ReturnPrompt bool `json:"return_prompt,omitempty"`
ReturnMetadata bool `json:"return_metadata,omitempty"`
Expand []string `json:"expand,omitempty"`
}
type ClassificationResponse struct {
Completion string `json:"completion"`
Label string `json:"label"`
Model string `json:"model"`
Object string `json:"object"`
SearchModel string `json:"search_model"`
SelectedExamples []ClassificationExamples `json:"selected_examples"`
}
func (r *ClassificationRequest) attachResponse() Response {
resp := &ClassificationResponse{}
return resp
}
func (r *ClassificationRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/classifications", config.baseUrl, config.endpointVersion)
}
func (r *ClassificationResponse) GetBody() Response {
return r
}
// AnswerRequest Answer Model structures
type AnswerRequest struct {
Documents []string `json:"documents"`
Question string `json:"question"`
SearchModel string `json:"search_model"`
Model string `json:"model"`
ExamplesContext string `json:"examples_context"`
Examples [][]string `json:"examples"`
MaxTokens int `json:"max_tokens"`
Stop []string `json:"stop"`
File string `json:"file,omitempty"`
MaxRerank int32 `json:"max_rerank"`
Temperature float32 `json:"temperature"`
Logprobs interface{} `json:"logprobs,omitempty"`
N int `json:"n,omitempty"`
LogitBias map[string]int8 `json:"logit_bias,omitempty"`
ReturnPrompt bool `json:"return_prompt"`
ReturnMetadata bool `json:"return_metadata"`
Expand []string `json:"expand,omitempty"`
}
type AnswerResponse struct {
Answers []string `json:"answers"`
Completion CompletionResponse `json:"completion"`
Model string `json:"model"`
Object string `json:"object"`
SearchModel string `json:"search_model"`
SelectedDocuments []Document `json:"selected_documents"`
}
func (r *AnswerRequest) attachResponse() Response {
resp := &AnswerResponse{}
return resp
}
func (r *AnswerRequest) getRequestMeta(config RequestConfig) (string, string) {
return postRequest, fmt.Sprintf("%s/%s/answers", config.baseUrl, config.endpointVersion)
}
func (r *AnswerResponse) GetBody() Response {
return r
}
//GptErrorResponse Error handling for client calls
type GptErrorResponse struct {
Code interface{} `json:"code"`
Message string `json:"message"`
Param string `json:"param"`
Type string `json:"type"`
}
type ErrorBag struct {
Err GptErrorResponse `json:"error"`
}
func (e ErrorBag) Error() string {
return fmt.Sprintf("[GPT ERROR] %v: %s %s %v",
e.Err.Code, e.Err.Type, e.Err.Param, e.Err.Message)
}
func (e ErrorBag) Timeout() bool {
return true
}
func (e ErrorBag) Temporary() bool {
return true
}