-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
319 lines (259 loc) · 9.32 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const { Packr, pack } = require('msgpackr');
const { Tokenizer } = require('./tokenizers/bindings/tokenizer');
const { wordPieceDecoder } = require('./tokenizers/bindings/decoders');
const { promisify } = require('util');
const fs = require('fs');
const word2vec = require('./node-word2vec-win32');
const { NGram, MultipleFile, GoodTuringSmoothing } = require('./nlptoolkit-ngram');
const packageJSON = require('./package.json');
const packr = new Packr({
structuredClone: true
});
const SIMI_CACHE = {};
class Kusari {
constructor() {
this.version = packageJSON.version;
this.corpus = [];
this.config = {
candidateMax: 5000,
ngrams: 3,
seed: 'banana',
top_k: 5,
infrequencySimiRatio: 0.5,
contextFrequencyLimit: 1
};
this.tokenFrequencies = {};
this.tokenCount = 0;
this.w2v = { words: 0, size: 0 };
this.wordVectors = null;
this.nGram = null;
this.tokenizer = null;
this.encodeTokens = null;
this.decodeTokens = null;
this.tokenRankings = {};
}
async init(tokenizerFile, w2vFile, tokenizedDataFile) {
this.rand = mulberry32(cyrb128(this.config.seed).reduce((a, v) => a + v));
this.tokenizer = await Tokenizer.fromFile(tokenizerFile);
this.tokenizer.setDecoder(wordPieceDecoder());
this.encodeTokens = promisify(this.tokenizer.encode.bind(this.tokenizer));
this.decodeTokens = promisify(this.tokenizer.decode.bind(this.tokenizer));
let simpleSmoothing = new GoodTuringSmoothing();
this.nGram = new NGram(new MultipleFile(tokenizedDataFile).readCorpus(), this.config.ngrams);
this.nGram.calculateNGramProbabilitiesSimple(simpleSmoothing);
let tokenSortable = Object.keys(this.tokenFrequencies).map(t => [t, this.tokenFrequencies[t]]);
tokenSortable = tokenSortable.sort((a, b) => b[1] - a[1]);
for (let i = 0; i < tokenSortable.length; i++) {
this.tokenRankings[tokenSortable[i][0]] = i;
}
await this.syncW2VFile(w2vFile);
return true;
}
syncW2VFile(w2vFile) {
return new Promise((resolve, reject) => {
word2vec.loadModel(w2vFile, (error, model) => {
if(error) return reject(error);
this.w2v = { words: this.tokenizer.getVocabSize(), size: Number(model.size) };
this.wordVectors = new Float32Array(this.w2v.words * this.w2v.size);
if(this.corpus.length < this.w2v.words) {
let padAmount = this.w2v.words - this.corpus.length;
for (let i = 0; i < padAmount; i++) { this.corpus.push([]); }
}
const vectors = model.getVectors();
for (let i = 0; i < vectors.length; i++) {
let wordID = Number(vectors[i].word);
this.wordVectors.set(vectors[i].values, wordID * this.w2v.size);
}
resolve(model);
});
});
}
async update(text, _settings={}) {
const settings = {
updateNGrams: false,
..._settings
};
let tokens = await this.encodeTokens(text);
let tokenIDs = tokens.getIds();
for (let i = 0; i < tokenIDs.length - 1; i++) {
if(!this.tokenFrequencies[tokenIDs[i]]) this.tokenFrequencies[tokenIDs[i]] = 0;
this.tokenFrequencies[tokenIDs[i]]++;
if(!this.corpus[tokenIDs[i]].includes(tokenIDs[i+1])) {
this.corpus[tokenIDs[i]].push(tokenIDs[i+1]);
}
}
this.tokenCount += tokenIDs.length;
if(settings.updateNGrams) this.nGram.addNGramSentence(tokenIDs);
return tokenIDs;
}
async generate({
from="",
maxlength=20
}) {
let currentToken;
let fromTokensIDs;
// If there is no from argument: Use the [CLS] starting token
// If there is a from argument: Use the 2nd to last token in from. (Last token is a stop token)
if(from === '') {
currentToken = this.tokenizer.tokenToId('[CLS]');
} else {
let fromTokens = await this.encodeTokens(from);
fromTokensIDs = fromTokens.getIds();
currentToken = fromTokensIDs[fromTokensIDs.length - 2];
}
// Track grams so that the model doesn't repeat itself
let oldGrams = [];
// Track used tokens to punish word reusage
let usedTokens = new Set();
let responseTokens = [currentToken];
let responseScore = 0;
// This loop repeats until a new route cannot be found or the currentToken is invalid.
while(currentToken) {
// Get all the possible routes from the currentToken
let routes = this.corpus[currentToken];
if(routes.length === 0) break;
// Setup the score format
let scores = routes.map(r => [r, 0]);
const contextTokenIDs = responseTokens.concat(fromTokensIDs);
const rewardUnit = contextTokenIDs.length;
for (let i = 0; i < scores.length; i++) {
let maxScore = rewardUnit * 3;
// Create an ngram
let gram = responseTokens.concat([scores[i][0]]).slice(-this.config.ngrams);
// Punishments
if(
(scores[i][0] === currentToken) || // Don't repeat the currentToken
(oldGrams.includes(gram.join('_'))) || // Don't use old ngrams
(usedTokens.has(scores[i][0])) // Don't use old tokens
) {
scores[i][1] -= rewardUnit;
}
// Calculate the probability that this ngram would occur in the dataset.
scores[i][1] += this.nGram.getProbability(gram[0], gram[1], gram[2]) * rewardUnit;
// Calculate the similarity to the prompt and response so far
let infrequencyScore = (1 - this.rankedFrequency(scores[i][0])) * this.config.infrequencySimiRatio;
for(let j = 0; j < contextTokenIDs.length; j++) {
// Skip the check if it passes the frequency threshold (probably a stop word).
if(this.rankedFrequency(contextTokenIDs[j]) > this.config.contextFrequencyLimit) continue;
let simi = this.similarity(scores[i][0], contextTokenIDs[j]) * (1 - this.config.infrequencySimiRatio);
scores[i][1] += simi + infrequencyScore;
}
// Score should sum to about 3 reward units by this point.
// Normalize the score
scores[i][1] = scores[i][1] / maxScore;
}
// Shuffle the array so that responses are not entirely deterministic.
shuffleArray(scores, this.rand);
scores = scores.sort((a, b) => b[1] - a[1]);
scores = scores.slice(0, this.config.top_k);
// Pick a top scoring token
let topScore = scores[Math.floor(this.rand() * scores.length)];
// Make it the new token if it's valid
if(!topScore) break;
currentToken = topScore[0];
if(!currentToken) break;
responseScore += topScore[1] || 0;
responseTokens.push(topScore[0]);
if(responseTokens.length >= maxlength) break;
// Record gram and token
let gram = responseTokens.slice(-this.config.ngrams);
oldGrams.push(gram.join('_'));
usedTokens.add(currentToken);
}
// Normalize response score
responseScore /= responseTokens.length;
let result = await this.decodeTokens(responseTokens, true);
return {
result: result,
tokens: responseTokens,
score: responseScore
};
}
routes(token, isPretokenized) {
let tokenized = this.toTokens(token, isPretokenized)[0];
let tokenID = this.addToDictionary(tokenized);
return this.getCorpusArr(tokenID).map(tid => this.dictionary[tid]);
}
similarity(token1, token2) {
if(token1 === token2) return 1;
if(SIMI_CACHE[`${token1}_${token2}`]) return SIMI_CACHE[`${token1}_${token2}`];
[token1, token2] = [token1, token2].map(Number);
if(!this.tokenizer.idToToken(token1) || !this.tokenizer.idToToken(token2)) return 0;
let sum = 0;
for (let i = 0; i < this.w2v.size; i++ ) {
sum += this.wordVectors[(token1 * this.w2v.size) + i] * this.wordVectors[(token2 * this.w2v.size) + i];
}
SIMI_CACHE[`${token1}_${token2}`] = sum;
return sum;
}
frequency(token) {
return this.tokenFrequencies[token] / this.tokenCount;
}
rankedFrequency(token) {
if(typeof this.tokenRankings[token] === 'undefined') return 0;
return 1 - (this.tokenRankings[token] / this.w2v.words);
}
save() {
const serializable = {
version: this.version,
corpus: this.corpus,
tokenFrequencies: this.tokenFrequencies,
tokenCount: this.tokenCount,
nGram: this.nGram
};
return packr.pack(serializable);
}
static load(buffer) {
if(typeof buffer === 'string') buffer = fs.readFileSync(buffer);
const data = packr.unpack(buffer);
const model = new Kusari();
model.version = data.version;
model.config = {
...model.config,
...data.config
};
model.corpus = data.corpus;
model.tokenFrequencies = data.tokenFrequencies;
model.tokenCount = data.tokenCount;
return model;
}
}
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
return [(h1^h2^h3^h4)>>>0, (h2^h1)>>>0, (h3^h1)>>>0, (h4^h1)>>>0];
}
function mulberry32(a) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
function shuffleArray(array, rand=Math.random) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(rand() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]
];
}
return array;
};
module.exports = Kusari;