Skip to content

Commit

Permalink
Merge pull request #82 from PolarisYan/thinking
Browse files Browse the repository at this point in the history
feat(chat): Change the output field format of the thinking content of…
  • Loading branch information
Vinlic authored Feb 10, 2025
2 parents 3c9b8fb + 6140537 commit 1250ea0
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ async function receiveStream(model: string, stream: any, refConvId?: string): Pr
choices: [
{
index: 0,
message: { role: "assistant", content: "" },
message: { role: "assistant", content: "", reasoning_content: "" },
finish_reason: "stop",
},
],
Expand All @@ -581,20 +581,25 @@ async function receiveStream(model: string, stream: any, refConvId?: string): Pr
refContent += searchResults.map(item => `${item.title} - ${item.url}`).join('\n');
return;
}
if (result.choices[0].delta.type === "thinking") {
if (isFoldModel && result.choices[0].delta.type === "thinking") {
if (!thinking && isThinkingModel && !isSilentModel) {
thinking = true;
data.choices[0].message.content += isFoldModel ? "<details><summary>思考过程</summary><pre>" : "[思考开始]\n";
}
if (isSilentModel)
return;
}
else if (thinking && isThinkingModel && !isSilentModel) {
else if (isFoldModel && thinking && isThinkingModel && !isSilentModel) {
thinking = false;
data.choices[0].message.content += isFoldModel ? "</pre></details>" : "\n\n[思考结束]\n";
}
if (result.choices[0].delta.content)
data.choices[0].message.content += result.choices[0].delta.content;
if (result.choices[0].delta.content) {
if(result.choices[0].delta.type === "thinking" && !isFoldModel){
data.choices[0].message.reasoning_content += result.choices[0].delta.content;
}else {
data.choices[0].message.content += result.choices[0].delta.content;
}
}
if (result.choices && result.choices[0] && result.choices[0].finish_reason === "stop") {
data.choices[0].message.content = data.choices[0].message.content.replace(/^\n+/, '').replace(/\[citation:\d+\]/g, '') + (refContent ? `\n\n搜索结果来自:\n${refContent}` : '');
resolve(data);
Expand Down Expand Up @@ -640,7 +645,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
choices: [
{
index: 0,
delta: { role: "assistant", content: "" },
delta: { role: "assistant", content: "" , reasoning_content: "" },
finish_reason: null,
},
],
Expand Down Expand Up @@ -676,7 +681,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
}
return;
}
if (result.choices[0].delta.type === "thinking") {
if (isFoldModel && result.choices[0].delta.type === "thinking") {
if (!thinking && isThinkingModel && !isSilentModel) {
thinking = true;
transStream.write(`data: ${JSON.stringify({
Expand All @@ -696,7 +701,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
if (isSilentModel)
return;
}
else if (thinking && isThinkingModel && !isSilentModel) {
else if (isFoldModel && thinking && isThinkingModel && !isSilentModel) {
thinking = false;
transStream.write(`data: ${JSON.stringify({
id: `${refConvId}@${result.message_id}`,
Expand All @@ -716,19 +721,25 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
if (!result.choices[0].delta.content)
return;

const deltaContent = result.choices[0].delta.content.replace(/\[citation:\d+\]/g, '');
const delta = result.choices[0].delta.type === "thinking" && !isFoldModel
? { role: "assistant", reasoning_content: deltaContent }
: { role: "assistant", content: deltaContent };

transStream.write(`data: ${JSON.stringify({
id: `${refConvId}@${result.message_id}`,
model: result.model,
object: "chat.completion.chunk",
choices: [
{
index: 0,
delta: { role: "assistant", content: result.choices[0].delta.content.replace(/\[citation:\d+\]/g, '') },
delta,
finish_reason: null,
},
],
created,
})}\n\n`);

if (result.choices && result.choices[0] && result.choices[0].finish_reason === "stop") {
transStream.write(`data: ${JSON.stringify({
id: `${refConvId}@${result.message_id}`,
Expand Down

0 comments on commit 1250ea0

Please sign in to comment.