Skip to content

Commit

Permalink
fix: streamline JSON parsing and response handling in stream handler
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Nov 29, 2023
1 parent 1972d1a commit c3ada36
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main.js
Expand Up @@ -212,9 +212,11 @@ function handleResponse(query, targetText, textFromResponse) {
if (textFromResponse !== '[DONE]') {
try {
const dataObj = JSON.parse(textFromResponse);
// https://github.com/openai/openai-node/blob/master/src/resources/chat/completions.ts#L190
const { choices } = dataObj;
if (choices && choices[0] && choices[0].delta.content) {
targetText += choices[0].delta.content;
const delta = choices[0]?.delta?.content;
if (delta) {
targetText += delta;
query.onStream({
result: {
from: query.detectFrom,
Expand Down

0 comments on commit c3ada36

Please sign in to comment.