-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
61 lines (43 loc) · 1.15 KB
/
utils.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
const Tokenizer = require('gpt3-tokenizer').default;
const tokenizer = new Tokenizer({ type: 'gpt3' });
class KeyValueStore {
constructor() {
this.store = {};
}
get(key) {
return this.store[key];
}
set(key, value) {
this.store[key] = value;
}
}
const storage = new KeyValueStore();
exports.writeToStorage = (uuid, value, merge = true) => {
const startState = JSON.parse(storage.get(uuid) || "{}")
if (merge) {
storage.set(uuid, JSON.stringify({
...startState,
...value
}))
} else {
storage.set(uuid, JSON.stringify(value))
}
}
exports.getStorage = (uuid) => {
return JSON.parse(storage.get(uuid) || "{}")
}
exports.validTrim = (str) => {
const string = str.trim()
const lastSpaceIndex = string.lastIndexOf(" ")
if (lastSpaceIndex === -1) {
return string.trim()
}
return string.substring(0, lastSpaceIndex).trim()
}
exports.getTokenizedHistoryLength = (history) => {
let length = 0;
for (const message of history) {
length += tokenizer.encode(message.content).bpe.length;
}
return length;
}