-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenai.js
143 lines (122 loc) · 4.71 KB
/
openai.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
async function ask(message, history, assistant, callback, controller) {
return new Promise(async (resolve, reject) => {
try {
const messages = [{ role: "system", content: assistant.systemPrompt }]
for (const message of history) {
messages.push({ role: message.role === "user" ? "user" : "assistant", content: message.content });
}
messages.push({ role: "user", content: message });
const params = {
api_key: assistant.params.api_key,
api_url: assistant.params.api_url,
model: assistant.modelFile,
temperature: assistant.params.temperature,
max_tokens: assistant.params.max_tokens,
top_p: assistant.params.top_p,
frequency_penalty: assistant.params.frequency_penalty,
presence_penalty: assistant.params.presence_penalty,
stop: assistant.stopTemplate,
}
const request = openai(messages, params, controller);
let content = "";
for await (const chunk of request) {
content += chunk.data.choices[0].delta.content;
callback && callback(chunk.data.choices[0].delta.content);
}
resolve(content);
} catch (e) {
reject(e);
}
});
}
async function* openai(messages, params = {}, controller) {
controller = controller ?? new AbortController()
const api_url = params.api_url;
if (!api_url) {
throw new Error("api_url is required");
}
const completionParams = { ...params, messages, stream: true };
delete completionParams.api_key
delete completionParams.api_url
const response = await fetch(`${api_url}`, {
method: 'POST',
body: JSON.stringify(completionParams),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${params.api_key}`
},
signal: controller.signal,
})
if(response.status !== 200) {
throw new Error(`Failed to connect openai: ${response.statusText}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let content = "";
let leftover = ""; // Buffer for partially read lines
try {
let cont = true;
while (cont) {
const result = await reader.read();
if (result.done) {
break;
}
// Add any leftover data to the current chunk of data
const text = leftover + decoder.decode(result.value);
// Check if the last character is a line break
const endsWithLineBreak = text.endsWith('\n');
// Split the text into lines
let lines = text.split('\n');
// If the text doesn't end with a line break, then the last line is incomplete
// Store it in leftover to be added to the next chunk of data
if (!endsWithLineBreak) {
leftover = lines.pop();
} else {
leftover = ""; // Reset leftover if we have a line break at the end
}
// Parse all sse events and add them to result
const regex = /^(\S+):\s(.*)$/gm;
for (const line of lines) {
const match = regex.exec(line);
if (match) {
result[match[1]] = match[2];
// since we know this is llama.cpp, let's just decode the json in data
if (result.data && result.data !== "[DONE]") {
result.data = JSON.parse(result.data);
if (result.data.choices[0].finish_reason) {
if (result.data.generation_settings) {
generation_settings = result.data.generation_settings;
}
cont = false;
break;
}
content += result.data.choices[0].delta.content;
// yield
yield result;
}
}
}
}
} catch (e) {
if (e.name !== 'AbortError') {
console.error("llama error: ", e);
}
throw e;
}
finally {
controller.abort();
}
return content;
}
async function getModels(api_key) {
const response = await fetch(`https://api.openai.com/v1/models`
, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${api_key}`
},
})
return await response.json();
}
module.exports = { ask, getModels }