-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
635 lines (576 loc) · 21.8 KB
/
index.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import {fetchEventSource} from '@microsoft/fetch-event-source';
export default class Api2d {
// 设置key和apiBaseUrl
constructor(key = null, apiBaseUrl = null, timeout = 60000, version = '2023-07-01-preview', deployments = {
'gpt-3.5-turbo':'gpt-35-turbo',
'gpt-3.5-turbo-0301':'gpt-35-turbo-0301',
'gpt-3.5-turbo-0613':'gpt-35-turbo-0613',
'gpt-3.5-16k':'gpt-35-16k',
'gpt-3.5-16k-0613':'gpt-35-16k-0613',
'gpt-4':'gpt-4',
'text-embedding-ada-002':'text-embedding-ada-002',
}) {
this.key = key || '';
this.apiBaseUrl = apiBaseUrl || (key && key.startsWith('fk') ? 'https://oa.api2d.net' : 'https://api.openai.com');
this.deployments = deployments;
this.version = version;
this._updateHeaders()
this.timeout = timeout;
this.controller = new AbortController();
this.apiVersion = 1;
}
// 根据 key 和 apiBaseUrl,更新请求 headers
_updateHeaders() {
// 如果 apiBaseUrl 包含 openai.azure.com
if( this.apiBaseUrl.includes('openai.azure.com') )
{
this.by = 'azure';
this.authHeader = {'api-key': this.key};
this.refHeader = {};
}else
{
// openai 默认配置
this.by = this.key.startsWith('fk') ? 'api2d' : 'openai';
this.authHeader = {"Authorization": "Bearer " + this.key};
if( this.key.startsWith('sk-or-') )
{
this.refHeader = {"HTTP-Referer":"https://ai0c.com"};
}else
{
this.refHeader = {};
}
}
}
// set key
setKey(key) {
this.key = key || '';
this._updateHeaders()
}
// set apiBaseUrl
setApiBaseUrl(apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
this._updateHeaders()
}
// set apiVersion
setApiVersion(apiVersion) {
this.apiVersion = apiVersion;
}
setTimeout(timeout) {
this.timeout = parseInt(timeout) || 60 * 1000;
}
abort() {
this.controller.abort();
this.controller = new AbortController();
}
api2dOnly( openaiOk = false )
{
if( openaiOk )
{
if( this.by != 'api2d'&& this.by != 'openai' )
{
throw new Error('Only support api2d');
}
}else
{
if( this.by != 'api2d' )
{
throw new Error('Only support api2d');
}
}
}
buildUrlByModel(model)
{
// console.log( "model", model );
if( this.by == 'azure' )
{
const deployment = this.deployments[model]||"GPT35";
if( String(model).toLowerCase().startsWith('text-embedding') )
{
return this.apiBaseUrl + '/openai/deployments/'+deployment+'/embeddings?api-version='+this.version;
}else
{
// if( model.toLowerCase().startsWith('gpt') )
// {
return this.apiBaseUrl + '/openai/deployments/'+deployment+'/chat/completions?api-version='+this.version;
// }
}
}
else
{
const trimmedUrl = this.apiBaseUrl.replace(/\/*$/, '');
if( String(model).toLowerCase().startsWith('text-embedding') || String(model).toLowerCase().endsWith('bge-m3') )
{
if (trimmedUrl.match(/\/v\d+$/))
return `${trimmedUrl}/embeddings`;
return `${trimmedUrl}/v${this.apiVersion}/embeddings`;
}else
{
if (trimmedUrl.match(/\/v\d+$/))
return `${trimmedUrl}/chat/completions`;
return `${trimmedUrl}/v${this.apiVersion}/chat/completions`;
}
}
}
// Completion
async completion(options) {
// 拼接headers
const headers = {
"Content-Type": "application/json",
...this.authHeader,...this.refHeader,
};
const {onMessage, onReasoning, onEnd, model, noCache, ...otherOptions} = options;
// 拼接目标URL
const url = this.buildUrlByModel(model || 'gpt-3.5-turbo');
const modelObj = this.by == 'azure' ? {} : {model: model || 'gpt-3.5-turbo'};
const { moderation, moderation_stop, ...optionsWithoutModeration } = otherOptions;
const restOptions = this.by == 'api2d' ? otherOptions : optionsWithoutModeration;
if (noCache) headers['x-api2d-no-cache'] = 1;
// 如果是流式返回,且有回调函数
if (restOptions.stream && onMessage) {
// 返回一个 Promise
return new Promise(async (resolve, reject) => {
try {
let chars = "";
// console.log("in stream");
// 使用 fetchEventSource 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
// throw new Error( "Timeout "+ this.timeout );
reject(new Error(`[408]:Timeout by ${this.timeout} ms`));
}, this.timeout);
const response = await fetchEventSource(url, {
signal: this.controller.signal,
openWhenHidden: true,
method: "POST",
headers: {...headers, "Accept": "text/event-stream"},
body: JSON.stringify({...restOptions, ...modelObj}),
async onopen(response) {
if (response.status != 200) {
const info = await response.text();
throw new Error(`[${response.status}]:${response.statusText} ${info}`);
}
},
onmessage: e => {
if (timeout_handle) {
clearTimeout(timeout_handle);
}
if (e.data == '[DONE]') {
// console.log( 'DONE' );
if (onEnd) onEnd(chars);
resolve(chars);
} else {
// 忽略所有非JSON的数据
if( !isJSON(e.data) ) return;
const event = JSON.parse(e.data);
if( event.error )
{
throw new Error(event.error.message);
}else
{
if( event.choices && event.choices.length > 0 )
{
// azure 不返回 [DONE],而是返回 finish_reason
const char = event.choices[0].delta.content;
const reasoning_char = event.choices[0].delta.reasoning_content || '';
if (char)
{
chars += char;
if (onMessage) onMessage(chars,char);
}else if(reasoning_char)
{
if(onReasoning) onReasoning(reasoning_char);
}
if( event.action && event.action === 'clean' )
{
chars = "";
}
if( event.choices[0].finish_reason )
{
// end
if (onEnd) onEnd(chars);
resolve(chars);
}
}
}
}
},
onerror: error => {
console.log(error);
let error_string = String(error);
if (error_string && error_string.match(/\[(\d+)\]/)) {
const matchs = error_string.match(/\[(\d+)\]/);
error_string = `[${matchs[1]}]:${error_string}`;
}
throw new Error(error_string);
}
});
// const ret = await response.json();
} catch (error) {
console.log(error);
reject(error);
}
});
} else {
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({...restOptions, ...modelObj})
});
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
}
async completionWithRetry ( data, retry = 2 )
{
return new Promise( (resolve, reject) => {
try {
this.completion(data).then( resolve ).catch( (error) => {
console.log( "error in completion", error );
if( retry > 0 && String(error).includes("retry") )
{
setTimeout( () => {
this.completionWithRetry( data, retry-1 ).then( resolve ).catch( reject );
}, 1000 );
}
else
{
console.log( "error in completion", error );
reject(error);
}
});
} catch (error) {
console.log( "error in completion", error );
}
});
}
async embeddings(options) {
// 拼接headers
const headers = {
"Content-Type": "application/json",
...this.authHeader,...this.refHeader,
};
const {model, ...restOptions} = options;
const modelObj = this.by == 'azure' ? {} : {model: model || 'text-embedding-ada-002'};
// 拼接目标URL
const url = this.buildUrlByModel(model || 'text-embedding-ada-002');
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({...restOptions, ...modelObj})
});
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async billing() {
this.api2dOnly(true);
const url = this.apiBaseUrl + "/dashboard/billing/credit_grants";
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
};
const response = await fetch(url, {
signal: this.controller.signal,
method: "GET",
headers: headers
});
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async vectorSave(options) {
this.api2dOnly();
// text, embedding, uuid = "", meta = ""
const {text, embedding, uuid, meta} = options;
// 拼接目标URL
const url = this.apiBaseUrl + "/vector";
// 拼接headers
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
};
// 使用 fetch 发送请求
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({
text: text,
uuid: uuid || "",
embedding: embedding,
meta: meta || ""
})
});
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async vectorSearch(options) {
this.api2dOnly();
const {searchable_id, embedding, topk} = options;
// 拼接目标URL
const url = this.apiBaseUrl + "/vector/search";
// 拼接headers
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
};
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({
searchable_id,
embedding,
topk: topk || 1
})
});
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async vectorDelete(options) {
this.api2dOnly();
const {uuid} = options;
// 拼接目标URL
const url = this.apiBaseUrl + "/vector/delete";
// 拼接headers
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
};
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({
uuid
})
});
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async vectorDeleteAll() {
this.api2dOnly();
// 拼接目标URL
const url = this.apiBaseUrl + "/vector/delete-all";
// 拼接headers
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
};
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response = await fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({})
});
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
async textToSpeech(options) {
this.api2dOnly();
const {text, voiceName, responseType, output, speed} = options;
// 拼接目标URL
const url = this.apiBaseUrl + "/azure/tts";
// 拼接headers
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key,
};
// 使用 fetch 发送请求
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const response_promise = fetch(url, {
signal: this.controller.signal,
method: "POST",
headers: headers,
body: JSON.stringify({
text,
voiceName,
speed
})
})
.then((response) => {
const reader = response.body.getReader();
return new ReadableStream({
start(controller) {
return pump();
function pump() {
return reader.read().then(({done, value}) => {
// When no more data needs to be consumed, close the stream
if (done) {
controller.close();
return;
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
return pump();
});
}
},
});
})
// Create a new response out of the stream
.then((stream) => new Response(stream))
// Create an object URL for the response
.then((response) => response.blob());
const saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
if (responseType === 'file') {
const ret = response_promise.then((blob) => saveData(blob, output));
clearTimeout(timeout_handle);
return ret;
} else if (responseType === 'blob') {
const ret = response_promise;
clearTimeout(timeout_handle);
return ret;
} else if (responseType === 'blob-url') {
const ret = response_promise.then((blob) => window.URL.createObjectURL(blob));
clearTimeout(timeout_handle);
return ret;
} else {
throw new Error('responseType must be file, blob or blob-url');
}
}
async speechToText(options) {
this.api2dOnly();
const {file, language, moderation, moderation_stop} = options;
// 拼接目标URL
const url = this.apiBaseUrl + "/azure/stt";
// 拼接headers
const headers = {
"Content-Type": "multipart/form-data",
"Authorization": "Bearer " + this.key,
};
const formData = new FormData();
formData.set('language', language);
formData.set('moderation', moderation);
formData.set('moderation_stop', moderation_stop);
formData.set('file', file);
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
// node-fetch 处理 formdata 有问题,用 axios
const response = await axios.post(url, formData, {
headers,
signal: this.controller.signal
});
clearTimeout(timeout_handle);
return response.data;
}
async imageGenerate(options) {
let { model, prompt, n, size, response_format} = options;
if( !n ) n = 1;
if( !size ) size = '1024x1024';
if( !response_format ) response_format = 'url';
if( !['dall-e-2','dall-e-3'].includes(model) ) model = 'dall-e-3';
const ret = await this.request({
path: 'v1/images/generations',
method: 'POST',
data: {
prompt,
n,
size,
model,
response_format
}
}, false);
return ret;
}
async request( options, api2dOnly = ture )
{
if(api2dOnly) this.api2dOnly();
const {url, method, headers, body, path, data} = options;
const timeout_handle = setTimeout(() => {
this.controller.abort();
this.controller = new AbortController();
}, this.timeout);
const final_url = path ? this.apiBaseUrl +'/'+ path : url;
const final_data = data ? JSON.stringify(data) : body;
let option = {
signal: this.controller.signal,
method: method || 'GET',
headers: {...( headers ? headers : {} ), ...{
"Content-Type": "application/json",
"Authorization": "Bearer " + this.key
}}
}
if( !['GET','HEAD'].includes( method.toUpperCase() ) ) option.body = final_data;
const response = await fetch( final_url, option );
// console.log( final_url, option, response );
const ret = await response.json();
clearTimeout(timeout_handle);
return ret;
}
}
// 一个测试能否被JSON parse的函数
function isJSON(str) {
if (typeof str == 'string') {
try {
const obj = JSON.parse(str);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
console.log('error:' + str + '!!!' + e);
return false;
}
}
console.log('It is not a string!')
return false;
}