-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
54 lines (48 loc) · 1.55 KB
/
helpers.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
import chalk from "chalk";
const MOTORHEAD_URL = "http://localhost:8080"
export const fetchMessages = () => fetch(`${MOTORHEAD_URL}/sessions/ozzy/memory`, {
method: "GET",
headers: { 'Content-Type': 'application/json' },
}).then(res => res.json())
export const postMessages = (messages) => fetch(`${MOTORHEAD_URL}/sessions/ozzy/memory`, {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages })
}).then(res => res.json())
export const retrieval = async (query) => {
if (!query) return Promise.resolve([])
return fetch(`${MOTORHEAD_URL}/sessions/ozzy/retrieval`, {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: query })
}).then(res => res.json())
}
export function streamResponse(completion) {
return new Promise((resolve) => {
let result = "";
completion.data.on("data", (data) => {
const lines = data
?.toString()
?.split("\n")
.filter((line) => line.trim() !== "");
for (const line of lines) {
const message = line.replace(/^data: /, "");
if (message == "[DONE]") {
process.stdout.write(chalk.green('\n'));
resolve(result);
} else {
let token;
try {
token = JSON.parse(message)?.choices?.[0]?.delta.content;
} catch (err) {
// console.log("ERROR", err);
}
if (token) {
result += token;
process.stdout.write(chalk.green(token));
}
}
}
});
});
}