Handle Anthropic streamable errors ()

This commit is contained in:
Timothy Carambat 2024-04-16 16:25:32 -07:00 committed by GitHub
parent 661563408a
commit f9ac27e9a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 9 deletions
server/utils
AiProviders/anthropic
chats

View file

@ -138,7 +138,7 @@ class AnthropicLLM {
async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
if (!this.isValidChatCompletionModel(this.model))
throw new Error(
`OpenAI chat: ${this.model} is not valid for chat completion!`
`Anthropic chat: ${this.model} is not valid for chat completion!`
);
const streamRequest = await this.anthropic.messages.stream({
@ -163,6 +163,28 @@ class AnthropicLLM {
const handleAbort = () => clientAbortedHandler(resolve, fullText);
response.on("close", handleAbort);
stream.on("error", (event) => {
const parseErrorMsg = (event) => {
const error = event?.error?.error;
if (!!error)
return `Anthropic Error:${error?.type || "unknown"} ${
error?.message || "unknown error."
}`;
return event.message;
};
writeResponseChunk(response, {
uuid,
sources: [],
type: "abort",
textResponse: null,
close: true,
error: parseErrorMsg(event),
});
response.removeListener("close", handleAbort);
resolve(fullText);
});
stream.on("streamEvent", (message) => {
const data = message;
if (

View file

@ -205,20 +205,30 @@ async function streamChatWithWorkspace(
});
}
const { chat } = await WorkspaceChats.new({
workspaceId: workspace.id,
prompt: message,
response: { text: completeText, sources, type: chatMode },
threadId: thread?.id || null,
user,
});
if (completeText?.length > 0) {
const { chat } = await WorkspaceChats.new({
workspaceId: workspace.id,
prompt: message,
response: { text: completeText, sources, type: chatMode },
threadId: thread?.id || null,
user,
});
writeResponseChunk(response, {
uuid,
type: "finalizeResponseStream",
close: true,
error: false,
chatId: chat.id,
});
return;
}
writeResponseChunk(response, {
uuid,
type: "finalizeResponseStream",
close: true,
error: false,
chatId: chat.id,
});
return;
}