θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion server/utils/AiProviders/anthropic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 (
Expand Down
26 changes: 18 additions & 8 deletions server/utils/chats/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down