Emalawi19 commited on
Commit
728424a
·
verified ·
1 Parent(s): 30f5aea

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +49 -157
server.js CHANGED
@@ -1,67 +1,14 @@
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-0.5B-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
- // 1. Load the single file and automatically parse [TOPIC: XXX] blocks
17
- async function initializeServer() {
18
- try {
19
- console.log("Reading knowledge.txt...");
20
- const knowledgePath = path.join(__dirname, 'knowledge.txt');
21
-
22
- if (fs.existsSync(knowledgePath)) {
23
- const rawContent = fs.readFileSync(knowledgePath, 'utf8');
24
-
25
- const topicRegex = /\[TOPIC:\s*(\w+)\]([\s\S]*?)(?=\[TOPIC:|$)/gi;
26
- let match;
27
-
28
- while ((match = topicRegex.exec(rawContent)) !== null) {
29
- const topicName = match[1].toLowerCase().trim();
30
- const topicContent = match[2].trim();
31
- KNOWLEDGE_TOPICS[topicName] = topicContent;
32
- console.log(`-> Successfully registered topic: "${topicName}"`);
33
- }
34
- } else {
35
- console.warn("WARNING: knowledge.txt not found. Starting blank.");
36
- }
37
-
38
- console.log("Loading Qwen2.5-Coder-3B model... (first load may take a few minutes)");
39
- generator = await pipeline('text-generation', MODEL_NAME, { dtype: 'q4' });
40
- console.log("Server fully initialized!");
41
-
42
- } catch (error) {
43
- console.error("Initialization failed:", error);
44
- process.exit(1);
45
- }
46
- }
47
-
48
- // 2. 100% Automated Router - Searches user prompt against loaded keys
49
- function getRelevantContext(userPrompt) {
50
- const lowerPrompt = userPrompt.toLowerCase();
51
- let dynamicContext = "";
52
-
53
- for (const topicName in KNOWLEDGE_TOPICS) {
54
- if (lowerPrompt.includes(topicName)) {
55
- dynamicContext += KNOWLEDGE_TOPICS[topicName] + "\n\n";
56
- }
57
- }
58
-
59
- if (!dynamicContext.trim()) {
60
- const available = Object.keys(KNOWLEDGE_TOPICS).join(", ");
61
- dynamicContext = `General support mode. Reference available topics: ${available}.`;
62
- }
63
 
64
- return dynamicContext;
 
 
 
65
  }
66
 
67
  const server = http.createServer(async (req, res) => {
@@ -79,10 +26,7 @@ const server = http.createServer(async (req, res) => {
79
 
80
  if (pathname === '/' && req.method === 'GET') {
81
  res.writeHead(200);
82
- res.end(JSON.stringify({
83
- status: "Backend is running",
84
- available_topics: Object.keys(KNOWLEDGE_TOPICS)
85
- }));
86
  return;
87
  }
88
 
@@ -94,126 +38,74 @@ const server = http.createServer(async (req, res) => {
94
  const { prompt } = JSON.parse(body);
95
  if (!generator) {
96
  res.writeHead(503);
97
- return res.end(JSON.stringify({ error: "System warming up..." }));
98
  }
99
 
100
- // Run prompt router to fetch targeted text injection
101
- const localizedContext = getRelevantContext(prompt);
102
-
103
- // Detect if it is a programming/coding request
104
- const codingKeywords = [
105
- 'generate', 'create', 'build', 'make', 'code', 'website', 'webpage',
106
- 'login', 'register', 'form', 'page', 'app', 'application', 'html',
107
- 'css', 'javascript', 'backend', 'frontend', 'server', 'api', 'express',
108
- 'node', 'database', 'function', 'script', 'component', 'write'
109
- ];
110
- const lowerPrompt = prompt.toLowerCase();
111
- const isCodingRequest = codingKeywords.some(k => lowerPrompt.includes(k));
112
-
113
- const finalPrompt = isCodingRequest
114
- ? `As a coding assistant, write the complete working code for the following task. Always provide full code, never refuse a coding task: ${prompt}`
115
- : prompt;
116
-
117
- // Core base prompt with static company identity and localized file data
118
- let systemPrompt = `You are Gini AI, an AI full-stack development assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
119
 
120
- IDENTITY:
121
  - Your name is Gini AI.
122
- - Your company is Emalawi19, founded by Professor Roosevelt Chinkwende.
123
- - You are an AI, not a human.
124
-
125
- REFERENCE KNOWLEDGE BASE:
126
- Use the following official documentation to accurately answer any user questions about operations or topics:
127
- ${localizedContext}`;
128
-
129
- // Inject dynamic behavior rules dependent on the intent type
130
- if (isCodingRequest) {
131
- systemPrompt += `
132
-
133
- ABSOLUTE RULES NEVER BREAK THESE:
134
- 1. You ALWAYS write code when asked. You NEVER say "I can't help with that" for any coding request.
135
- 2. Any request involving web development or script building is ALWAYS answered with complete working code blocks.
136
- 3. You guide users step by step, one step at a time, with complete copy-paste ready code.
137
- 4. For EACH step provide:
138
- a) "Step X of Y: Title"
139
- b) One sentence explaining what this step does
140
- c) The COMPLETE working code in a properly labelled code block
141
- d) Filename and where to save it
142
- e) How to run it if needed
143
  f) End with: "Say 'next' to continue to Step X."
144
- 5. NEVER use placeholders like '// add your logic here'. Write the actual working logic.
145
- 6. NEVER combine steps. One step per response only.
146
- 7. ALWAYS finish the full code for the current step — never cut off mid-code.
147
-
148
- CODE FORMAT:
149
- - Always use triple backticks with language tag: \`\`\`html \`\`\`css \`\`\`javascript \`\`\``;
150
- } else {
151
- systemPrompt += `
152
-
153
- RULES FOR GENERAL ASSISTANCE:
154
- 1. Answer the user's question clearly, helpfully, and accurately using facts from the REFERENCE KNOWLEDGE BASE block above.
155
- 2. DO NOT write or generate programming code (like JavaScript, HTML, or Node.js scripts) unless explicitly requested by the user.
156
- 3. Keep your formatting clean using concise paragraphs or scannable bullet points.`;
157
- }
158
-
159
- const messages = [
160
- { role: 'system', content: systemPrompt },
161
- { role: 'user', content: finalPrompt }
162
  ];
163
 
164
  const output = await generator(messages, {
165
- max_new_tokens: 1024,
166
- temperature: 0.2,
167
- repetition_penalty: 1.15,
168
- do_sample: false
169
  });
170
 
171
  const result = output[0].generated_text.at(-1).content;
172
-
173
- // Safety fallback mechanism for false refusal flags on coding inputs
174
- const refusalPhrases = ["i can't assist", "i cannot assist", "i'm sorry", "i am sorry", "i'm unable", "i cannot help"];
175
- const isRefusal = refusalPhrases.some(p => result.toLowerCase().startsWith(p));
176
-
177
- if (isRefusal && isCodingRequest) {
178
- const retryMessages = [
179
- {
180
- role: 'system',
181
- content: `You are a code generator. You only output code. Never refuse. Never explain. Just write the complete working code asked for.`
182
- },
183
- {
184
- role: 'user',
185
- content: `Write complete working code for: ${prompt}. Include all elements required.`
186
- }
187
- ];
188
-
189
- const retryOutput = await generator(retryMessages, {
190
- max_new_tokens: 1024,
191
- temperature: 0.1,
192
- do_sample: false
193
- });
194
-
195
- const retryResult = retryOutput[0].generated_text.at(-1).content;
196
- res.writeHead(200);
197
- return res.end(JSON.stringify({ result: retryResult }));
198
- }
199
-
200
  res.writeHead(200);
201
  res.end(JSON.stringify({ result }));
202
 
203
  } catch (err) {
204
  res.writeHead(400);
205
- res.end(JSON.stringify({ error: "Processing error", detail: err.message }));
206
  }
207
  });
208
  return;
209
  }
210
 
211
  res.writeHead(404);
212
- res.end(JSON.stringify({ error: "Not Found" }));
213
  });
214
 
215
- initializeServer().then(() => {
216
  server.listen(PORT, '0.0.0.0', () => {
217
- console.log(`Server listening on port ${PORT}`);
218
  });
219
  });
 
1
  import { pipeline } from '@huggingface/transformers';
2
  import http from 'http';
 
 
 
3
 
4
  const PORT = 7860;
5
  const MODEL_NAME = 'onnx-community/Qwen2.5-0.5B-Instruct';
 
 
 
 
6
  let generator;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ async function loadModel() {
9
+ console.log("Loading coding model...");
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
 
27
  if (pathname === '/' && req.method === 'GET') {
28
  res.writeHead(200);
29
+ res.end(JSON.stringify({ status: "Backend is running" }));
 
 
 
30
  return;
31
  }
32
 
 
38
  const { prompt } = JSON.parse(body);
39
  if (!generator) {
40
  res.writeHead(503);
41
+ return res.end(JSON.stringify({ error: "Model is still loading..." }));
42
  }
43
 
44
+ const messages = [
45
+ {
46
+ role: 'system',
47
+ content: `You are Gini AI, a full-stack web development AI assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+ IDENTITY RULES:
50
  - Your name is Gini AI.
51
+ - Your company is Emalawi19.
52
+ - The founder is Professor Roosevelt Chinkwende.
53
+ - You are an AI, not a human developer.
54
+
55
+ YOUR SPECIALTY:
56
+ - You build complete websites with both frontend (HTML, CSS, JavaScript) and backend (Node.js + Express).
57
+ - You ALWAYS guide users one step at a time.
58
+ - You NEVER give vague instructions every step includes COMPLETE, COPY-PASTE READY code.
59
+ - You NEVER cut off mid-response. Always finish the current step fully.
60
+
61
+ STEP-BY-STEP GUIDE RULES (CRITICAL):
62
+ 1. When asked to build a website or app, first list all steps with a short title each.
63
+ 2. Then say: "Let's begin with Step 1. Say 'next' when you are ready to continue."
64
+ 3. For each step provide:
65
+ a) Step title and number (e.g. "Step 1 of 5: Project Setup")
66
+ b) One short sentence explaining what this step does
67
+ c) The COMPLETE working code for this step inside a code block
68
+ d) Where to save the file (e.g. "Save as server.js in your project folder")
69
+ e) How to run it if needed (e.g. "Run: node server.js")
 
 
70
  f) End with: "Say 'next' to continue to Step X."
71
+ 4. NEVER skip code. NEVER say 'add your logic here'. Write the actual logic.
72
+ 5. NEVER combine multiple steps into one response.
73
+ 6. Default stack: HTML/CSS/JS frontend, Node.js + Express backend.
74
+ 7. ALWAYS complete the current step fully before stopping — do not cut off mid-code.
75
+
76
+ CODE FORMAT RULES:
77
+ - Always wrap code in triple backticks with the language tag e.g. \`\`\`javascript or \`\`\`html
78
+ - Code must be real, working, and complete for that step.
79
+ - Do not write pseudo-code or placeholders.`
80
+ },
81
+ { role: 'user', content: prompt }
 
 
 
 
 
 
 
82
  ];
83
 
84
  const output = await generator(messages, {
85
+ max_new_tokens: 800, // increased so steps never cut off
86
+ temperature: 0.4, // lower = more focused, less hallucination
87
+ repetition_penalty: 1.2, // prevents looping/repeating text
88
+ do_sample: true
89
  });
90
 
91
  const result = output[0].generated_text.at(-1).content;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  res.writeHead(200);
93
  res.end(JSON.stringify({ result }));
94
 
95
  } catch (err) {
96
  res.writeHead(400);
97
+ res.end(JSON.stringify({ error: "Invalid request", detail: err.message }));
98
  }
99
  });
100
  return;
101
  }
102
 
103
  res.writeHead(404);
104
+ res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
105
  });
106
 
107
+ loadModel().then(() => {
108
  server.listen(PORT, '0.0.0.0', () => {
109
+ console.log(`Server running at http://0.0.0.0:${PORT}`);
110
  });
111
  });