forked from andreykobal/llama-unity-client-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (114 loc) · 2.95 KB
/
server.js
File metadata and controls
142 lines (114 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//server.js
import express from 'express';
import http from 'http';
import { Server } from 'socket.io';
import bodyParser from 'body-parser';
import { spawn } from 'child_process';
import path from 'path';
import cors from 'cors';
const app = express();
app.use(cors({
origin: 'http://192.168.1.137:8080'
}));
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: 'http://192.168.1.137:8080',
methods: ['GET', 'POST']
}
});
const PORT = 80;
app.use(bodyParser.json());
const CHAT_APP_LOCATION = path.join(process.cwd(), "llama.cpp", "main");
const socketPrograms = new Map();
// Handle socket connections
io.on("connection", (socket) => {
console.log(`A user connected with ID: ${socket.id}`);
let program = spawn(CHAT_APP_LOCATION, [
"-m",
path.join(
process.cwd(),
"llama.cpp",
"models",
"luna-ai-llama2-uncensored.ggmlv3.q5_1.bin"
),
"-n",
"512",
"--repeat_penalty",
"1.0",
"--color",
"-i",
"-r",
"User: ",
"-p",
"You are a helpful assistant. Answer questions and continue conversations."
]);
socketPrograms.set(socket.id, program);
setupProgramHandlers(socket, program);
socket.on("message", (message) => {
if (!message) {
socket.emit("error", "Message is required.");
return;
}
if (program && program.stdin) {
program.stdin.write("User: " + message + "\n");
}
});
socket.on("disconnect", () => {
console.log(`User disconnected with ID: ${socket.id}`);
const program = socketPrograms.get(socket.id);
if (program) {
program.kill();
socketPrograms.delete(socket.id);
}
});
});
function setupProgramHandlers(socket, program) {
program.stdout.on("data", (data) => {
const output = data.toString("utf8");
socket.emit("response", processOutput(output));
});
program.stderr.on("data", (data) => {
console.error(`Error: ${data}`);
});
program.on("close", (code) => {
if (code !== 0) {
socket.emit("error", "Failed to get response.");
}
});
}
let isTagOpen = false;
let closing = "";
function processOutput(output) {
if (output.includes("<")) {
isTagOpen = true;
}
if (output.includes("=>")) {
closing = "";
}
if (isTagOpen) {
closing = "";
}
if (output.includes(">")) {
closing = ">";
}
if (output) {
console.log(`Received from program: ${output.trim()}`);
// Emitting the "response" event is now handled outside of this function
} else {
console.log("No output received from the program.");
}
if (closing === ">" && !isTagOpen) {
closing = "";
io.emit("chatend"); // If this is the end, emit a "chatend" event
return "";
}
return output;
}
app.post("/llm-api/message", (req, res) => {
io.emit("message", req.body.message);
res.status(200).send({ message: "Message received" });
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});