nsarrazin HF staff commited on
Commit
a01ed5a
1 Parent(s): eb956bc

Group tokens randomly (#964)

Browse files

* Group tokens together

* remove unused comment

* Send final buffer at the end if needed

src/routes/conversation/[id]/+server.ts CHANGED
@@ -409,6 +409,8 @@ export async function POST({ request, locals, params, getClientAddress }) {
409
 
410
  let hasError = false;
411
 
 
 
412
  try {
413
  const endpoint = await model.getEndpoint();
414
  for await (const output of await endpoint({
@@ -419,12 +421,18 @@ export async function POST({ request, locals, params, getClientAddress }) {
419
  })) {
420
  // if not generated_text is here it means the generation is not done
421
  if (!output.generated_text) {
422
- // else we get the next token
423
  if (!output.token.special) {
424
- update({
425
- type: "stream",
426
- token: output.token.text,
427
- });
 
 
 
 
 
 
 
428
  // abort check
429
  const date = abortedGenerations.get(convId.toString());
430
  if (date && date > promptedAt) {
@@ -466,6 +474,13 @@ export async function POST({ request, locals, params, getClientAddress }) {
466
  message: "No output was generated. Something went wrong.",
467
  });
468
  }
 
 
 
 
 
 
 
469
  }
470
 
471
  await collections.conversations.updateOne(
 
409
 
410
  let hasError = false;
411
 
412
+ let buffer = "";
413
+
414
  try {
415
  const endpoint = await model.getEndpoint();
416
  for await (const output of await endpoint({
 
421
  })) {
422
  // if not generated_text is here it means the generation is not done
423
  if (!output.generated_text) {
 
424
  if (!output.token.special) {
425
+ // 33% chance to send the stream update, with a max buffer size of 30 chars
426
+ buffer += output.token.text;
427
+
428
+ if (Math.random() < 0.33 || buffer.length > 30) {
429
+ update({
430
+ type: "stream",
431
+ token: buffer,
432
+ });
433
+ buffer = "";
434
+ }
435
+
436
  // abort check
437
  const date = abortedGenerations.get(convId.toString());
438
  if (date && date > promptedAt) {
 
474
  message: "No output was generated. Something went wrong.",
475
  });
476
  }
477
+
478
+ if (buffer) {
479
+ update({
480
+ type: "stream",
481
+ token: buffer,
482
+ });
483
+ }
484
  }
485
 
486
  await collections.conversations.updateOne(