BackEndAPI / index.js
Chysev's picture
Update index.js
7f52b00 verified
raw
history blame
1.11 kB
import express from "express"
import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp"
import path from "path"
import cors from "cors"
const app = express();
const port = 7860;
app.use(cors());
app.use(express.json());
// 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 reqData = await req.json();
const userInput = reqData.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(userInput);
console.log(reqData);
console.log(aiAnswer);
res.json({ aiAnswer });
});
app.listen(port, () => {
console.log(`Express server is running on port ${port}`);
});