Skip to content

Commit

Permalink
don't use first msg
Browse files Browse the repository at this point in the history
  • Loading branch information
jackalcooper committed Feb 19, 2025
1 parent 7aebb28 commit c2398e6
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions app/api/siliconflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async function request(req: NextRequest) {
{
index: 0,
delta: {
content: null,
content: "",
reasoning_content: "",
role: "assistant",
},
Expand All @@ -246,13 +246,13 @@ async function request(req: NextRequest) {
},
};
type msg = typeof message0;
let msg0: msg | null = null;
let searchInjected = false;
try {
while (true && reader) {
const { done, value } = await reader.read();
if (done) break;

if (!msg0) {
if (!searchInjected) {
let msg0 = null;
const prefixData = "data: ";
const suffixLineBreak = "\n\n";
const msg_0_str = decoder.decode(value);
Expand All @@ -261,16 +261,24 @@ async function request(req: NextRequest) {

if (match && match[1]) {
msg0 = JSON.parse(match[1]) as msg;
msg0.choices[0].delta.reasoning_content =
searchPrependResult;
if (!!msg0.choices[0].delta.reasoning_content) {
msg0.choices[0].delta.reasoning_content = `${searchPrependResult}${msg0.choices[0].delta.reasoning_content}`;
searchInjected = true;
}
if (!!msg0.choices[0].delta.content) {
msg0.choices[0].delta.content = `${searchPrependResult}${msg0.choices[0].delta.reasoning_content}`;
searchInjected = true;
}
}

if (searchInjected) {
const customData = encoder.encode(
prefixData + JSON.stringify(msg0) + suffixLineBreak,
);
controller.enqueue(customData);
} else {
console.error("No JSON data found or invalid format.");
controller.enqueue(value);
}
const customData = encoder.encode(
prefixData + JSON.stringify(msg0) + suffixLineBreak,
);
controller.enqueue(customData);
controller.enqueue(encoder.encode(msg_0_str));
} else {
controller.enqueue(value);
}
Expand Down

0 comments on commit c2398e6

Please sign in to comment.