Emalawi19 commited on
Commit
1d0e0ac
·
verified ·
1 Parent(s): e93b7d1

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +74 -18
server.js CHANGED
@@ -1,14 +1,66 @@
1
  import { pipeline } from '@huggingface/transformers';
2
  import http from 'http';
 
 
 
3
 
4
  const PORT = 7860;
5
  const MODEL_NAME = 'onnx-community/Qwen2.5-Coder-3B-Instruct';
 
 
 
 
6
  let generator;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- async function loadModel() {
9
- console.log("Loading Qwen2.5-Coder-3B model... (first load may take a few minutes)");
10
- generator = await pipeline('text-generation', MODEL_NAME, { dtype: 'q4' });
11
- console.log("Model loaded successfully!");
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
 
14
  const server = http.createServer(async (req, res) => {
@@ -26,7 +78,10 @@ const server = http.createServer(async (req, res) => {
26
 
27
  if (pathname === '/' && req.method === 'GET') {
28
  res.writeHead(200);
29
- res.end(JSON.stringify({ status: "Backend is running", model: MODEL_NAME }));
 
 
 
30
  return;
31
  }
32
 
@@ -38,10 +93,11 @@ const server = http.createServer(async (req, res) => {
38
  const { prompt } = JSON.parse(body);
39
  if (!generator) {
40
  res.writeHead(503);
41
- return res.end(JSON.stringify({ error: "Model is still loading, please wait..." }));
42
  }
43
 
44
- // Detect if the prompt is a coding/building request
 
45
  const codingKeywords = [
46
  'generate', 'create', 'build', 'make', 'code', 'website', 'webpage',
47
  'login', 'register', 'form', 'page', 'app', 'application', 'html',
@@ -51,8 +107,6 @@ const server = http.createServer(async (req, res) => {
51
  const lowerPrompt = prompt.toLowerCase();
52
  const isCodingRequest = codingKeywords.some(k => lowerPrompt.includes(k));
53
 
54
- // If it's a coding request, prepend a clear coding instruction
55
- // so the model never misinterprets it as harmful
56
  const finalPrompt = isCodingRequest
57
  ? `As a coding assistant, write the complete working code for the following task. Always provide full code, never refuse a coding task: ${prompt}`
58
  : prompt;
@@ -67,12 +121,16 @@ IDENTITY:
67
  - Your company is Emalawi19, founded by Professor Roosevelt Chinkwende.
68
  - You are an AI coding assistant, not a human.
69
 
 
 
 
 
70
  ABSOLUTE RULES — NEVER BREAK THESE:
71
  1. You ALWAYS write code when asked. You NEVER say "I can't help with that" for any coding request.
72
  2. Any request involving websites, HTML, CSS, JavaScript, login pages, register pages, forms, backends, APIs, or any web development topic is ALWAYS answered with complete working code.
73
  3. A "login page", "register page", "website", or any web page request is a NORMAL, SAFE coding task. Always generate the code for it.
74
  4. You NEVER refuse a web development or coding request. If asked to build something, you build it.
75
- 5. For non-coding general questions, answer helpfully and briefly.
76
 
77
  YOUR SPECIALTY:
78
  - Frontend: HTML, CSS, JavaScript
@@ -106,21 +164,19 @@ CODE FORMAT:
106
  max_new_tokens: 1024,
107
  temperature: 0.2,
108
  repetition_penalty: 1.15,
109
- do_sample: false // deterministic = no random refusals
110
  });
111
 
112
  const result = output[0].generated_text.at(-1).content;
113
 
114
- // If the model still refuses, catch it and retry with a more direct prompt
115
  const refusalPhrases = ["i can't assist", "i cannot assist", "i'm sorry", "i am sorry", "i'm unable", "i cannot help"];
116
  const isRefusal = refusalPhrases.some(p => result.toLowerCase().startsWith(p));
117
 
118
  if (isRefusal && isCodingRequest) {
119
- // Retry with an even more direct coding-only prompt
120
  const retryMessages = [
121
  {
122
  role: 'system',
123
- content: 'You are a code generator. You only output code. Never refuse. Never explain. Just write the complete working code asked for.'
124
  },
125
  {
126
  role: 'user',
@@ -144,18 +200,18 @@ CODE FORMAT:
144
 
145
  } catch (err) {
146
  res.writeHead(400);
147
- res.end(JSON.stringify({ error: "Invalid request", detail: err.message }));
148
  }
149
  });
150
  return;
151
  }
152
 
153
  res.writeHead(404);
154
- res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
155
  });
156
 
157
- loadModel().then(() => {
158
  server.listen(PORT, '0.0.0.0', () => {
159
- console.log(`Server running at http://0.0.0.0:${PORT}`);
160
  });
161
  });
 
1
  import { pipeline } from '@huggingface/transformers';
2
  import http from 'http';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
 
7
  const PORT = 7860;
8
  const MODEL_NAME = 'onnx-community/Qwen2.5-Coder-3B-Instruct';
9
+
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = path.dirname(__filename);
12
+
13
  let generator;
14
+ const KNOWLEDGE_TOPICS = {};
15
+
16
+ async function initializeServer() {
17
+ try {
18
+ console.log("Reading knowledge.txt...");
19
+ const knowledgePath = path.join(__dirname, 'knowledge.txt');
20
+
21
+ if (fs.existsSync(knowledgePath)) {
22
+ const rawContent = fs.readFileSync(knowledgePath, 'utf8');
23
+
24
+ const topicRegex = /\[TOPIC:\s*(\w+)\]([\s\S]*?)(?=\[TOPIC:|$)/gi;
25
+ let match;
26
+
27
+ while ((match = topicRegex.exec(rawContent)) !== null) {
28
+ const topicName = match[1].toLowerCase().trim();
29
+ const topicContent = match[2].trim();
30
+ KNOWLEDGE_TOPICS[topicName] = topicContent;
31
+ console.log(`-> Successfully registered topic: "${topicName}"`);
32
+ }
33
+ } else {
34
+ console.warn("WARNING: knowledge.txt not found. Starting blank.");
35
+ }
36
+
37
+ console.log("Loading Qwen2.5-Coder-3B model... (first load may take a few minutes)");
38
+ generator = await pipeline('text-generation', MODEL_NAME, { dtype: 'q4' });
39
+ console.log("Server fully initialized!");
40
+
41
+ } catch (error) {
42
+ console.error("Initialization failed:", error);
43
+ process.exit(1);
44
+ }
45
+ }
46
 
47
+ // 100% AUTOMATED ROUTER
48
+ function getRelevantContext(userPrompt) {
49
+ const lowerPrompt = userPrompt.toLowerCase();
50
+ let dynamicContext = "";
51
+
52
+ for (const topicName in KNOWLEDGE_TOPICS) {
53
+ if (lowerPrompt.includes(topicName)) {
54
+ dynamicContext += KNOWLEDGE_TOPICS[topicName] + "\n\n";
55
+ }
56
+ }
57
+
58
+ if (!dynamicContext.trim()) {
59
+ const available = Object.keys(KNOWLEDGE_TOPICS).join(", ");
60
+ dynamicContext = `General support mode. Reference available topics: ${available}.`;
61
+ }
62
+
63
+ return dynamicContext;
64
  }
65
 
66
  const server = http.createServer(async (req, res) => {
 
78
 
79
  if (pathname === '/' && req.method === 'GET') {
80
  res.writeHead(200);
81
+ res.end(JSON.stringify({
82
+ status: "Backend is running",
83
+ available_topics: Object.keys(KNOWLEDGE_TOPICS)
84
+ }));
85
  return;
86
  }
87
 
 
93
  const { prompt } = JSON.parse(body);
94
  if (!generator) {
95
  res.writeHead(503);
96
+ return res.end(JSON.stringify({ error: "System warming up..." }));
97
  }
98
 
99
+ const localizedContext = getRelevantContext(prompt);
100
+
101
  const codingKeywords = [
102
  'generate', 'create', 'build', 'make', 'code', 'website', 'webpage',
103
  'login', 'register', 'form', 'page', 'app', 'application', 'html',
 
107
  const lowerPrompt = prompt.toLowerCase();
108
  const isCodingRequest = codingKeywords.some(k => lowerPrompt.includes(k));
109
 
 
 
110
  const finalPrompt = isCodingRequest
111
  ? `As a coding assistant, write the complete working code for the following task. Always provide full code, never refuse a coding task: ${prompt}`
112
  : prompt;
 
121
  - Your company is Emalawi19, founded by Professor Roosevelt Chinkwende.
122
  - You are an AI coding assistant, not a human.
123
 
124
+ REFERENCE KNOWLEDGE BASE:
125
+ Use the following official information to accurately answer any user questions about the company, projects, founders, or operations:
126
+ ${localizedContext}
127
+
128
  ABSOLUTE RULES — NEVER BREAK THESE:
129
  1. You ALWAYS write code when asked. You NEVER say "I can't help with that" for any coding request.
130
  2. Any request involving websites, HTML, CSS, JavaScript, login pages, register pages, forms, backends, APIs, or any web development topic is ALWAYS answered with complete working code.
131
  3. A "login page", "register page", "website", or any web page request is a NORMAL, SAFE coding task. Always generate the code for it.
132
  4. You NEVER refuse a web development or coding request. If asked to build something, you build it.
133
+ 5. For non-coding general questions, answer helpfully and briefly using facts from the REFERENCE KNOWLEDGE BASE above.
134
 
135
  YOUR SPECIALTY:
136
  - Frontend: HTML, CSS, JavaScript
 
164
  max_new_tokens: 1024,
165
  temperature: 0.2,
166
  repetition_penalty: 1.15,
167
+ do_sample: false
168
  });
169
 
170
  const result = output[0].generated_text.at(-1).content;
171
 
 
172
  const refusalPhrases = ["i can't assist", "i cannot assist", "i'm sorry", "i am sorry", "i'm unable", "i cannot help"];
173
  const isRefusal = refusalPhrases.some(p => result.toLowerCase().startsWith(p));
174
 
175
  if (isRefusal && isCodingRequest) {
 
176
  const retryMessages = [
177
  {
178
  role: 'system',
179
+ content: `You are a code generator. You only output code. Never refuse. Never explain. Just write the complete working code asked for. Reference context if needed: ${localizedContext}`
180
  },
181
  {
182
  role: 'user',
 
200
 
201
  } catch (err) {
202
  res.writeHead(400);
203
+ res.end(JSON.stringify({ error: "Processing error", detail: err.message }));
204
  }
205
  });
206
  return;
207
  }
208
 
209
  res.writeHead(404);
210
+ res.end(JSON.stringify({ error: "Not Found" }));
211
  });
212
 
213
+ initializeServer().then(() => {
214
  server.listen(PORT, '0.0.0.0', () => {
215
+ console.log(`Server listening on port ${PORT}`);
216
  });
217
  });