-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhelpers.js
74 lines (67 loc) · 2 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import chalk from "chalk";
import * as dotenv from "dotenv"
dotenv.config()
const MOTORHEAD_URL = "https://api.getmetal.io/v1/motorhead"
const API_KEY = process.env.METAL_API_KEY
const CLIENT_ID = process.env.METAL_CLIENT_ID
export const fetchMessages = () => fetch(`${MOTORHEAD_URL}/sessions/ozzy/memory`, {
method: "GET",
headers: {
'Content-Type': 'application/json',
'x-metal-api-key': API_KEY,
'x-metal-client-id': CLIENT_ID,
},
}).then(res => res.json())
.then(res => res.data)
export const postMessages = (messages) => fetch(`${MOTORHEAD_URL}/sessions/ozzy/memory`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'x-metal-api-key': API_KEY,
'x-metal-client-id': CLIENT_ID,
},
body: JSON.stringify({ messages })
}).then(res => res.json())
.then(res => res.data)
export const retrieval = async (query) => {
if (!query) return Promise.resolve([])
return fetch(`${MOTORHEAD_URL}/sessions/ozzy/retrieval`, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'x-metal-api-key': API_KEY,
'x-metal-client-id': CLIENT_ID,
},
body: JSON.stringify({ text: query })
}).then(res => res.json())
.then(res => res.data)
}
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));
}
}
}
});
});
}