Skip to content

Commit 1250ea0

Browse files
authored
Merge pull request #82 from PolarisYan/thinking
feat(chat): Change the output field format of the thinking content of…
2 parents 3c9b8fb + 6140537 commit 1250ea0

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/api/controllers/chat.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ async function receiveStream(model: string, stream: any, refConvId?: string): Pr
558558
choices: [
559559
{
560560
index: 0,
561-
message: { role: "assistant", content: "" },
561+
message: { role: "assistant", content: "", reasoning_content: "" },
562562
finish_reason: "stop",
563563
},
564564
],
@@ -581,20 +581,25 @@ async function receiveStream(model: string, stream: any, refConvId?: string): Pr
581581
refContent += searchResults.map(item => `${item.title} - ${item.url}`).join('\n');
582582
return;
583583
}
584-
if (result.choices[0].delta.type === "thinking") {
584+
if (isFoldModel && result.choices[0].delta.type === "thinking") {
585585
if (!thinking && isThinkingModel && !isSilentModel) {
586586
thinking = true;
587587
data.choices[0].message.content += isFoldModel ? "<details><summary>思考过程</summary><pre>" : "[思考开始]\n";
588588
}
589589
if (isSilentModel)
590590
return;
591591
}
592-
else if (thinking && isThinkingModel && !isSilentModel) {
592+
else if (isFoldModel && thinking && isThinkingModel && !isSilentModel) {
593593
thinking = false;
594594
data.choices[0].message.content += isFoldModel ? "</pre></details>" : "\n\n[思考结束]\n";
595595
}
596-
if (result.choices[0].delta.content)
597-
data.choices[0].message.content += result.choices[0].delta.content;
596+
if (result.choices[0].delta.content) {
597+
if(result.choices[0].delta.type === "thinking" && !isFoldModel){
598+
data.choices[0].message.reasoning_content += result.choices[0].delta.content;
599+
}else {
600+
data.choices[0].message.content += result.choices[0].delta.content;
601+
}
602+
}
598603
if (result.choices && result.choices[0] && result.choices[0].finish_reason === "stop") {
599604
data.choices[0].message.content = data.choices[0].message.content.replace(/^\n+/, '').replace(/\[citation:\d+\]/g, '') + (refContent ? `\n\n搜索结果来自:\n${refContent}` : '');
600605
resolve(data);
@@ -640,7 +645,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
640645
choices: [
641646
{
642647
index: 0,
643-
delta: { role: "assistant", content: "" },
648+
delta: { role: "assistant", content: "" , reasoning_content: "" },
644649
finish_reason: null,
645650
},
646651
],
@@ -676,7 +681,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
676681
}
677682
return;
678683
}
679-
if (result.choices[0].delta.type === "thinking") {
684+
if (isFoldModel && result.choices[0].delta.type === "thinking") {
680685
if (!thinking && isThinkingModel && !isSilentModel) {
681686
thinking = true;
682687
transStream.write(`data: ${JSON.stringify({
@@ -696,7 +701,7 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
696701
if (isSilentModel)
697702
return;
698703
}
699-
else if (thinking && isThinkingModel && !isSilentModel) {
704+
else if (isFoldModel && thinking && isThinkingModel && !isSilentModel) {
700705
thinking = false;
701706
transStream.write(`data: ${JSON.stringify({
702707
id: `${refConvId}@${result.message_id}`,
@@ -716,19 +721,25 @@ function createTransStream(model: string, stream: any, refConvId: string, endCal
716721
if (!result.choices[0].delta.content)
717722
return;
718723

724+
const deltaContent = result.choices[0].delta.content.replace(/\[citation:\d+\]/g, '');
725+
const delta = result.choices[0].delta.type === "thinking" && !isFoldModel
726+
? { role: "assistant", reasoning_content: deltaContent }
727+
: { role: "assistant", content: deltaContent };
728+
719729
transStream.write(`data: ${JSON.stringify({
720730
id: `${refConvId}@${result.message_id}`,
721731
model: result.model,
722732
object: "chat.completion.chunk",
723733
choices: [
724734
{
725735
index: 0,
726-
delta: { role: "assistant", content: result.choices[0].delta.content.replace(/\[citation:\d+\]/g, '') },
736+
delta,
727737
finish_reason: null,
728738
},
729739
],
730740
created,
731741
})}\n\n`);
742+
732743
if (result.choices && result.choices[0] && result.choices[0].finish_reason === "stop") {
733744
transStream.write(`data: ${JSON.stringify({
734745
id: `${refConvId}@${result.message_id}`,

0 commit comments

Comments
 (0)