Spaces:
Running
Running
Update core/localmodel.mjs
Browse files- core/localmodel.mjs +25 -8
core/localmodel.mjs
CHANGED
|
@@ -11,7 +11,15 @@ export const APLocalModel = {
|
|
| 11 |
wllama: null,
|
| 12 |
label: 'MiMo-V2.6-Distill-Qwen-9B (local en este navegador)',
|
| 13 |
async cleanupCache() {
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
},
|
| 16 |
async load(onProgress) {
|
| 17 |
if (this.state === 'ready' || this.state === 'downloading') return;
|
|
@@ -39,16 +47,25 @@ export const APLocalModel = {
|
|
| 39 |
throw e;
|
| 40 |
}
|
| 41 |
},
|
|
|
|
| 42 |
async chat(messages, { maxTokens = 640, temperature = 0.2, onToken } = {}) {
|
| 43 |
if (this.state !== 'ready') throw new Error('modelo local no cargado');
|
| 44 |
-
|
| 45 |
-
let
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
-
return answer;
|
| 52 |
},
|
| 53 |
};
|
| 54 |
globalThis.APLocalModel = APLocalModel;
|
|
|
|
| 11 |
wllama: null,
|
| 12 |
label: 'MiMo-V2.6-Distill-Qwen-9B (local en este navegador)',
|
| 13 |
async cleanupCache() {
|
| 14 |
+
// Solo la cach茅 del propio modelo/wllama de ESTE origen; nada m谩s.
|
| 15 |
+
try {
|
| 16 |
+
for (const name of await caches.keys()) {
|
| 17 |
+
const c = await caches.open(name);
|
| 18 |
+
for (const req of await c.keys()) {
|
| 19 |
+
if (/wllama|MiMo-V2\.6/i.test(req.url)) await c.delete(req);
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
} catch (_) {}
|
| 23 |
},
|
| 24 |
async load(onProgress) {
|
| 25 |
if (this.state === 'ready' || this.state === 'downloading') return;
|
|
|
|
| 47 |
throw e;
|
| 48 |
}
|
| 49 |
},
|
| 50 |
+
stop() { this._abort = true; },
|
| 51 |
async chat(messages, { maxTokens = 640, temperature = 0.2, onToken } = {}) {
|
| 52 |
if (this.state !== 'ready') throw new Error('modelo local no cargado');
|
| 53 |
+
this.state = 'generating'; this._abort = false;
|
| 54 |
+
let stream = null;
|
| 55 |
+
try {
|
| 56 |
+
stream = await this.wllama.createChatCompletion({ messages, max_tokens: maxTokens, temperature, stream: true });
|
| 57 |
+
let answer = '';
|
| 58 |
+
for await (const chunk of stream) {
|
| 59 |
+
if (this._abort) break; // salir del bucle cierra el iterador y frena la generaci贸n
|
| 60 |
+
const delta = (chunk.choices && chunk.choices[0] && chunk.choices[0].delta) || {};
|
| 61 |
+
const tk = delta.content || '';
|
| 62 |
+
if (tk) { answer += tk; if (onToken) onToken(answer); }
|
| 63 |
+
}
|
| 64 |
+
return answer;
|
| 65 |
+
} finally {
|
| 66 |
+
this._abort = false;
|
| 67 |
+
if (this.state === 'generating') this.state = 'ready';
|
| 68 |
}
|
|
|
|
| 69 |
},
|
| 70 |
};
|
| 71 |
globalThis.APLocalModel = APLocalModel;
|