mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
More reliable Cohere stream parsing
This commit is contained in:
@ -48,32 +48,22 @@ function postProcessPrompt(messages, type, charName, userName) {
|
|||||||
*/
|
*/
|
||||||
async function parseCohereStream(jsonStream, request, response) {
|
async function parseCohereStream(jsonStream, request, response) {
|
||||||
try {
|
try {
|
||||||
let partialData = '';
|
|
||||||
jsonStream.body.on('data', (data) => {
|
jsonStream.body.on('data', (data) => {
|
||||||
const chunk = data.toString();
|
|
||||||
partialData += chunk;
|
|
||||||
while (true) {
|
|
||||||
let json;
|
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(partialData);
|
const json = JSON.parse(data.toString());
|
||||||
} catch (e) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (json.message) {
|
if (json.message) {
|
||||||
const message = json.message || 'Unknown error';
|
const message = json.message || 'Unknown error';
|
||||||
const chunk = { error: { message: message } };
|
const chunk = { error: { message: message } };
|
||||||
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||||
partialData = '';
|
|
||||||
break;
|
|
||||||
} else if (json.event_type === 'text-generation') {
|
} else if (json.event_type === 'text-generation') {
|
||||||
const text = json.text || '';
|
const text = json.text || '';
|
||||||
const chunk = { choices: [{ text }] };
|
const chunk = { choices: [{ text }] };
|
||||||
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||||
partialData = '';
|
|
||||||
} else {
|
} else {
|
||||||
partialData = '';
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// ignore
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -437,7 +427,7 @@ async function sendAI21Request(request, response) {
|
|||||||
|
|
||||||
console.log('AI21 request:', body);
|
console.log('AI21 request:', body);
|
||||||
|
|
||||||
try{
|
try {
|
||||||
const generateResponse = await fetch(API_AI21 + '/chat/completions', options);
|
const generateResponse = await fetch(API_AI21 + '/chat/completions', options);
|
||||||
if (request.body.stream) {
|
if (request.body.stream) {
|
||||||
forwardFetchResponse(generateResponse, response);
|
forwardFetchResponse(generateResponse, response);
|
||||||
|
Reference in New Issue
Block a user