mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2025-04-17 18:18:11 +00:00
Handle Anthropic streamable errors (#1113)
This commit is contained in:
parent
661563408a
commit
f9ac27e9a4
2 changed files with 41 additions and 9 deletions
|
@ -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 (
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue