-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoobapi-ws.mjs
69 lines (61 loc) · 1.78 KB
/
oobapi-ws.mjs
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
//default options from https://github.com/oobabooga/text-generation-webui/blob/main/api-example-stream.py
const default_options = {
prompt: "In order to make homemade bread, follow these steps:\n1)",
max_new_tokens: 250,
do_sample: true,
temperature: 1.3,
top_p: 0.1,
typical_p: 1,
epsilon_cutoff: 0, // In units of 1e-4
eta_cutoff: 0, // In units of 1e-4
repetition_penalty: 1.18,
top_k: 40,
min_length: 0,
no_repeat_ngram_size: 0,
num_beams: 1,
penalty_alpha: 0,
length_penalty: 1,
early_stopping: false,
mirostat_mode: 0,
mirostat_tau: 5,
mirostat_eta: 0.1,
seed: -1,
add_bos_token: true,
truncation_length: 2048,
ban_eos_token: false,
skip_special_tokens: true,
stopping_strings: []
}
const api = WebSocket => ({host='localhost',port=5005}={}, parameters=default_options) => new Promise(res => {
const URI = `ws://${host}:${port}/api/v1/stream`
const socket = new WebSocket(URI)
//browser compatibility shim
if (!socket.on) socket.on = socket.addEventListener
let api = {parameters}
api.generate = prompt => {
//check for string prompt and wrap in an object
if (typeof prompt === 'string') prompt = {prompt}
socket.send(JSON.stringify(Object.assign({},api.parameters,prompt)))
}
socket.on('message', (data) => {
//browser compatibility shim
if (data.data) data = data.data
const parsed = JSON.parse(data)
switch (parsed.event) {
case 'text_stream':
api.ontoken?.(parsed.text)
api.debugstream?.(parsed)
break;
case 'stream_end':
api.onend?.(parsed)
}
})
socket.on('error', (error) => {
console.error(`WebSocket error: `,error)
})
socket.on('close', () => {
console.error(`WebSocket closed`)
})
socket.on('open', () => res(api))
})
export default api