Skip to content
Snippets Groups Projects
Unverified Commit f9ac27e9 authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

Handle Anthropic streamable errors (#1113)

parent 66156340
No related branches found
No related tags found
No related merge requests found
...@@ -138,7 +138,7 @@ class AnthropicLLM { ...@@ -138,7 +138,7 @@ class AnthropicLLM {
async streamGetChatCompletion(messages = null, { temperature = 0.7 }) { async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
if (!this.isValidChatCompletionModel(this.model)) if (!this.isValidChatCompletionModel(this.model))
throw new Error( 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({ const streamRequest = await this.anthropic.messages.stream({
...@@ -163,6 +163,28 @@ class AnthropicLLM { ...@@ -163,6 +163,28 @@ class AnthropicLLM {
const handleAbort = () => clientAbortedHandler(resolve, fullText); const handleAbort = () => clientAbortedHandler(resolve, fullText);
response.on("close", handleAbort); 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) => { stream.on("streamEvent", (message) => {
const data = message; const data = message;
if ( if (
......
...@@ -205,20 +205,30 @@ async function streamChatWithWorkspace( ...@@ -205,20 +205,30 @@ async function streamChatWithWorkspace(
}); });
} }
const { chat } = await WorkspaceChats.new({ if (completeText?.length > 0) {
workspaceId: workspace.id, const { chat } = await WorkspaceChats.new({
prompt: message, workspaceId: workspace.id,
response: { text: completeText, sources, type: chatMode }, prompt: message,
threadId: thread?.id || null, response: { text: completeText, sources, type: chatMode },
user, threadId: thread?.id || null,
}); user,
});
writeResponseChunk(response, {
uuid,
type: "finalizeResponseStream",
close: true,
error: false,
chatId: chat.id,
});
return;
}
writeResponseChunk(response, { writeResponseChunk(response, {
uuid, uuid,
type: "finalizeResponseStream", type: "finalizeResponseStream",
close: true, close: true,
error: false, error: false,
chatId: chat.id,
}); });
return; return;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment