Merge pull request #1780 from cheahjs/fix/harden-streaming

fix: harden openai streaming parsing
This commit is contained in:
Timothy Jaeryang Baek 2024-04-26 11:06:20 -07:00 committed by GitHub
commit 81fb53e757
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -36,10 +36,14 @@ async function* openAIStreamToIterator(
// OpenRouter sends heartbeats like ": OPENROUTER PROCESSING"
continue
} 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);
}
}
}
}