@genai-fi/nanogpt 0.17.2 → 0.17.3

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.
@@ -1,6 +1,10 @@
1
1
  import { Conversation, ITokeniser } from './tokeniser/type';
2
2
  import { default as EE } from 'eventemitter3';
3
3
  import { default as Model, ModelForwardAttributes } from './models/model';
4
+ export interface GeneratorConversation extends Conversation {
5
+ _completed?: boolean;
6
+ _timestamp?: number;
7
+ }
4
8
  export interface GenerateOptions {
5
9
  temperature?: number;
6
10
  topK?: number;
package/dist/Generator.js CHANGED
@@ -11701,7 +11701,7 @@ class AS extends Ui {
11701
11701
  if (e) {
11702
11702
  const s = e.length > 0 && e[e.length - 1].role === "assistant";
11703
11703
  let o = [];
11704
- return n?.nonConversational ? s && n?.continuation ? o = t.encode(e[e.length - 1].content) : o = t.encodeAsSequence(e, !0) : o = t.encodeConversation(e, !0), o.length > this.model.config.blockSize && (o = o.slice(-this.model.config.blockSize)), Kt([o], [1, o.length], "int32");
11704
+ return n?.nonConversational ? s && n?.continuation ? o = [t.bosToken, ...t.encode(e[e.length - 1].content)] : o = t.encodeAsSequence(e, !0) : o = t.encodeConversation(e, !0), o.length > this.model.config.blockSize && (o = o.slice(-this.model.config.blockSize)), Kt([o], [1, o.length], "int32");
11705
11705
  } else {
11706
11706
  const s = n?.nonConversational ? void 0 : t.getSpecialTokenIndex("<|assistant_start|>"), o = s !== void 0 ? [t.bosToken, s] : [t.bosToken];
11707
11707
  return Kt([o], [1, o.length], "int32");
@@ -11804,40 +11804,46 @@ class AS extends Ui {
11804
11804
  }
11805
11805
  /** Generate multiple tokens in a loop and produce text */
11806
11806
  async _generate(t) {
11807
- (this.lastToken < 0 || this.outputConversation.length === 0 || this.outputConversation[this.outputConversation.length - 1]._completed || this.outputConversation[this.outputConversation.length - 1].role !== "assistant") && (this.outputConversation.push({ role: "assistant", content: "", _timestamp: Date.now() }), this.resetCache(!t?.noCache));
11808
- let e = this.lastToken >= 0 && this.cache ? Kt([this.lastToken], [1, 1], "int32") : await this.tokenisePrompt(this.actualTokeniser, this.outputConversation.slice(0, -1), t);
11809
- const n = t?.maxLength ?? 1e3;
11810
- for (let s = 0; s < n && this.active; s++) {
11807
+ let e = !1;
11808
+ //this.lastToken < 0 ||
11809
+ this.outputConversation.length === 0 || this.outputConversation[this.outputConversation.length - 1]._completed || this.outputConversation[this.outputConversation.length - 1].role !== "assistant" ? (this.outputConversation.push({ role: "assistant", content: "", _timestamp: Date.now() }), e = !0, this.resetCache(!t?.noCache)) : this.lastToken < 0 && this.resetCache(!t?.noCache);
11810
+ let n = this.lastToken >= 0 && this.cache ? Kt([this.lastToken], [1, 1], "int32") : await this.tokenisePrompt(
11811
+ this.actualTokeniser,
11812
+ e ? this.outputConversation.slice(0, -1) : this.outputConversation,
11813
+ t
11814
+ );
11815
+ const s = t?.maxLength ?? 1e3;
11816
+ for (let o = 0; o < s && this.active; o++) {
11811
11817
  const {
11812
- output: o,
11813
- probabilities: r,
11814
- attention: c,
11815
- loss: i,
11816
- multinomialRand: l
11817
- } = await this._generateToken(e, this.cache ? this.cache : void 0, {
11818
+ output: r,
11819
+ probabilities: c,
11820
+ attention: i,
11821
+ loss: l,
11822
+ multinomialRand: u
11823
+ } = await this._generateToken(n, this.cache ? this.cache : void 0, {
11818
11824
  ...t,
11819
11825
  usePadding: !this.cache
11820
11826
  });
11821
- if (this.lastMultinomialRand = l, i !== void 0 && (this.lastLoss = i), this.cache)
11822
- e.dispose(), e = o;
11827
+ if (this.lastMultinomialRand = u, l !== void 0 && (this.lastLoss = l), this.cache)
11828
+ n.dispose(), n = r;
11823
11829
  else {
11824
- const p = e;
11825
- e = Gd([e, o], 1), p.dispose();
11830
+ const d = n;
11831
+ n = Gd([n, r], 1), d.dispose();
11826
11832
  }
11827
- const u = await this.processResponse(
11833
+ const p = await this.processResponse(
11828
11834
  this.actualTokeniser,
11829
- o,
11835
+ r,
11830
11836
  t?.allowSpecial ?? !1,
11831
- c,
11832
- r
11837
+ i,
11838
+ c
11833
11839
  );
11834
- if (this.cache || o.dispose(), u === null) {
11840
+ if (this.cache || r.dispose(), p === null) {
11835
11841
  this.outputConversation[this.outputConversation.length - 1]._completed = !0;
11836
11842
  break;
11837
11843
  }
11838
- this.outputConversation[this.outputConversation.length - 1].content += u;
11844
+ o === s - 1 && (this.outputConversation[this.outputConversation.length - 1]._completed = !0), this.outputConversation[this.outputConversation.length - 1].content += p;
11839
11845
  }
11840
- return e.dispose(), this.outputConversation;
11846
+ return n.dispose(), this.outputConversation;
11841
11847
  }
11842
11848
  resetCache(t) {
11843
11849
  this.cache && (this.cache.forEach((e) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",