This commit is contained in:
Carlos
2025-04-10 01:16:49 -04:00
parent 17e3f2bbed
commit baa4486c79

View File

@ -59,23 +59,32 @@ app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => {
); );
// 🔥 Manual stream reading for logging // 🔥 Manual stream reading for logging
console.log("Streaming response from Ollama..."); let fullResponse = "";
response.data.on("data", (chunk) => { response.data.on("data", (chunk) => {
const text = chunk.toString("utf8"); const lines = chunk.toString("utf8").split("\n").filter(Boolean); // Remove empty lines
console.log("🧩 Chunk:", text);
res.write(text); // forward to client for (const line of lines) {
try {
const json = JSON.parse(line);
fullResponse += json.response || "";
} catch (err) {
console.warn("Error parsing chunk:", err);
}
}
}); });
response.data.on("end", () => { response.data.on("end", () => {
console.log("✅ Streaming complete."); console.log("\n✅ Full Response:\n", fullResponse);
res.end(); res.json({ response: fullResponse });
}); });
response.data.on("error", (err) => { response.data.on("error", (err) => {
console.error("Stream error:", err.message); console.error("Stream error from Ollama:", err);
res.end(); res
.status(500)
.json({ error: "Stream error from Ollama", message: err.message });
}); });
} catch (error) { } catch (error) {
console.error( console.error(
"Error forwarding request to Ollama:", "Error forwarding request to Ollama:",