better error messages

main
Cogent Apps 2023-03-20 14:14:03 +00:00
parent 746981ed9e
commit da0d4844f1
2 changed files with 14 additions and 9 deletions

View File

@ -150,13 +150,13 @@ export class ChatManager extends EventEmitter {
let lastChunkReceivedAt = Date.now();
const onError = () => {
const onError = (error?: string) => {
if (reply.done) {
return;
}
clearInterval(timer);
cancel();
reply.content += "\n\nI'm sorry, I'm having trouble connecting to OpenAI. Please make sure you've entered your OpenAI API key correctly and try again.";
reply.content += `\n\nI'm sorry, I'm having trouble connecting to OpenAI (${error || 'no response from the API'}). Please make sure you've entered your OpenAI API key correctly and try again.`;
reply.content = reply.content.trim();
reply.done = true;
this.activeReplies.delete(reply.id);
@ -169,21 +169,20 @@ export class ChatManager extends EventEmitter {
let timer = setInterval(() => {
const sinceLastChunk = Date.now() - lastChunkReceivedAt;
if (sinceLastChunk > 10000 && !reply.done) {
onError();
if (sinceLastChunk > 30000 && !reply.done) {
onError('no response from OpenAI in the last 30 seconds');
}
}, 2000);
emitter.on('error', () => {
emitter.on('error', (e: any) => {
if (!reply.content && !reply.done) {
lastChunkReceivedAt = Date.now();
onError();
onError(e);
}
});
emitter.on('data', (data: string) => {
if (reply.done) {
cancel();
return;
}
lastChunkReceivedAt = Date.now();

View File

@ -126,7 +126,11 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
eventSource.addEventListener('error', (event: any) => {
if (!contents) {
emitter.emit('error');
let error = event.data;
try {
error = JSON.parse(error).error.message;
} catch (e) {}
emitter.emit('error', error);
}
});
@ -164,4 +168,6 @@ async function selectMessagesToSendSafely(messages: OpenAIMessage[], maxTokens:
preserveSystemPrompt: true,
});
return compressor.process();
}
}
setTimeout(() => selectMessagesToSendSafely([], 2048), 2000);