bitgpu 0.16.0 → 0.18.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/README.md +35 -11
- package/dist/chat.d.ts +95 -4
- package/dist/chat.js +409 -17
- package/dist/chat.js.map +1 -1
- package/dist/gguf.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +178 -46
- package/dist/index.js.map +1 -1
- package/dist/{types-Wg8VrdrL.d.ts → types-CSpyQjyc.d.ts} +32 -2
- package/package.json +1 -1
package/dist/chat.js
CHANGED
|
@@ -4308,11 +4308,12 @@ function holdback$1(s, tag) {
|
|
|
4308
4308
|
var ThinkSplitter = class {
|
|
4309
4309
|
open;
|
|
4310
4310
|
close;
|
|
4311
|
-
inside
|
|
4311
|
+
inside;
|
|
4312
4312
|
hold = "";
|
|
4313
|
-
constructor(open = "<think>", close = "</think>") {
|
|
4313
|
+
constructor(open = "<think>", close = "</think>", startInside = false) {
|
|
4314
4314
|
this.open = open;
|
|
4315
4315
|
this.close = close;
|
|
4316
|
+
this.inside = startInside;
|
|
4316
4317
|
}
|
|
4317
4318
|
push(chunk) {
|
|
4318
4319
|
let s = this.hold + chunk;
|
|
@@ -4398,6 +4399,43 @@ var StopScanner = class {
|
|
|
4398
4399
|
return r;
|
|
4399
4400
|
}
|
|
4400
4401
|
};
|
|
4402
|
+
/** Budget-forcing for the think channel (s1-style "budget forcing", training-free): counts the
|
|
4403
|
+
* tokens generated inside a <think> block; once `budget` is spent, `force()` names `</think>` as
|
|
4404
|
+
* the only permitted candidate (the engine's constrained pick guarantees it is reachable even
|
|
4405
|
+
* when outside the top-K), so the model closes its reasoning and continues straight into the
|
|
4406
|
+
* visible answer. `budget: 0` suppresses reasoning entirely while keeping the thinking-mode
|
|
4407
|
+
* template. advance() must see every emitted token. */
|
|
4408
|
+
var ThinkBudget = class {
|
|
4409
|
+
openId;
|
|
4410
|
+
closeId;
|
|
4411
|
+
budget;
|
|
4412
|
+
inThink;
|
|
4413
|
+
spent = 0;
|
|
4414
|
+
closed = false;
|
|
4415
|
+
constructor(openId, closeId, budget, startInside) {
|
|
4416
|
+
this.openId = openId;
|
|
4417
|
+
this.closeId = closeId;
|
|
4418
|
+
this.budget = budget;
|
|
4419
|
+
this.inThink = startInside;
|
|
4420
|
+
}
|
|
4421
|
+
advance(id) {
|
|
4422
|
+
if (this.closed) return;
|
|
4423
|
+
if (!this.inThink) {
|
|
4424
|
+
if (this.openId != null && id === this.openId) this.inThink = true;
|
|
4425
|
+
return;
|
|
4426
|
+
}
|
|
4427
|
+
if (this.closeId != null && id === this.closeId) {
|
|
4428
|
+
this.inThink = false;
|
|
4429
|
+
this.closed = true;
|
|
4430
|
+
return;
|
|
4431
|
+
}
|
|
4432
|
+
this.spent++;
|
|
4433
|
+
}
|
|
4434
|
+
/** The forced token id once the budget is exhausted (only `</think>` may be emitted), else null. */
|
|
4435
|
+
force() {
|
|
4436
|
+
return this.inThink && !this.closed && this.spent >= this.budget && this.closeId != null ? this.closeId : null;
|
|
4437
|
+
}
|
|
4438
|
+
};
|
|
4401
4439
|
//#endregion
|
|
4402
4440
|
//#region src/chat/json.ts
|
|
4403
4441
|
const SUPPORTED = /* @__PURE__ */ new Set([
|
|
@@ -4757,10 +4795,13 @@ var JsonMachine = class JsonMachine {
|
|
|
4757
4795
|
byte(b) {
|
|
4758
4796
|
if (!WS$1.has(b)) this.wsRun = 0;
|
|
4759
4797
|
switch (this.phase) {
|
|
4760
|
-
case 0:
|
|
4798
|
+
case 0: {
|
|
4761
4799
|
if (WS$1.has(b)) return this.ws();
|
|
4762
4800
|
if (b === 123 || b === 91) return this.openValue(b);
|
|
4801
|
+
const rt = this.root?.type;
|
|
4802
|
+
if (rt !== void 0 && rt !== "object" && rt !== "array") return this.openValue(b);
|
|
4763
4803
|
return false;
|
|
4804
|
+
}
|
|
4764
4805
|
case 1:
|
|
4765
4806
|
if (WS$1.has(b)) return this.ws();
|
|
4766
4807
|
return this.openValue(b);
|
|
@@ -5181,18 +5222,230 @@ var ToolBodyMachine = class ToolBodyMachine {
|
|
|
5181
5222
|
}
|
|
5182
5223
|
}
|
|
5183
5224
|
};
|
|
5225
|
+
const X_FUNC = utf8.encode("<function=");
|
|
5226
|
+
const X_PARAM = utf8.encode("<parameter=");
|
|
5227
|
+
const X_FCLOSE = utf8.encode("</function>");
|
|
5228
|
+
const X_PCLOSE = utf8.encode("</parameter>");
|
|
5229
|
+
const litPrefix = (buf, lit) => {
|
|
5230
|
+
if (buf.length > lit.length) return false;
|
|
5231
|
+
for (let i = 0; i < buf.length; i++) if (buf[i] !== lit[i]) return false;
|
|
5232
|
+
return true;
|
|
5233
|
+
};
|
|
5234
|
+
const litEq = (buf, lit) => buf.length === lit.length && litPrefix(buf, lit);
|
|
5235
|
+
var ToolBodyMachineXml = class ToolBodyMachineXml {
|
|
5236
|
+
cands;
|
|
5237
|
+
props;
|
|
5238
|
+
phase = 0;
|
|
5239
|
+
lit = 0;
|
|
5240
|
+
nameBuf = "";
|
|
5241
|
+
wsRun = 0;
|
|
5242
|
+
elem = [];
|
|
5243
|
+
keyBuf = "";
|
|
5244
|
+
keyCands = [];
|
|
5245
|
+
reqLeft = [];
|
|
5246
|
+
valTail = [];
|
|
5247
|
+
valStarted = false;
|
|
5248
|
+
valMode = "raw";
|
|
5249
|
+
valBuf = "";
|
|
5250
|
+
valEnum = [];
|
|
5251
|
+
valJson = null;
|
|
5252
|
+
closeLit = 0;
|
|
5253
|
+
complete = false;
|
|
5254
|
+
name = "";
|
|
5255
|
+
/** byte-space tool names, and per name its property keys / required subset / param schemas */
|
|
5256
|
+
constructor(cands, props) {
|
|
5257
|
+
this.cands = cands;
|
|
5258
|
+
this.props = props;
|
|
5259
|
+
}
|
|
5260
|
+
clone() {
|
|
5261
|
+
const m = new ToolBodyMachineXml(this.cands, this.props);
|
|
5262
|
+
m.phase = this.phase;
|
|
5263
|
+
m.lit = this.lit;
|
|
5264
|
+
m.nameBuf = this.nameBuf;
|
|
5265
|
+
m.wsRun = this.wsRun;
|
|
5266
|
+
m.elem = [...this.elem];
|
|
5267
|
+
m.keyBuf = this.keyBuf;
|
|
5268
|
+
m.keyCands = [...this.keyCands];
|
|
5269
|
+
m.reqLeft = [...this.reqLeft];
|
|
5270
|
+
m.valTail = [...this.valTail];
|
|
5271
|
+
m.valStarted = this.valStarted;
|
|
5272
|
+
m.complete = this.complete;
|
|
5273
|
+
m.name = this.name;
|
|
5274
|
+
m.valMode = this.valMode;
|
|
5275
|
+
m.valBuf = this.valBuf;
|
|
5276
|
+
m.valEnum = [...this.valEnum];
|
|
5277
|
+
m.valJson = this.valJson ? this.valJson.clone() : null;
|
|
5278
|
+
m.closeLit = this.closeLit;
|
|
5279
|
+
return m;
|
|
5280
|
+
}
|
|
5281
|
+
feed(bytes) {
|
|
5282
|
+
for (let i = 0; i < bytes.length; i++) if (!this.byte(bytes[i])) return false;
|
|
5283
|
+
return true;
|
|
5284
|
+
}
|
|
5285
|
+
forcedNext() {
|
|
5286
|
+
switch (this.phase) {
|
|
5287
|
+
case 2: return this.cands.includes(this.nameBuf) && !this.cands.some((c) => c !== this.nameBuf && c.startsWith(this.nameBuf)) ? 62 : -1;
|
|
5288
|
+
case 4: return this.keyCands.length > 0 && this.keyCands.includes(this.keyBuf) && !this.keyCands.some((c) => c !== this.keyBuf && c.startsWith(this.keyBuf)) ? 62 : -1;
|
|
5289
|
+
case 3: return this.elem.length === 0 && this.wsRun === 0 ? 10 : -1;
|
|
5290
|
+
case 5:
|
|
5291
|
+
if (this.wsRun === 0) return 10;
|
|
5292
|
+
if (this.valMode === "enum" && this.valEnum.includes(this.valBuf) && !this.valEnum.some((c) => c !== this.valBuf && c.startsWith(this.valBuf))) return 10;
|
|
5293
|
+
return -1;
|
|
5294
|
+
default: return -1;
|
|
5295
|
+
}
|
|
5296
|
+
}
|
|
5297
|
+
byte(b) {
|
|
5298
|
+
switch (this.phase) {
|
|
5299
|
+
case 0:
|
|
5300
|
+
if (WS.has(b)) return ++this.wsRun <= WS_CAP;
|
|
5301
|
+
if (b !== X_FUNC[0]) return false;
|
|
5302
|
+
this.phase = 1;
|
|
5303
|
+
this.lit = 1;
|
|
5304
|
+
return true;
|
|
5305
|
+
case 1:
|
|
5306
|
+
if (b !== X_FUNC[this.lit]) return false;
|
|
5307
|
+
if (++this.lit === X_FUNC.length) {
|
|
5308
|
+
this.phase = 2;
|
|
5309
|
+
this.nameBuf = "";
|
|
5310
|
+
}
|
|
5311
|
+
return true;
|
|
5312
|
+
case 2:
|
|
5313
|
+
if (b === 62) {
|
|
5314
|
+
if (!this.cands.includes(this.nameBuf)) return false;
|
|
5315
|
+
this.name = this.nameBuf;
|
|
5316
|
+
const spec = this.props.get(this.nameBuf);
|
|
5317
|
+
this.keyCands = [...spec?.keys ?? []];
|
|
5318
|
+
this.reqLeft = [...spec?.required ?? []];
|
|
5319
|
+
this.phase = 3;
|
|
5320
|
+
this.elem = [];
|
|
5321
|
+
this.wsRun = 0;
|
|
5322
|
+
return true;
|
|
5323
|
+
}
|
|
5324
|
+
this.nameBuf += String.fromCharCode(b);
|
|
5325
|
+
for (const c of this.cands) if (c.startsWith(this.nameBuf)) return true;
|
|
5326
|
+
return false;
|
|
5327
|
+
case 3: {
|
|
5328
|
+
if (this.elem.length === 0 && this.wsRun === 0) {
|
|
5329
|
+
if (b !== 10) return false;
|
|
5330
|
+
this.wsRun = 1;
|
|
5331
|
+
return true;
|
|
5332
|
+
}
|
|
5333
|
+
this.elem.push(b);
|
|
5334
|
+
if (litEq(this.elem, X_PARAM)) {
|
|
5335
|
+
this.phase = 4;
|
|
5336
|
+
this.keyBuf = "";
|
|
5337
|
+
return true;
|
|
5338
|
+
}
|
|
5339
|
+
if (litEq(this.elem, X_FCLOSE)) {
|
|
5340
|
+
if (this.reqLeft.length) return false;
|
|
5341
|
+
this.phase = 7;
|
|
5342
|
+
this.complete = true;
|
|
5343
|
+
this.wsRun = 0;
|
|
5344
|
+
return true;
|
|
5345
|
+
}
|
|
5346
|
+
const pP = litPrefix(this.elem, X_PARAM);
|
|
5347
|
+
const pF = litPrefix(this.elem, X_FCLOSE);
|
|
5348
|
+
if (pF && !pP && this.reqLeft.length) return false;
|
|
5349
|
+
return pP || pF;
|
|
5350
|
+
}
|
|
5351
|
+
case 4:
|
|
5352
|
+
if (b === 62) {
|
|
5353
|
+
if (this.keyCands.length && !this.keyCands.includes(this.keyBuf)) return false;
|
|
5354
|
+
this.keyCands = this.keyCands.filter((k) => k !== this.keyBuf);
|
|
5355
|
+
this.reqLeft = this.reqLeft.filter((k) => k !== this.keyBuf);
|
|
5356
|
+
this.phase = 5;
|
|
5357
|
+
this.valTail = [];
|
|
5358
|
+
this.wsRun = 0;
|
|
5359
|
+
this.valStarted = false;
|
|
5360
|
+
const sch = this.props.get(this.name)?.schemas?.get(this.keyBuf);
|
|
5361
|
+
const t = sch?.type;
|
|
5362
|
+
if (sch && t !== void 0 && t !== "string") {
|
|
5363
|
+
this.valMode = "json";
|
|
5364
|
+
this.valJson = new JsonMachine(sch);
|
|
5365
|
+
} else if (sch?.enum?.length) {
|
|
5366
|
+
this.valMode = "enum";
|
|
5367
|
+
this.valBuf = "";
|
|
5368
|
+
this.valEnum = sch.enum.map(ToolBodyMachine.bytesOf);
|
|
5369
|
+
} else this.valMode = "raw";
|
|
5370
|
+
return true;
|
|
5371
|
+
}
|
|
5372
|
+
this.keyBuf += String.fromCharCode(b);
|
|
5373
|
+
if (!this.keyCands.length) return true;
|
|
5374
|
+
for (const c of this.keyCands) if (c.startsWith(this.keyBuf)) return true;
|
|
5375
|
+
return false;
|
|
5376
|
+
case 5:
|
|
5377
|
+
if (this.wsRun === 0) {
|
|
5378
|
+
if (b !== 10) return false;
|
|
5379
|
+
this.wsRun = 1;
|
|
5380
|
+
return true;
|
|
5381
|
+
}
|
|
5382
|
+
if (!this.valStarted) {
|
|
5383
|
+
if (b === 10) return false;
|
|
5384
|
+
this.valStarted = true;
|
|
5385
|
+
}
|
|
5386
|
+
if (this.valMode === "json") {
|
|
5387
|
+
const j = this.valJson;
|
|
5388
|
+
if (j.feed(Uint8Array.of(b))) return true;
|
|
5389
|
+
if (!j.complete || b !== X_PCLOSE[0]) return false;
|
|
5390
|
+
this.phase = 6;
|
|
5391
|
+
this.closeLit = 1;
|
|
5392
|
+
return true;
|
|
5393
|
+
}
|
|
5394
|
+
if (this.valMode === "enum") {
|
|
5395
|
+
const next = this.valBuf + String.fromCharCode(b);
|
|
5396
|
+
if (this.valEnum.some((c) => c.startsWith(next))) {
|
|
5397
|
+
this.valBuf = next;
|
|
5398
|
+
return true;
|
|
5399
|
+
}
|
|
5400
|
+
if (b === 10 && this.valEnum.includes(this.valBuf)) {
|
|
5401
|
+
this.phase = 6;
|
|
5402
|
+
this.closeLit = 0;
|
|
5403
|
+
return true;
|
|
5404
|
+
}
|
|
5405
|
+
return false;
|
|
5406
|
+
}
|
|
5407
|
+
this.valTail.push(b);
|
|
5408
|
+
if (this.valTail.length > X_PCLOSE.length) this.valTail.shift();
|
|
5409
|
+
if (litEq(this.valTail, X_PCLOSE)) {
|
|
5410
|
+
this.phase = 3;
|
|
5411
|
+
this.elem = [];
|
|
5412
|
+
this.wsRun = 0;
|
|
5413
|
+
}
|
|
5414
|
+
return true;
|
|
5415
|
+
case 6:
|
|
5416
|
+
if (b !== X_PCLOSE[this.closeLit]) return false;
|
|
5417
|
+
if (++this.closeLit === X_PCLOSE.length) {
|
|
5418
|
+
this.phase = 3;
|
|
5419
|
+
this.elem = [];
|
|
5420
|
+
this.wsRun = 0;
|
|
5421
|
+
}
|
|
5422
|
+
return true;
|
|
5423
|
+
case 7: return WS.has(b) && ++this.wsRun <= WS_CAP;
|
|
5424
|
+
}
|
|
5425
|
+
}
|
|
5426
|
+
};
|
|
5184
5427
|
/** Per-step candidate filter for tool turns. Auto mode: free text (everything permitted) until
|
|
5185
5428
|
* the model opens <tool_call>, then the body grammar takes over until </tool_call>, then free
|
|
5186
5429
|
* text again (another call, prose, or eos). Forced mode: the FIRST token must be <tool_call>,
|
|
5187
5430
|
* the body is constrained to the named tool, and after </tool_call> only eos is permitted.
|
|
5188
5431
|
* Call advance() with each emitted token to move the real machine. */
|
|
5189
|
-
function makeToolFilter(table, prep) {
|
|
5432
|
+
function makeToolFilter(table, prep, startInThink = false) {
|
|
5190
5433
|
const { ids, forced } = prep;
|
|
5191
5434
|
const cands = (forced ? prep.tools.filter((t) => t.function.name === forced) : prep.tools).map((t) => t.function.name).map(ToolBodyMachine.bytesOf);
|
|
5192
5435
|
const schemas = new Map(prep.tools.map((t) => [ToolBodyMachine.bytesOf(t.function.name), argsSchemaOf(t)]));
|
|
5436
|
+
const props = new Map(prep.tools.map((t) => {
|
|
5437
|
+
const p = t.function.parameters;
|
|
5438
|
+
const schemas = new Map(Object.entries(p?.properties ?? {}).map(([k, v]) => [ToolBodyMachine.bytesOf(k), v]));
|
|
5439
|
+
return [ToolBodyMachine.bytesOf(t.function.name), {
|
|
5440
|
+
keys: [...schemas.keys()],
|
|
5441
|
+
required: (p?.required ?? []).map(ToolBodyMachine.bytesOf),
|
|
5442
|
+
schemas
|
|
5443
|
+
}];
|
|
5444
|
+
}));
|
|
5445
|
+
const newBody = () => prep.format === "xml" ? new ToolBodyMachineXml(cands, props) : new ToolBodyMachine(cands, schemas);
|
|
5193
5446
|
let state = forced ? 1 : 0;
|
|
5194
5447
|
let body = null;
|
|
5195
|
-
let inThink =
|
|
5448
|
+
let inThink = startInThink;
|
|
5196
5449
|
return {
|
|
5197
5450
|
filter: (candidates) => {
|
|
5198
5451
|
if (state === 0 || inThink) return Array.from(candidates, Number);
|
|
@@ -5214,6 +5467,11 @@ function makeToolFilter(table, prep) {
|
|
|
5214
5467
|
}
|
|
5215
5468
|
const bytes = table.bytes(n);
|
|
5216
5469
|
if (!bytes || bytes.length === 0) continue;
|
|
5470
|
+
const fb = m.forcedNext?.() ?? -1;
|
|
5471
|
+
if (fb >= 0) {
|
|
5472
|
+
if (bytes.length === 1 && bytes[0] === fb) out.push(n);
|
|
5473
|
+
continue;
|
|
5474
|
+
}
|
|
5217
5475
|
if (m.clone().feed(bytes)) out.push(n);
|
|
5218
5476
|
}
|
|
5219
5477
|
return out;
|
|
@@ -5233,7 +5491,7 @@ function makeToolFilter(table, prep) {
|
|
|
5233
5491
|
case 1:
|
|
5234
5492
|
if (id === ids.open) {
|
|
5235
5493
|
state = 2;
|
|
5236
|
-
body =
|
|
5494
|
+
body = newBody();
|
|
5237
5495
|
}
|
|
5238
5496
|
return;
|
|
5239
5497
|
case 2:
|
|
@@ -5346,6 +5604,63 @@ function parseToolCall(raw) {
|
|
|
5346
5604
|
raw
|
|
5347
5605
|
};
|
|
5348
5606
|
}
|
|
5607
|
+
/** Parse one XML block's content into a ToolCall (the Qwen3.5 `<function=…><parameter=…>` protocol).
|
|
5608
|
+
* Values are coerced by the tool's schema - string params keep their raw text, everything else is
|
|
5609
|
+
* JSON.parse'd. Returns name '' if the block names an unknown tool/property, a required property is
|
|
5610
|
+
* missing, or a non-string value is not valid JSON (so a surfaced call always conforms). */
|
|
5611
|
+
function parseToolCallXml(raw, tools) {
|
|
5612
|
+
const fn = /<function=([^>]*)>/.exec(raw);
|
|
5613
|
+
if (!fn) return {
|
|
5614
|
+
name: "",
|
|
5615
|
+
arguments: {},
|
|
5616
|
+
raw
|
|
5617
|
+
};
|
|
5618
|
+
const name = fn[1];
|
|
5619
|
+
const tool = tools.find((t) => t.function.name === name);
|
|
5620
|
+
if (!tool) return {
|
|
5621
|
+
name: "",
|
|
5622
|
+
arguments: {},
|
|
5623
|
+
raw
|
|
5624
|
+
};
|
|
5625
|
+
const schema = tool.function.parameters;
|
|
5626
|
+
const props = schema?.properties ?? {};
|
|
5627
|
+
const args = {};
|
|
5628
|
+
const re = /<parameter=([^>]*)>([\s\S]*?)<\/parameter>/g;
|
|
5629
|
+
for (let m = re.exec(raw); m !== null; m = re.exec(raw)) {
|
|
5630
|
+
const key = m[1];
|
|
5631
|
+
const valText = m[2].replace(/^\n/, "").replace(/\n$/, "");
|
|
5632
|
+
const pschema = props[key];
|
|
5633
|
+
if (pschema?.type !== void 0 && pschema.type !== "string") try {
|
|
5634
|
+
args[key] = JSON.parse(valText);
|
|
5635
|
+
} catch {
|
|
5636
|
+
return {
|
|
5637
|
+
name: "",
|
|
5638
|
+
arguments: {},
|
|
5639
|
+
raw
|
|
5640
|
+
};
|
|
5641
|
+
}
|
|
5642
|
+
else args[key] = valText;
|
|
5643
|
+
}
|
|
5644
|
+
if (schema) {
|
|
5645
|
+
for (const req of schema.required ?? []) if (!(req in args)) return {
|
|
5646
|
+
name: "",
|
|
5647
|
+
arguments: {},
|
|
5648
|
+
raw
|
|
5649
|
+
};
|
|
5650
|
+
if (schema.additionalProperties === false) {
|
|
5651
|
+
for (const k of Object.keys(args)) if (!(k in props)) return {
|
|
5652
|
+
name: "",
|
|
5653
|
+
arguments: {},
|
|
5654
|
+
raw
|
|
5655
|
+
};
|
|
5656
|
+
}
|
|
5657
|
+
}
|
|
5658
|
+
return {
|
|
5659
|
+
name,
|
|
5660
|
+
arguments: args,
|
|
5661
|
+
raw
|
|
5662
|
+
};
|
|
5663
|
+
}
|
|
5349
5664
|
//#endregion
|
|
5350
5665
|
//#region src/chat/index.ts
|
|
5351
5666
|
function deriveChatWrap(tk) {
|
|
@@ -5422,6 +5737,21 @@ async function createChat(engine, options) {
|
|
|
5422
5737
|
tk = new ChatTokenizer(json, cfg);
|
|
5423
5738
|
}
|
|
5424
5739
|
const wrap = deriveChatWrap(tk);
|
|
5740
|
+
const thinkPreopened = (() => {
|
|
5741
|
+
if (!tk.hasChatTemplate) return false;
|
|
5742
|
+
try {
|
|
5743
|
+
const r = tk.applyChatTemplate([{
|
|
5744
|
+
role: "user",
|
|
5745
|
+
content: "x"
|
|
5746
|
+
}], {
|
|
5747
|
+
addGenerationPrompt: true,
|
|
5748
|
+
enableThinking: true
|
|
5749
|
+
});
|
|
5750
|
+
return r.lastIndexOf("<think>") > r.lastIndexOf("</think>");
|
|
5751
|
+
} catch {
|
|
5752
|
+
return false;
|
|
5753
|
+
}
|
|
5754
|
+
})();
|
|
5425
5755
|
let committed = null;
|
|
5426
5756
|
let cacheEndsAtEos = false;
|
|
5427
5757
|
let committedToolsKey = null;
|
|
@@ -5434,6 +5764,7 @@ async function createChat(engine, options) {
|
|
|
5434
5764
|
engine.resetCache();
|
|
5435
5765
|
};
|
|
5436
5766
|
let templateToolsOk = null;
|
|
5767
|
+
let toolFormat = "json";
|
|
5437
5768
|
function prepareTools(tools, choice) {
|
|
5438
5769
|
validateTools(tools, choice);
|
|
5439
5770
|
if (templateToolsOk === null) try {
|
|
@@ -5448,6 +5779,7 @@ async function createChat(engine, options) {
|
|
|
5448
5779
|
}]
|
|
5449
5780
|
});
|
|
5450
5781
|
templateToolsOk = probe.includes("bitgpu_probe_tool") && probe.includes("<tool_call>");
|
|
5782
|
+
toolFormat = probe.includes("<function=") ? "xml" : "json";
|
|
5451
5783
|
} catch {
|
|
5452
5784
|
templateToolsOk = false;
|
|
5453
5785
|
}
|
|
@@ -5458,6 +5790,7 @@ async function createChat(engine, options) {
|
|
|
5458
5790
|
return {
|
|
5459
5791
|
tools,
|
|
5460
5792
|
forced: typeof choice === "object" ? choice.name : null,
|
|
5793
|
+
format: toolFormat,
|
|
5461
5794
|
ids: {
|
|
5462
5795
|
open,
|
|
5463
5796
|
close,
|
|
@@ -5476,6 +5809,27 @@ async function createChat(engine, options) {
|
|
|
5476
5809
|
};
|
|
5477
5810
|
};
|
|
5478
5811
|
let byteTable = null;
|
|
5812
|
+
let dryBreakerIds = null;
|
|
5813
|
+
const defaultDryBreakers = () => {
|
|
5814
|
+
if (dryBreakerIds) return dryBreakerIds;
|
|
5815
|
+
const ids = /* @__PURE__ */ new Set();
|
|
5816
|
+
for (const t of [
|
|
5817
|
+
"\n",
|
|
5818
|
+
"\n\n",
|
|
5819
|
+
":",
|
|
5820
|
+
";",
|
|
5821
|
+
",",
|
|
5822
|
+
"\"",
|
|
5823
|
+
"'",
|
|
5824
|
+
"*",
|
|
5825
|
+
"-",
|
|
5826
|
+
"|",
|
|
5827
|
+
"#",
|
|
5828
|
+
".",
|
|
5829
|
+
".\n"
|
|
5830
|
+
]) for (const id of tk.encode(t)) ids.add(id);
|
|
5831
|
+
return dryBreakerIds = [...ids];
|
|
5832
|
+
};
|
|
5479
5833
|
async function sendImpl(messages, o = {}) {
|
|
5480
5834
|
if (messages.length === 0) throw new Error("bitgpu/chat: no messages");
|
|
5481
5835
|
const json = o.format !== void 0;
|
|
@@ -5489,7 +5843,7 @@ async function createChat(engine, options) {
|
|
|
5489
5843
|
const wantReuse = (o.reuseCache ?? true) && !think && wrap !== null && toolsKey === committedToolsKey;
|
|
5490
5844
|
const userAppend = wantReuse && isCleanAppend(committed, messages);
|
|
5491
5845
|
const toolAppend = wantReuse && !userAppend && isToolAppend(committed, messages);
|
|
5492
|
-
|
|
5846
|
+
let canReuse = userAppend || toolAppend;
|
|
5493
5847
|
let inputTokenIds;
|
|
5494
5848
|
if (userAppend) {
|
|
5495
5849
|
const w = wrap;
|
|
@@ -5498,11 +5852,36 @@ async function createChat(engine, options) {
|
|
|
5498
5852
|
inputTokenIds = tk.encode(deltaStr, false);
|
|
5499
5853
|
} else if (toolAppend) {
|
|
5500
5854
|
const toolMsgs = messages.slice(committed.length);
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5855
|
+
let deltaStr = null;
|
|
5856
|
+
try {
|
|
5857
|
+
deltaStr = `${cacheEndsAtEos ? "" : tk.eosToken}\n` + tk.applyChatTemplate(toolMsgs, {
|
|
5858
|
+
addGenerationPrompt: true,
|
|
5859
|
+
enableThinking: false
|
|
5860
|
+
});
|
|
5861
|
+
} catch {
|
|
5862
|
+
const tools = prep?.tools;
|
|
5863
|
+
const fullNew = tk.applyChatTemplate(messages, {
|
|
5864
|
+
addGenerationPrompt: true,
|
|
5865
|
+
enableThinking: false,
|
|
5866
|
+
tools
|
|
5867
|
+
});
|
|
5868
|
+
const committedRender = tk.applyChatTemplate(committed, {
|
|
5869
|
+
addGenerationPrompt: false,
|
|
5870
|
+
enableThinking: false,
|
|
5871
|
+
tools
|
|
5872
|
+
}).replace(/\n$/, "");
|
|
5873
|
+
if (fullNew.startsWith(committedRender)) deltaStr = `${cacheEndsAtEos ? "" : tk.eosToken}` + fullNew.slice(committedRender.length);
|
|
5874
|
+
}
|
|
5875
|
+
if (deltaStr !== null) inputTokenIds = tk.encode(deltaStr, false);
|
|
5876
|
+
else {
|
|
5877
|
+
dropCache();
|
|
5878
|
+
canReuse = false;
|
|
5879
|
+
inputTokenIds = tk.encode(tk.applyChatTemplate(messages, {
|
|
5880
|
+
addGenerationPrompt: true,
|
|
5881
|
+
enableThinking: think,
|
|
5882
|
+
tools: prep?.tools
|
|
5883
|
+
}), false);
|
|
5884
|
+
}
|
|
5506
5885
|
} else {
|
|
5507
5886
|
dropCache();
|
|
5508
5887
|
inputTokenIds = tk.encode(tk.applyChatTemplate(messages, {
|
|
@@ -5512,7 +5891,7 @@ async function createChat(engine, options) {
|
|
|
5512
5891
|
}), false);
|
|
5513
5892
|
}
|
|
5514
5893
|
const decoder = tk.createDecoderStream(true);
|
|
5515
|
-
const splitter = new ThinkSplitter();
|
|
5894
|
+
const splitter = new ThinkSplitter("<think>", "</think>", think && thinkPreopened);
|
|
5516
5895
|
const stops = o.stopSequences?.length ? new StopScanner(o.stopSequences) : null;
|
|
5517
5896
|
const stopCtl = stops ? new AbortController() : null;
|
|
5518
5897
|
const signal = stopCtl ? o.signal ? AbortSignal.any([o.signal, stopCtl.signal]) : stopCtl.signal : o.signal;
|
|
@@ -5521,7 +5900,7 @@ async function createChat(engine, options) {
|
|
|
5521
5900
|
const toolSplit = prep ? new ToolCallSplitter() : null;
|
|
5522
5901
|
const toolCalls = [];
|
|
5523
5902
|
const pushBlock = (block) => {
|
|
5524
|
-
const call = parseToolCall(block);
|
|
5903
|
+
const call = prep?.format === "xml" ? parseToolCallXml(block, prep.tools) : parseToolCall(block);
|
|
5525
5904
|
toolCalls.push(call);
|
|
5526
5905
|
if (call.name) o.onToolCall?.(call);
|
|
5527
5906
|
};
|
|
@@ -5548,7 +5927,8 @@ async function createChat(engine, options) {
|
|
|
5548
5927
|
const maxTokens = o.maxTokens ?? (think ? 1024 : 512);
|
|
5549
5928
|
const epoch0 = resetEpoch;
|
|
5550
5929
|
const jf = json ? makeJsonFilter(byteTable ??= new TokenByteTable(tk), tk.eosTokenId, schema) : null;
|
|
5551
|
-
const tf = prep ? makeToolFilter(byteTable ??= new TokenByteTable(tk), prep) : null;
|
|
5930
|
+
const tf = prep ? makeToolFilter(byteTable ??= new TokenByteTable(tk), prep, think && thinkPreopened) : null;
|
|
5931
|
+
const tb = think && o.thinkBudget != null && tk.tokenToId("</think>") != null ? new ThinkBudget(tk.tokenToId("<think>"), tk.tokenToId("</think>"), Math.max(0, o.thinkBudget), thinkPreopened) : null;
|
|
5552
5932
|
let result;
|
|
5553
5933
|
try {
|
|
5554
5934
|
result = await engine.generate(inputTokenIds, {
|
|
@@ -5556,7 +5936,14 @@ async function createChat(engine, options) {
|
|
|
5556
5936
|
temperature: o.temperature,
|
|
5557
5937
|
topK: o.topK,
|
|
5558
5938
|
topP: o.topP,
|
|
5939
|
+
minP: o.minP,
|
|
5559
5940
|
repetitionPenalty: o.repetitionPenalty,
|
|
5941
|
+
presencePenalty: o.presencePenalty,
|
|
5942
|
+
dryMultiplier: o.dryMultiplier,
|
|
5943
|
+
dryBase: o.dryBase,
|
|
5944
|
+
dryAllowedLength: o.dryAllowedLength,
|
|
5945
|
+
dryRange: o.dryRange,
|
|
5946
|
+
dryBreakers: (o.dryMultiplier ?? 0) > 0 ? o.dryBreakers ?? defaultDryBreakers() : void 0,
|
|
5560
5947
|
noRepeatNgramSize: o.noRepeatNgramSize,
|
|
5561
5948
|
seed: o.seed,
|
|
5562
5949
|
logprobs: o.logprobs,
|
|
@@ -5564,8 +5951,13 @@ async function createChat(engine, options) {
|
|
|
5564
5951
|
stopTokens: [tk.eosTokenId, ...o.stopTokens ?? []],
|
|
5565
5952
|
reuseCache: canReuse,
|
|
5566
5953
|
signal,
|
|
5567
|
-
candidateFilter: jf
|
|
5954
|
+
candidateFilter: jf || tf || tb ? (ids) => {
|
|
5955
|
+
const forced = tb?.force();
|
|
5956
|
+
if (forced != null) return [forced];
|
|
5957
|
+
return jf ? jf.filter(ids) : tf ? tf.filter(ids) : Array.from(ids);
|
|
5958
|
+
} : void 0,
|
|
5568
5959
|
onToken: (id) => {
|
|
5960
|
+
tb?.advance(id);
|
|
5569
5961
|
jf?.advance(id);
|
|
5570
5962
|
tf?.advance(id);
|
|
5571
5963
|
emit(splitter.push(decoder.push(id)));
|
|
@@ -5727,6 +6119,6 @@ async function createChat(engine, options) {
|
|
|
5727
6119
|
};
|
|
5728
6120
|
}
|
|
5729
6121
|
//#endregion
|
|
5730
|
-
export { ChatTokenizer, JsonMachine, StopScanner, ThinkSplitter, ToolBodyMachine, ToolCallSplitter, createChat, makeToolFilter, parseToolCall, validateJsonSchema, validateTools };
|
|
6122
|
+
export { ChatTokenizer, JsonMachine, StopScanner, ThinkBudget, ThinkSplitter, ToolBodyMachine, ToolBodyMachineXml, ToolCallSplitter, createChat, makeToolFilter, parseToolCall, parseToolCallXml, validateJsonSchema, validateTools };
|
|
5731
6123
|
|
|
5732
6124
|
//# sourceMappingURL=chat.js.map
|