fix: catch any errors parsing openai sse events

This commit is contained in:
Jun Siang Cheah 2024-04-26 18:38:25 +01:00
parent e9ba8d74d2
commit 510afab37c

View File

@ -32,10 +32,14 @@ async function* openAIStreamToIterator(
if (line === 'data: [DONE]') {
yield { done: true, value: '' };
} else {
const data = JSON.parse(line.replace(/^data: /, ''));
console.log(data);
try {
const data = JSON.parse(line.replace(/^data: /, ''));
console.log(data);
yield { done: false, value: data.choices[0].delta.content ?? '' };
yield { done: false, value: data.choices[0].delta.content ?? '' };
} catch (e) {
console.error('Error extracting delta from SSE event:', e);
}
}
}
}