adding
This commit is contained in:
26
server.js
26
server.js
@ -59,7 +59,7 @@ app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => {
|
||||
);
|
||||
|
||||
let finalAnswer = "";
|
||||
let lastChunk = "";
|
||||
let insideThink = false;
|
||||
|
||||
response.data.on("data", (chunk) => {
|
||||
const lines = chunk.toString("utf8").split("\n").filter(Boolean);
|
||||
@ -67,19 +67,31 @@ app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => {
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const json = JSON.parse(line);
|
||||
lastChunk = json.response || lastChunk;
|
||||
if (json.done) {
|
||||
finalAnswer = lastChunk;
|
||||
const text = json.response;
|
||||
|
||||
if (text?.includes("<think>")) {
|
||||
insideThink = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (text?.includes("</think>")) {
|
||||
insideThink = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!insideThink && text) {
|
||||
finalAnswer += text;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("Chunk parsing failed:", err);
|
||||
console.warn("Chunk parse failed:", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
response.data.on("end", () => {
|
||||
console.log("\n🎯 Final Answer Only:\n", finalAnswer);
|
||||
res.json({ response: finalAnswer.trim() });
|
||||
const cleaned = finalAnswer.trim();
|
||||
console.log("🎯 Final (excluding think block):\n", cleaned);
|
||||
res.json({ response: cleaned });
|
||||
});
|
||||
|
||||
response.data.on("error", (err) => {
|
||||
|
Reference in New Issue
Block a user