Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jan 31, 2025
2 parents c0023e0 + 3ef5993 commit f125d7f
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with Claude, GPT

</div>

## 🥳 Cheer for DeepSeek, China's AI star!
> Purpose-Built UI for DeepSeek Reasoner Model
<img src="https://github.com/user-attachments/assets/f3952210-3af1-4dc0-9b81-40eaa4847d9a"/>



## 🫣 NextChat Support MCP !
> Before build, please set env ENABLE_MCP=true
Expand Down
54 changes: 47 additions & 7 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 @@ -132,8 +142,9 @@ export class DeepSeekApi implements LLMApi {
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
26 changes: 23 additions & 3 deletions app/utils.ts
Original file line number Diff line number Diff line change
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

0 comments on commit f125d7f

Please sign in to comment.