feat: Support DeepSeek API streaming with thinking mode

This commit is contained in:
Kadxy
2025-01-31 00:07:52 +08:00
parent 553b8c9f28
commit c449737127
2 changed files with 301 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ import {
ChatMessageTool,
usePluginStore,
} from "@/app/store";
import { stream } from "@/app/utils/chat";
import { streamWithThink } from "@/app/utils/chat";
import {
ChatOptions,
getHeaders,
@@ -107,6 +107,8 @@ export class DeepSeekApi implements LLMApi {
headers: getHeaders(),
};
console.log(chatPayload);
// make a fetch request
const requestTimeoutId = setTimeout(
() => controller.abort(),
@@ -119,7 +121,7 @@ export class DeepSeekApi implements LLMApi {
.getAsTools(
useChatStore.getState().currentSession().mask?.plugin || [],
);
return stream(
return streamWithThink(
chatPath,
requestPayload,
getHeaders(),
@@ -128,12 +130,13 @@ export class DeepSeekApi implements LLMApi {
controller,
// parseSSE
(text: string, runTools: ChatMessageTool[]) => {
// console.log("parseSSE", text, runTools);
console.log("parseSSE", text, runTools);
const json = JSON.parse(text);
const choices = json.choices as Array<{
delta: {
content: string;
content: string | null;
tool_calls: ChatMessageTool[];
reasoning_content: string | null;
};
}>;
const tool_calls = choices[0]?.delta?.tool_calls;
@@ -155,7 +158,36 @@ export class DeepSeekApi implements LLMApi {
runTools[index]["function"]["arguments"] += args;
}
}
return choices[0]?.delta?.content;
const reasoning = choices[0]?.delta?.reasoning_content;
const content = choices[0]?.delta?.content;
// Skip if both content and reasoning_content are empty or null
if (
(!reasoning || reasoning.trim().length === 0) &&
(!content || content.trim().length === 0)
) {
return {
isThinking: false,
content: "",
};
}
if (reasoning && reasoning.trim().length > 0) {
return {
isThinking: true,
content: reasoning,
};
} else if (content && content.trim().length > 0) {
return {
isThinking: false,
content: content,
};
}
return {
isThinking: false,
content: "",
};
},
// processToolMessage, include tool_calls message and tool call results
(