Chysev commited on
Commit
e4584af
·
verified ·
1 Parent(s): 8820311

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +42 -10
index.js CHANGED
@@ -1,14 +1,46 @@
1
- var express = require('express');
 
 
 
2
 
3
- // Constants
4
- var DEFAULT_PORT = 7860;
5
- var PORT = process.env.PORT || DEFAULT_PORT;
6
 
7
- // App
8
- var app = express();
9
- app.get('/', function (req, res) {
10
- res.send('Hello World\n');
 
 
 
 
 
 
11
  });
12
 
13
- app.listen(PORT)
14
- console.log('Running on http://localhost:' + PORT);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ });