Skip to content

Commit e52b3cd

Browse files
committed
LanguageModel: Make callback/event return the instance rather than the value
1 parent e211111 commit e52b3cd

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

examples/LanguageModel/sketch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ function generateText() {
4242
lm.generate(prompt, options, gotText);
4343
}
4444

45-
function gotText(text, lm) {
46-
console.log('Model returned "' + text + '"');
45+
function gotText(result) {
46+
console.log('Model returned "' + result.text + '"');
4747
// lm.words contains the output broken up in words
48-
words = lm.words;
48+
words = result.words;
4949
curWord = 0;
5050
}

examples/LanguageModelEvents/sketch.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ function generateText() {
3636
}
3737

3838

39-
function onToken(token, lm) {
40-
//console.log('Token', token);
39+
function onToken(lm) {
40+
console.log('Token', lm.tokens.slice(-1));
4141
}
4242

43-
function onWord(word, lm) {
44-
//console.log('Word "' + word + '"');
43+
function onWord(lm) {
44+
console.log('Word "' + lm.words.slice(-1) + '"');
4545
push();
4646
translate(width/2, height/2);
4747
rotate(radians(angle));
@@ -55,10 +55,10 @@ function onWord(word, lm) {
5555
fill(255);
5656
textAlign(CENTER);
5757
textSize(10);
58-
text(word, 0, 0);
58+
text(lm.words.slice(-1), 0, 0);
5959
pop();
6060
}
6161

62-
function onFinsh(text, lm) {
63-
console.log('Generation finished');
62+
function onFinsh(lm) {
63+
console.log('Model returned "' + lm.text + '"');
6464
}

src/LanguageModel/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ class LanguageModel extends EventEmitter {
115115
}
116116

117117
// on-token event
118-
this.emit('token', this.tokens[this.tokens.length-1], this);
118+
this.emit('token', this);
119119

120120
// redo word tokenization
121121
const wordDelimiters = ' .,:;"“?!\n';
122122
const re = new RegExp('(?=[' + wordDelimiters + '])|(?<=[' + wordDelimiters + '])', 'g');
123-
const prevNumWords = this.words.length;
124-
this.words = this.text.split(re);
123+
const newWords = this.text.split(re);
125124
// ignore the last word if we can't be certain it's complete
126125
if (!wordDelimiters.includes(this.text.slice(-1)) && !this.finished) {
127-
this.words.pop();
126+
newWords.pop();
128127
}
129-
// on-word event
130-
for (let i=prevNumWords; i < this.words.length; i++) {
131-
this.emit('word', this.words[i], this);
128+
// on-word events
129+
for (let i=this.words.length; i < newWords.length; i++) {
130+
this.words[i] = newWords[i];
131+
this.emit('word', this);
132132
}
133133

134134
// on-finished promise/event/callback
@@ -137,9 +137,9 @@ class LanguageModel extends EventEmitter {
137137
if (this.promiseResolve) {
138138
this.promiseResolve(this.text);
139139
}
140-
this.emit('finish', this.text, this);
140+
this.emit('finish', this);
141141
if (this.callback) {
142-
this.callback(this.text, this);
142+
this.callback(this);
143143
}
144144
}
145145
}, 'viifi');

0 commit comments

Comments
 (0)