Skip to content

Commit

Permalink
handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks committed Jan 22, 2023
1 parent 4a7de86 commit b7ed528
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ const handler = async (req: NextRequest): Promise<Response> => {
controller.close();
return;
}
const json = JSON.parse(data);
const text = json.choices[0].text;
if (counter < 2 && (text.match(/\n/) || []).length) {
// this is a prefix character (i.e., "\n\n"), do nothing
return;
try {
const json = JSON.parse(data);
const text = json.choices[0].text;
if (counter < 2 && (text.match(/\n/) || []).length) {
// this is a prefix character (i.e., "\n\n"), do nothing
return;
}
const queue = encoder.encode(text);
controller.enqueue(queue);
counter++;
} catch (e) {
// maybe parse error
controller.error(e);
}
const queue = encoder.encode(text);
controller.enqueue(queue);
counter++;
}
}

Expand Down

0 comments on commit b7ed528

Please sign in to comment.