Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DeepSeek API streaming reasoning content #6116

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 48 additions & 8 deletions app/client/platforms/deepseek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +22,10 @@ import {
SpeechOptions,
} from "../api";
import { getClientConfig } from "@/app/config/client";
import { getMessageTextContent } from "@/app/utils";
import {
getMessageTextContent,
getMessageTextContentWithoutThinking,
} from "@/app/utils";
import { RequestPayload } from "./openai";
import { fetch } from "@/app/utils/stream";

Expand Down Expand Up @@ -67,8 +70,13 @@ export class DeepSeekApi implements LLMApi {
async chat(options: ChatOptions) {
const messages: ChatOptions["messages"] = [];
for (const v of options.messages) {
const content = getMessageTextContent(v);
messages.push({ role: v.role, content });
if (v.role === "assistant") {
const content = getMessageTextContentWithoutThinking(v);
messages.push({ role: v.role, content });
} else {
const content = getMessageTextContent(v);
messages.push({ role: v.role, content });
}
}

const modelConfig = {
Expand Down Expand Up @@ -107,6 +115,8 @@ export class DeepSeekApi implements LLMApi {
headers: getHeaders(),
};

console.log(chatPayload);

// make a fetch request
const requestTimeoutId = setTimeout(
() => controller.abort(),
Expand All @@ -119,7 +129,7 @@ export class DeepSeekApi implements LLMApi {
.getAsTools(
useChatStore.getState().currentSession().mask?.plugin || [],
);
return stream(
return streamWithThink(
chatPath,
requestPayload,
getHeaders(),
Expand All @@ -128,12 +138,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;
Expand All @@ -155,7 +166,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
(
Expand Down
30 changes: 25 additions & 5 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function trimTopic(topic: string) {
return (
topic
// fix for gemini
.replace(/^["β€œβ€*]+|["β€œβ€*]+$/g, "")
.replace(/[οΌŒγ€‚οΌοΌŸβ€β€œ"、,.!?*]*$/, "")
.replace(/^["""*]+|[""*]+$/g, "")
.replace(/[οΌŒγ€‚οΌοΌŸ""""、,.!?*]*$/, "")
);
}

Expand Down Expand Up @@ -241,6 +241,28 @@ export function getMessageTextContent(message: RequestMessage) {
return "";
}

export function getMessageTextContentWithoutThinking(message: RequestMessage) {
let content = "";

if (typeof message.content === "string") {
content = message.content;
} else {
for (const c of message.content) {
if (c.type === "text") {
content = c.text ?? "";
break;
}
}
}

// Filter out thinking lines (starting with "> ")
return content
.split("\n")
.filter((line) => !line.startsWith("> ") && line.trim() !== "")
.join("\n")
.trim();
}

export function getMessageImages(message: RequestMessage): string[] {
if (typeof message.content === "string") {
return [];
Expand All @@ -256,9 +278,7 @@ export function getMessageImages(message: RequestMessage): string[] {

export function isVisionModel(model: string) {
const visionModels = useAccessStore.getState().visionModels;
const envVisionModels = visionModels
?.split(",")
.map((m) => m.trim());
const envVisionModels = visionModels?.split(",").map((m) => m.trim());
if (envVisionModels?.includes(model)) {
return true;
}
Expand Down
Loading