diff --git a/server.js b/server.js index 4c8d763..d140406 100644 --- a/server.js +++ b/server.js @@ -36,25 +36,46 @@ const validateApiKey = (req, res, next) => { // Forward request to localhost:11434 (ollama) app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => { try { - // Forwarding the request to localhost:11434 with the prompt console.log("Body: ", req.body); + const response = await axios.post( "http://localhost:11434/api/generate", req.body, + { + responseType: "stream", + headers: { + "Content-Type": "application/json", + }, + }, ); - console.log("Response: ", response.data); - // Send the response from localhost:11434 back to the client - res.status(response.status).json(response.data); + res.setHeader("Content-Type", "application/json"); + + // Log stream content and pipe to client + response.data.on("data", (chunk) => { + const text = chunk.toString(); + console.log("Streamed chunk from Ollama:", text); + res.write(text); // Forward chunk to client + }); + + response.data.on("end", () => { + console.log("Streaming complete."); + res.end(); // Finalize response + }); + + response.data.on("error", (err) => { + console.error("Stream error:", err); + res.status(500).end("Stream error."); + }); } catch (error) { - // Enhanced error logging console.error( "Error forwarding request to localhost:11434:", - error.response ? error.response.data : error.message, + error.response?.data || error.message, ); - res - .status(500) - .json({ error: "Internal Server Error", message: error.message }); + res.status(500).json({ + error: "Internal Server Error", + message: error.message, + }); } });