Forward error messages from Cohere streams

This commit is contained in:
Cohee 2024-04-04 21:20:30 +03:00
parent 813476d72a
commit ee3718ad7a
1 changed files with 7 additions and 1 deletions

View File

@ -36,7 +36,13 @@ async function parseCohereStream(jsonStream, request, response) {
} catch (e) {
break;
}
if (json.event_type === 'text-generation') {
if (json.message) {
const message = json.message || 'Unknown error';
const chunk = { error: { message: message } };
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
partialData = '';
break;
} else if (json.event_type === 'text-generation') {
const text = json.text || '';
const chunk = { choices: [{ text }] };
response.write(`data: ${JSON.stringify(chunk)}\n\n`);