-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.js
111 lines (91 loc) · 2.71 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
"use strict";
const config = require('./config');
const axios = require('axios');
const DEFAULT_ENGINE = "davinci";
class OpenAI {
constructor(api_key) {
this._api_key = api_key;
}
_send_request(url, method, opts = {}) {
let camelToUnderscore = (key) => {
let result = key.replace(/([A-Z])/g, " $1");
return result.split(' ').join('_').toLowerCase();
};
const data = {};
for (const key in opts) {
data[camelToUnderscore(key)] = opts[key];
}
return axios({
url,
headers: {
'Authorization': `Bearer ${this._api_key}`,
'Content-Type': 'application/json'
},
data: Object.keys(data).length ? data : '',
method,
});
}
_check_embeddings_engine_name(engine) {
const availableEngineNames = [
'text-similarity-ada-001',
'text-similarity-babbage-001',
'text-similarity-curie-001',
'text-similarity-davinci-001',
'text-search-ada-doc-001',
'text-search-ada-query-001',
'text-search-babbage-doc-001',
'text-search-babbage-query-001',
'text-search-curie-doc-001',
'text-search-curie-query-001',
'text-search-davinci-doc-001',
'text-search-davinci-query-001',
'code-search-ada-code-001',
'code-search-ada-text-001',
'code-search-babbage-code-001',
'code-search-babbage-text-001',
];
if (!availableEngineNames.includes(engine)) {
throw new Error(`Unknown engine name for embeddings. Available engine names are ${availableEngineNames}`);
}
}
complete(opts) {
const url = config.completionURL(opts.engine || DEFAULT_ENGINE);
delete opts.engine;
return this._send_request(url, 'post', opts);
}
encode() {
// This method is no longer supported in Node>=v14. See
return Promise.resolve(new Array(2047).fill(""));
}
search(opts) {
const url = config.searchURL(opts.engine || DEFAULT_ENGINE);
delete opts.engine;
return this._send_request(url, 'post', opts);
}
answers(opts) {
const url = config.answersUrl();
return this._send_request(url, 'post', opts);
}
classification(opts) {
const url = config.classificationsUrl();
return this._send_request(url, "post", opts);
}
engines() {
const url = config.enginesUrl();
return this._send_request(url, 'get');
}
engine(engine) {
const url = config.engineUrl(engine);
return this._send_request(url, 'get');
}
embeddings(opts) {
this._check_embeddings_engine_name(opts.engine);
const url = config.embeddingsUrl(opts.engine);
return this._send_request(url, 'post', opts);
}
chat(opts) {
const url = config.chatURL();
return this._send_request(url, 'post', opts);
}
}
module.exports = OpenAI;