Chysev commited on
Commit
e9d637f
·
verified ·
1 Parent(s): 6d0423b

Update index.js

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