Skip to content

Commit

Permalink
TextStreamer - support custom callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed May 31, 2024
1 parent b57c3b7 commit 7236245
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/generation/streamers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ export class TextStreamer extends BaseStreamer {
*/
constructor(tokenizer, {
skip_prompt = false,
callback_function = null,
...decode_kwargs
} = {}) {
super();
this.tokenizer = tokenizer;
this.skip_prompt = skip_prompt;
this.callback_function = callback_function ?? stdout_write;
this.decode_kwargs = decode_kwargs;

// variables used in the streaming process
Expand Down Expand Up @@ -115,10 +117,10 @@ export class TextStreamer extends BaseStreamer {
*/
on_finalized_text(text, stream_end) {
if (text.length > 0) {
stdout_write(text);
this.callback_function(text);
}
if (stream_end) {
stdout_write('\n');
this.callback_function('\n');
}
}
}

0 comments on commit 7236245

Please sign in to comment.