File size: 8,168 Bytes
1d3f990 | 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | /**
* Ahmad JIT Engine
* Browser-based LLM inference using WebLLM (ES module)
* Model: TinyLlama-1.1B-Chat-v1.0-q4f32_1-MLC
*/
class AhmadJITEngine {
constructor() {
this.engine = null;
this.state = 'OFFLINE';
this.conversationHistory = [];
this.notebookCells = [];
this.abortController = null;
this.modelId = 'TinyLlama-1.1B-Chat-v1.0-q4f32_1-MLC'; // Real WebLLM model
}
async initialize() {
try {
this.state = 'CHECKING_WEBGPU';
if (!navigator.gpu) {
console.warn('WebGPU not available, will use CPU (slower)');
} else {
console.log('✓ WebGPU available for acceleration');
}
this.state = 'LOADING';
console.log('Dynamically importing WebLLM ES module...');
// Dynamic import for ES module (not a global script)
let webllm;
try {
webllm = await import('https://esm.run/@mlc-ai/web-llm');
console.log('✓ WebLLM ES module imported successfully');
} catch (importError) {
console.error('Failed to import WebLLM:', importError);
throw new Error(`Failed to import WebLLM ES module: ${importError.message}`);
}
// Verify CreateMLCEngine is available
if (!webllm.CreateMLCEngine) {
throw new Error('WebLLM loaded, but CreateMLCEngine is unavailable. Check module export.');
}
console.log('✓ CreateMLCEngine available from WebLLM module');
// Create engine using CreateMLCEngine (not MLCEngine constructor)
this.state = 'DOWNLOADING_MODEL';
console.log(`Creating MLCEngine for model: ${this.modelId}`);
try {
this.engine = await webllm.CreateMLCEngine(this.modelId, {
initProgressCallback: (progress) => {
console.log('Model loading progress:', progress);
},
});
console.log('✓ MLCEngine created and model initialized successfully');
} catch (engineError) {
console.error('Failed to create engine:', engineError);
throw new Error(`Failed to create MLCEngine: ${engineError.message}`);
}
this.state = 'INDEXING';
this.buildNotebookIndex();
this.state = 'READY';
console.log('✓ Ahmad Bot ready!');
return true;
} catch (error) {
this.state = 'ERROR';
console.error('Ahmad JIT Engine initialization failed:', error);
throw error;
}
}
buildNotebookIndex() {
this.notebookCells = [];
const cellElements = document.querySelectorAll(
'[data-cell-id], .notebook-cell, .cell, [class*="cell"]'
);
let cellIndex = 0;
cellElements.forEach((elem) => {
if (elem.contains(document.getElementById('ahmad-jit-box'))) {
return; // Skip Ahmad Bot's own UI
}
const cellId = elem.dataset.cellId || `ROWM-${String(cellIndex + 1).padStart(3, '0')}`;
elem.dataset.cellId = cellId;
const text = elem.textContent.substring(0, 500).trim();
if (text.length > 0) {
this.notebookCells.push({
id: cellId,
text: text,
element: elem,
});
cellIndex++;
}
});
console.log(`Indexed ${this.notebookCells.length} notebook cells`);
}
findRelevantCells(question, maxCells = 3) {
const questionTerms = question.toLowerCase().split(/\s+/);
const scored = this.notebookCells.map((cell) => {
const cellLower = cell.text.toLowerCase();
const matches = questionTerms.filter((term) => cellLower.includes(term)).length;
return { cell, score: matches };
});
return scored
.filter((s) => s.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, maxCells)
.map((s) => s.cell);
}
buildSystemPrompt(question) {
const relevant = this.findRelevantCells(question);
let context = 'NOTEBOOK CONTEXT:\n\n';
relevant.forEach((cell) => {
context += `[${cell.id}]\n${cell.text}\n\n`;
});
return `You are Ahmad Bot, the embedded local assistant for the ROWM Polymorphic Notebook.
Answer from the supplied notebook cells.
Cite notebook cells using their real identifiers.
Do not invent missing information.
Preserve Unicode exactly.
Do not claim a hash is a signature.
Do not claim visual animation is formal verification.
${context}
USER QUESTION:
${question}`;
}
async generate(userMessage, onToken) {
if (this.state !== 'READY') {
throw new Error(`Engine not ready: ${this.state}`);
}
if (!userMessage || userMessage.trim().length === 0) {
throw new Error('User message cannot be empty');
}
this.state = 'GENERATING';
this.abortController = new AbortController();
let fullResponse = '';
try {
const systemPrompt = this.buildSystemPrompt(userMessage);
const messages = [
{ role: 'system', content: systemPrompt },
...this.conversationHistory,
{ role: 'user', content: userMessage },
];
console.log('Generating response with WebLLM...');
// WebLLM completion() is the correct streaming method
const response = await this.engine.completion({
messages: messages,
temperature: 0.2,
top_p: 0.9,
max_tokens: 600,
stream: true,
});
// Stream tokens from response
for await (const chunk of response) {
if (this.abortController.signal.aborted) {
break;
}
// WebLLM chunk has format: { choices: [{ delta: { content: "token" } }] }
const content = chunk.choices?.[0]?.delta?.content ?? '';
if (content && typeof content === 'string') {
fullResponse += content;
if (onToken && typeof onToken === 'function') {
try {
onToken(content);
} catch (callbackError) {
console.error('onToken callback error:', callbackError);
}
}
}
}
// Store in conversation history
this.conversationHistory.push({ role: 'user', content: userMessage });
this.conversationHistory.push({ role: 'assistant', content: fullResponse });
this.state = 'READY';
return fullResponse;
} catch (error) {
if (error.name === 'AbortError') {
this.state = 'STOPPED';
return fullResponse;
}
this.state = 'ERROR';
console.error('Generation error:', error);
throw error;
}
}
stop() {
if (this.abortController) {
this.abortController.abort();
}
}
resetConversation() {
this.conversationHistory = [];
}
unload() {
if (this.engine) {
this.engine.terminate?.();
}
this.engine = null;
this.state = 'OFFLINE';
}
getState() {
return this.state;
}
}
// Global instance
window.ahmadEngine = null;
// Export
if (typeof module !== 'undefined' && module.exports) {
module.exports = AhmadJITEngine;
}
|