Skip to content

Commit

Permalink
fix: enforce that the first message (excluding system messages) is a …
Browse files Browse the repository at this point in the history
…user message in the Deepseek API
  • Loading branch information
Kadxy committed Feb 28, 2025
1 parent 2167076 commit 9f0182b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/client/platforms/deepseek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ export class DeepSeekApi implements LLMApi {
}
}

// 检测并修复消息顺序,确保除system外的第一个消息是user
const filteredMessages: ChatOptions["messages"] = [];
let hasFoundFirstUser = false;

for (const msg of messages) {
if (msg.role === "system") {
// Keep all system messages
filteredMessages.push(msg);
} else if (msg.role === "user") {
// User message directly added
filteredMessages.push(msg);
hasFoundFirstUser = true;
} else if (hasFoundFirstUser) {
// After finding the first user message, all subsequent non-system messages are retained.
filteredMessages.push(msg);
}
// If hasFoundFirstUser is false and it is not a system message, it will be skipped.
}

const modelConfig = {
...useAppConfig.getState().modelConfig,
...useChatStore.getState().currentSession().mask.modelConfig,
Expand Down

0 comments on commit 9f0182b

Please sign in to comment.