adding
This commit is contained in:
27
server.js
27
server.js
@ -59,23 +59,32 @@ app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => {
|
||||
);
|
||||
|
||||
// 🔥 Manual stream reading for logging
|
||||
console.log("Streaming response from Ollama...");
|
||||
let fullResponse = "";
|
||||
|
||||
response.data.on("data", (chunk) => {
|
||||
const text = chunk.toString("utf8");
|
||||
console.log("🧩 Chunk:", text);
|
||||
res.write(text); // forward to client
|
||||
const lines = chunk.toString("utf8").split("\n").filter(Boolean); // Remove empty lines
|
||||
|
||||
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", () => {
|
||||
console.log("✅ Streaming complete.");
|
||||
res.end();
|
||||
console.log("\n✅ Full Response:\n", fullResponse);
|
||||
res.json({ response: fullResponse });
|
||||
});
|
||||
|
||||
response.data.on("error", (err) => {
|
||||
console.error("❌ Stream error:", err.message);
|
||||
res.end();
|
||||
console.error("Stream error from Ollama:", err);
|
||||
res
|
||||
.status(500)
|
||||
.json({ error: "Stream error from Ollama", message: err.message });
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Error forwarding request to Ollama:",
|
||||
|
Reference in New Issue
Block a user