modifying endpoint

This commit is contained in:
Carlos
2025-04-10 00:50:33 -04:00
parent 537c62f808
commit 2a8e019ea7

View File

@ -36,25 +36,46 @@ const validateApiKey = (req, res, next) => {
// Forward request to localhost:11434 (ollama) // Forward request to localhost:11434 (ollama)
app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => { app.post("/api/generate/chat/completions", validateApiKey, async (req, res) => {
try { try {
// Forwarding the request to localhost:11434 with the prompt
console.log("Body: ", req.body); console.log("Body: ", req.body);
const response = await axios.post( const response = await axios.post(
"http://localhost:11434/api/generate", "http://localhost:11434/api/generate",
req.body, 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.setHeader("Content-Type", "application/json");
res.status(response.status).json(response.data);
// 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) { } catch (error) {
// Enhanced error logging
console.error( console.error(
"Error forwarding request to localhost:11434:", "Error forwarding request to localhost:11434:",
error.response ? error.response.data : error.message, error.response?.data || error.message,
); );
res res.status(500).json({
.status(500) error: "Internal Server Error",
.json({ error: "Internal Server Error", message: error.message }); message: error.message,
});
} }
}); });