@genai-fi/nanogpt 1.3.0 → 1.4.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.
@@ -29,13 +29,10 @@ var r = class {
29
29
  }
30
30
  async _runForRecord(e, t, n) {
31
31
  let r = t.previous_response_id ? this._responses.get(t.previous_response_id) : void 0;
32
- if (r) {
33
- let e = r.generator.getConversation();
34
- t.input && Array.isArray(t.input) ? t.input = [...e, ...t.input] : t.input && typeof t.input == "string" ? t.input = [...e, {
35
- role: t.nonConversational ? "text" : "user",
36
- content: t.input
37
- }] : t.input = e;
38
- }
32
+ r && (t.input && Array.isArray(t.input) || t.input && typeof t.input == "string" && (t.input = [{
33
+ role: t.nonConversational ? "text" : "user",
34
+ content: t.input
35
+ }]));
39
36
  let i = r ? r.generator : this._responses.get(e).generator, a = n ? () => n({
40
37
  output: i.getConversation(),
41
38
  id: e,
@@ -63,11 +60,17 @@ var r = class {
63
60
  done: !1
64
61
  };
65
62
  return c.then((n) => {
66
- o.done = !0, t.done = !0, t.output = n, this._hookedResponses.delete(e), this._resumeWaiters.delete(e), a && i.off("tokens", a), this.ee.emit("done", e), this._processNextJob();
63
+ o.done = !0, t.done = !0, t.output = n, this._hookedResponses.delete(e), this._resumeWaiters.delete(e);
64
+ let r = n[0]?._id || e;
65
+ n.forEach((e) => {
66
+ e._id === void 0 && (e._id = r);
67
+ }), a && i.off("tokens", a), this.ee.emit("done", e), this._processNextJob();
67
68
  }), t;
68
69
  }
69
- let l = await c;
70
- return a && i.off("tokens", a), o.done = !0, this._hookedResponses.delete(e), this._resumeWaiters.delete(e), this.ee.emit("done", e), this._processNextJob(), {
70
+ let l = await c, u = l[0]?._id || e;
71
+ return l.forEach((e) => {
72
+ e._id === void 0 && (e._id = u);
73
+ }), a && i.off("tokens", a), o.done = !0, this._hookedResponses.delete(e), this._resumeWaiters.delete(e), this.ee.emit("done", e), this._processNextJob(), {
71
74
  output: l,
72
75
  id: e,
73
76
  done: !0
@@ -187,7 +187,9 @@ var y = class extends e {
187
187
  this.reset();
188
188
  }
189
189
  initialise(e, n) {
190
- if (this.cache && n?.noCache && this.reset(), this.initialPrompt = e || null, this.lastToken === -1 ? this.outputConversation = (this.initialPrompt || []).slice() : e && e.length > this.outputConversation.length && (this.outputConversation = (this.initialPrompt || []).slice(), this.resetCache()), !this.cache && !n?.noCache && (this.model.config.modelType !== "GenAI_NanoGPT_v1" || this.model.config.useRope)) {
190
+ if (this.cache && n?.noCache && this.reset(), this.initialPrompt = e || null, this.lastToken === -1 && this.outputConversation.length === 0 ? this.outputConversation = this.initialPrompt || [] : e && e.length > 0 && (e.forEach((e) => {
191
+ this.outputConversation.push(e);
192
+ }), this.resetCache()), !this.cache && !n?.noCache && (this.model.config.modelType !== "GenAI_NanoGPT_v1" || this.model.config.useRope)) {
191
193
  let e = Array(this.model.config.nLayer);
192
194
  for (let t = 0; t < this.model.config.nLayer; t++) e[t] = {
193
195
  k: void 0,
@@ -3,7 +3,7 @@ import { I as e } from "../dist-Da20xy8E.js";
3
3
  async function t(t, n, r, i) {
4
4
  if (r) {
5
5
  let a = r.length > 0 && (r[r.length - 1].role === "text" || r[r.length - 1].role === "assistant"), o;
6
- return i?.nonConversational ? o = a && i?.continuation ? [t.bosToken, ...t.encode(r[r.length - 1].content)] : t.encodeAsSequence(r, !0) : a && i?.continuation ? (o = t.encodeConversation(r, !0), o[o.length - 1] === t.getSpecialTokenIndex("<|assistant_start|>") && o[o.length - 2] === t.getSpecialTokenIndex("<|assistant_end|>") && (o.pop(), o.pop())) : o = t.encodeConversation(r, !0), o.length > n && (o = o.slice(-n)), e([o], [1, o.length], "int32");
6
+ return i?.nonConversational ? o = a && i?.continuation ? [t.bosToken, ...t.encode(r[r.length - 1].content)] : t.encodeAsSequence(r, !0) : a && i?.continuation ? (o = t.encodeConversation(r, !0), o[o.length - 1] === t.getSpecialTokenIndex("<|assistant_start|>") && o[o.length - 2] === t.getSpecialTokenIndex("<|assistant_end|>") && (o.pop(), o.pop())) : o = t.encodeConversation(r, !0), o.length > n && (o = o.slice(-n)), i?.debug && console.log("Tokenised prompt:", t.decode(o)), e([o], [1, o.length], "int32");
7
7
  } else {
8
8
  let n = i?.nonConversational ? void 0 : t.getSpecialTokenIndex("<|assistant_start|>"), r = n === void 0 ? [t.bosToken] : [t.bosToken, n];
9
9
  return e([r], [1, r.length], "int32");
@@ -17,6 +17,7 @@ export interface IGeneratorOutputInternal extends IGeneratorOutput {
17
17
  outputTensor: Tensor;
18
18
  }
19
19
  export interface GeneratorConversation extends Conversation {
20
+ _id?: string;
20
21
  _completed?: boolean;
21
22
  _timestamp?: number;
22
23
  _output?: IGeneratorOutput[];
@@ -45,6 +46,7 @@ export interface IGenerateOptions {
45
46
  continuation?: boolean; /** Continue the previous response without adding a new turn. */
46
47
  chunkSize?: number; /** Number of tokens to generate before returning a partial response. */
47
48
  background?: boolean; /** Run the generation in the background and return immediately. */
49
+ debug?: boolean;
48
50
  _onChunk?: (output: IGeneratorOutput) => void | Promise<void>;
49
51
  }
50
52
  export interface IGeneratorResponse {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",