Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,14 +1,46 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
var PORT = process.env.PORT || DEFAULT_PORT;
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
});
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const bodyParser = require('body-parser');
|
| 3 |
+
const { LlamaModel, LlamaContext, LlamaChatSession } = require('node-llama-cpp');
|
| 4 |
+
const path = require('path');
|
| 5 |
|
| 6 |
+
const app = express();
|
| 7 |
+
const port = 7860;
|
|
|
|
| 8 |
|
| 9 |
+
app.use(bodyParser.json());
|
| 10 |
+
|
| 11 |
+
// Test Express API GET method with parameters
|
| 12 |
+
app.get('/api/test', async (req, res) => {
|
| 13 |
+
const reqData = req.query;
|
| 14 |
+
res.json({
|
| 15 |
+
message: "Test getApiResponse GET success!",
|
| 16 |
+
method: "GET",
|
| 17 |
+
reqData,
|
| 18 |
+
});
|
| 19 |
});
|
| 20 |
|
| 21 |
+
// Test Express API POST method with variables
|
| 22 |
+
app.post('/api/test', async (req, res) => {
|
| 23 |
+
const reqData = req.body;
|
| 24 |
+
const userInput = reqData.userInput;
|
| 25 |
+
|
| 26 |
+
const model = new LlamaModel({
|
| 27 |
+
modelPath: path.join(
|
| 28 |
+
process.cwd(),
|
| 29 |
+
"model",
|
| 30 |
+
"orca-mini-3b-gguf2-q4_0.gguf"
|
| 31 |
+
),
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
const context = new LlamaContext({ model });
|
| 35 |
+
const session = new LlamaChatSession({ context });
|
| 36 |
+
const aiAnswer = await session.prompt(userInput);
|
| 37 |
+
|
| 38 |
+
console.log(reqData);
|
| 39 |
+
console.log(aiAnswer);
|
| 40 |
+
|
| 41 |
+
res.json({ aiAnswer });
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
app.listen(port, () => {
|
| 45 |
+
console.log(`Express server is running on port ${port}`);
|
| 46 |
+
});
|