@matatbread/matbot-provider-webllm 0.4.21 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/adapter.ts +4 -3
- package/src/index.ts +2 -1
- package/src/webllm.ts +7 -7
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"matbotRuntime": [
|
|
4
4
|
"browser"
|
|
5
5
|
],
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.5.0",
|
|
7
7
|
"engines": {
|
|
8
8
|
"node": ">=24.0.0"
|
|
9
9
|
},
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@matatbread/matbot-plugin-api": "^0.
|
|
41
|
-
"@matatbread/matbot-core": "^0.
|
|
40
|
+
"@matatbread/matbot-plugin-api": "^0.5.0",
|
|
41
|
+
"@matatbread/matbot-core": "^0.5.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"typescript": "^6.0.3",
|
|
45
|
-
"@matatbread/matbot-plugin-api": "^0.
|
|
46
|
-
"@matatbread/matbot-core": "^0.
|
|
45
|
+
"@matatbread/matbot-plugin-api": "^0.5.0",
|
|
46
|
+
"@matatbread/matbot-core": "^0.5.0"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"typecheck": "tsc --noEmit"
|
package/src/adapter.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ProviderAdapter, ProviderConfig, Message, Tool, CompletionEvent, HealthStatus } from '@matatbread/matbot-plugin-api';
|
|
2
|
+
import { withFailure } from '@matatbread/matbot-plugin-api';
|
|
2
3
|
import { ReplyParser, toChatMessages, toolCallFormat, type Piece } from './format.js';
|
|
3
4
|
import { CHARS_PER_TOKEN, fitToWindow, forgetFit, isWindowOverflow, omittedNote } from './window.js';
|
|
4
5
|
import { DEFAULT_LIBRARY, cacheBackend, checkModel, library, loadEngine, offeredModels, type CacheBackend, type ChatRequest, type InitProgressReport, type Loaded } from './webllm.js';
|
|
@@ -102,7 +103,7 @@ export class WebLLMAdapter implements ProviderAdapter {
|
|
|
102
103
|
const kept = fitted.omitted > 0 ? [...fitted.messages, noteMessage(omittedNote(fitted.omitted))] : fitted.messages;
|
|
103
104
|
const chat = toChatMessages(kept, offered);
|
|
104
105
|
if (chat.at(-1)?.role !== 'user')
|
|
105
|
-
throw new Error('webllm: the conversation must end with a user turn or a tool result — WebLLM cannot continue an assistant message.');
|
|
106
|
+
throw withFailure(new Error('webllm: the conversation must end with a user turn or a tool result — WebLLM cannot continue an assistant message.'), 'invalid');
|
|
106
107
|
return {
|
|
107
108
|
omitted: fitted.omitted,
|
|
108
109
|
shrunk: fitted.shrunk,
|
|
@@ -186,10 +187,10 @@ export class WebLLMAdapter implements ProviderAdapter {
|
|
|
186
187
|
continue;
|
|
187
188
|
}
|
|
188
189
|
if (isWindowOverflow(e))
|
|
189
|
-
throw new Error(`webllm: the conversation does not fit the ${contextWindow}-token context window even with its `
|
|
190
|
+
throw withFailure(new Error(`webllm: the conversation does not fit the ${contextWindow}-token context window even with its `
|
|
190
191
|
+ 'earlier turns left out — the system prompt, the tool list and the current turn alone are too long. '
|
|
191
192
|
+ 'Raise `contextWindow` (it costs GPU memory), offer fewer tools (the tool-router plugin, or '
|
|
192
|
-
+ '`parameters.tools: false`), or start a new conversation.', { cause: e });
|
|
193
|
+
+ '`parameters.tools: false`), or start a new conversation.', { cause: e }), 'invalid');
|
|
193
194
|
throw e;
|
|
194
195
|
} finally {
|
|
195
196
|
// What the model was sent and what it wrote, verbatim, at the level DevTools hides unless "Verbose".
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export { WebLLMAdapter, DEFAULT_MODEL } from './adapter.js';
|
|
2
2
|
export { webllmModelsTool, type WebLLMModelList, type WebLLMModelSummary } from './models.js';
|
|
3
3
|
|
|
4
|
+
import { PLUGIN_API_VERSION } from '@matatbread/matbot-plugin-api';
|
|
4
5
|
import type { MatbotPluginSpec } from '@matatbread/matbot-plugin-api';
|
|
5
6
|
import { WebLLMAdapter } from './adapter.js';
|
|
6
7
|
import { webllmModelsTool } from './models.js';
|
|
7
8
|
|
|
8
9
|
export const plugin: MatbotPluginSpec = {
|
|
9
|
-
apiVersion:
|
|
10
|
+
apiVersion: PLUGIN_API_VERSION,
|
|
10
11
|
provider: (_config) => new WebLLMAdapter(),
|
|
11
12
|
tools: [webllmModelsTool],
|
|
12
13
|
};
|
package/src/webllm.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// taken from the package because the package is never installed: the library is several MB, matters only
|
|
3
3
|
// to someone who chooses this provider, and is imported from a CDN at the moment it is first needed.
|
|
4
4
|
|
|
5
|
-
import { closestIds } from '@matatbread/matbot-plugin-api';
|
|
5
|
+
import { closestIds, withFailure } from '@matatbread/matbot-plugin-api';
|
|
6
6
|
|
|
7
7
|
export interface InitProgressReport { progress: number; timeElapsed: number; text: string }
|
|
8
8
|
|
|
@@ -183,22 +183,22 @@ export async function checkModel(lib: WebLLM, model: string, withTools: boolean)
|
|
|
183
183
|
const record = models.find(r => r.model_id === model);
|
|
184
184
|
if (!record) {
|
|
185
185
|
const near = closestModels(model, withTools ? toolModels(lib) : models);
|
|
186
|
-
throw new Error(`webllm: "${model}" is not a model this WebLLM build can run.`
|
|
186
|
+
throw withFailure(new Error(`webllm: "${model}" is not a model this WebLLM build can run.`
|
|
187
187
|
+ (near.length > 0 ? ` Closest: ${near.join(', ')}.` : '')
|
|
188
|
-
+ ' The webllm_models tool lists them.');
|
|
188
|
+
+ ' The webllm_models tool lists them.'), 'invalid');
|
|
189
189
|
}
|
|
190
190
|
if (withTools && !callsTools(model)) {
|
|
191
191
|
const near = closestModels(model, toolModels(lib), 3);
|
|
192
|
-
throw new Error(`webllm: "${model}" is not trained on the tool-call format this provider uses, so it would answer `
|
|
192
|
+
throw withFailure(new Error(`webllm: "${model}" is not trained on the tool-call format this provider uses, so it would answer `
|
|
193
193
|
+ 'instead of calling tools. Use a Qwen3, Qwen2.5 or Hermes model'
|
|
194
194
|
+ (near.length > 0 ? ` (closest: ${near.join(', ')})` : '')
|
|
195
|
-
+ ', or set `parameters.tools: false` to use it for plain chat.');
|
|
195
|
+
+ ', or set `parameters.tools: false` to use it for plain chat.'), 'invalid');
|
|
196
196
|
}
|
|
197
197
|
const have = await gpuFeatures();
|
|
198
198
|
const missing = have ? (record.required_features ?? []).filter(f => !have.has(f)) : [];
|
|
199
199
|
if (missing.length > 0) {
|
|
200
200
|
const f32 = model.replace('q4f16', 'q4f32');
|
|
201
|
-
throw new Error(`webllm: "${model}" needs the GPU feature${missing.length > 1 ? 's' : ''} ${missing.join(', ')}, which this GPU lacks.`
|
|
202
|
-
+ (f32 !== model && models.some(r => r.model_id === f32) ? ` Use "${f32}" instead.` : ''));
|
|
201
|
+
throw withFailure(new Error(`webllm: "${model}" needs the GPU feature${missing.length > 1 ? 's' : ''} ${missing.join(', ')}, which this GPU lacks.`
|
|
202
|
+
+ (f32 !== model && models.some(r => r.model_id === f32) ? ` Use "${f32}" instead.` : '')), 'invalid');
|
|
203
203
|
}
|
|
204
204
|
}
|