BackEndAPI / index.js
Chysev's picture
Update index.js
770fc7e verified
const express = require("express");
const path = require("path");
const cors = require("cors");
const app = express();
const port = 7860;
app.use(cors());
// Test Express API GET method with parameters
app.get("/api/test", async (req, res) => {
const reqData = req.query;
res.json({
message: "Test getApiResponse GET success!",
method: "GET",
reqData,
});
});
// Test Express API POST method with variables
app.post("/api/chat", async (req, res) => {
const { LlamaModel, LlamaContext, LlamaChatSession } = await import(
"node-llama-cpp"
);
const reqData = await req.body.userInput;
const model = new LlamaModel({
modelPath: path.join(
process.cwd(),
"Model",
"orca-mini-3b-gguf2-q4_0.gguf"
),
});
const context = new LlamaContext({ model });
const session = new LlamaChatSession({ context });
const aiAnswer = await session.prompt(reqData);
console.log(reqData);
console.log(aiAnswer);
res.json({ aiAnswer });
});
app.listen(port, () => {
console.log(`Express server is running on port ${port}`);
});