node-llama-cpp 3.0.0-beta.1 → 3.0.0-beta.2

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.
Files changed (149) hide show
  1. package/README.md +2 -0
  2. package/dist/ChatWrapper.d.ts +49 -0
  3. package/dist/ChatWrapper.js +120 -0
  4. package/dist/ChatWrapper.js.map +1 -0
  5. package/dist/chatWrappers/AlpacaChatWrapper.d.ts +12 -0
  6. package/dist/chatWrappers/AlpacaChatWrapper.js +21 -0
  7. package/dist/chatWrappers/AlpacaChatWrapper.js.map +1 -0
  8. package/dist/chatWrappers/ChatMLChatWrapper.d.ts +13 -0
  9. package/dist/chatWrappers/ChatMLChatWrapper.js +83 -0
  10. package/dist/chatWrappers/ChatMLChatWrapper.js.map +1 -0
  11. package/dist/chatWrappers/EmptyChatWrapper.d.ts +4 -0
  12. package/dist/chatWrappers/EmptyChatWrapper.js +5 -0
  13. package/dist/chatWrappers/EmptyChatWrapper.js.map +1 -0
  14. package/dist/chatWrappers/FalconChatWrapper.d.ts +21 -0
  15. package/dist/chatWrappers/FalconChatWrapper.js +104 -0
  16. package/dist/chatWrappers/FalconChatWrapper.js.map +1 -0
  17. package/dist/chatWrappers/FunctionaryChatWrapper.d.ts +41 -0
  18. package/dist/chatWrappers/FunctionaryChatWrapper.js +200 -0
  19. package/dist/chatWrappers/FunctionaryChatWrapper.js.map +1 -0
  20. package/dist/chatWrappers/GeneralChatWrapper.d.ts +21 -0
  21. package/dist/chatWrappers/GeneralChatWrapper.js +112 -0
  22. package/dist/chatWrappers/GeneralChatWrapper.js.map +1 -0
  23. package/dist/chatWrappers/LlamaChatWrapper.d.ts +13 -0
  24. package/dist/chatWrappers/LlamaChatWrapper.js +78 -0
  25. package/dist/chatWrappers/LlamaChatWrapper.js.map +1 -0
  26. package/dist/chatWrappers/resolveChatWrapperBasedOnModel.d.ts +4 -4
  27. package/dist/chatWrappers/resolveChatWrapperBasedOnModel.js +20 -14
  28. package/dist/chatWrappers/resolveChatWrapperBasedOnModel.js.map +1 -1
  29. package/dist/cli/commands/ChatCommand.d.ts +2 -1
  30. package/dist/cli/commands/ChatCommand.js +71 -33
  31. package/dist/cli/commands/ChatCommand.js.map +1 -1
  32. package/dist/config.js +1 -1
  33. package/dist/config.js.map +1 -1
  34. package/dist/index.d.ts +16 -10
  35. package/dist/index.js +15 -8
  36. package/dist/index.js.map +1 -1
  37. package/dist/llamaEvaluator/LlamaBins.d.ts +0 -1
  38. package/dist/llamaEvaluator/LlamaChat/LlamaChat.d.ts +175 -0
  39. package/dist/llamaEvaluator/LlamaChat/LlamaChat.js +704 -0
  40. package/dist/llamaEvaluator/LlamaChat/LlamaChat.js.map +1 -0
  41. package/dist/llamaEvaluator/LlamaChat/utils/FunctionCallGrammar.d.ts +21 -0
  42. package/dist/llamaEvaluator/LlamaChat/utils/FunctionCallGrammar.js +120 -0
  43. package/dist/llamaEvaluator/LlamaChat/utils/FunctionCallGrammar.js.map +1 -0
  44. package/dist/llamaEvaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.d.ts +16 -0
  45. package/dist/llamaEvaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.js +117 -0
  46. package/dist/llamaEvaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.js.map +1 -0
  47. package/dist/llamaEvaluator/{LlamaChatSession.d.ts → LlamaChatSession/LlamaChatSession.d.ts} +48 -25
  48. package/dist/llamaEvaluator/LlamaChatSession/LlamaChatSession.js +211 -0
  49. package/dist/llamaEvaluator/LlamaChatSession/LlamaChatSession.js.map +1 -0
  50. package/dist/llamaEvaluator/LlamaChatSession/utils/defineChatSessionFunction.d.ts +7 -0
  51. package/dist/llamaEvaluator/LlamaChatSession/utils/defineChatSessionFunction.js +8 -0
  52. package/dist/llamaEvaluator/LlamaChatSession/utils/defineChatSessionFunction.js.map +1 -0
  53. package/dist/llamaEvaluator/LlamaContext/LlamaContext.d.ts +18 -23
  54. package/dist/llamaEvaluator/LlamaContext/LlamaContext.js +58 -101
  55. package/dist/llamaEvaluator/LlamaContext/LlamaContext.js.map +1 -1
  56. package/dist/llamaEvaluator/LlamaContext/types.d.ts +6 -10
  57. package/dist/llamaEvaluator/LlamaGrammar.d.ts +8 -12
  58. package/dist/llamaEvaluator/LlamaGrammar.js +7 -12
  59. package/dist/llamaEvaluator/LlamaGrammar.js.map +1 -1
  60. package/dist/llamaEvaluator/LlamaJsonSchemaGrammar.js +2 -1
  61. package/dist/llamaEvaluator/LlamaJsonSchemaGrammar.js.map +1 -1
  62. package/dist/llamaEvaluator/LlamaModel.d.ts +10 -2
  63. package/dist/llamaEvaluator/LlamaModel.js +14 -3
  64. package/dist/llamaEvaluator/LlamaModel.js.map +1 -1
  65. package/dist/types.d.ts +41 -3
  66. package/dist/types.js +5 -1
  67. package/dist/types.js.map +1 -1
  68. package/dist/utils/LlamaText.d.ts +42 -0
  69. package/dist/utils/LlamaText.js +207 -0
  70. package/dist/utils/LlamaText.js.map +1 -0
  71. package/dist/utils/StopGenerationDetector.d.ts +28 -0
  72. package/dist/utils/StopGenerationDetector.js +205 -0
  73. package/dist/utils/StopGenerationDetector.js.map +1 -0
  74. package/dist/utils/TokenStreamRegulator.d.ts +30 -0
  75. package/dist/utils/TokenStreamRegulator.js +96 -0
  76. package/dist/utils/TokenStreamRegulator.js.map +1 -0
  77. package/dist/utils/appendUserMessageToChatHistory.d.ts +2 -0
  78. package/dist/utils/appendUserMessageToChatHistory.js +18 -0
  79. package/dist/utils/appendUserMessageToChatHistory.js.map +1 -0
  80. package/dist/utils/compareTokens.d.ts +2 -0
  81. package/dist/utils/compareTokens.js +4 -0
  82. package/dist/utils/compareTokens.js.map +1 -0
  83. package/dist/utils/compileLLamaCpp.js +11 -6
  84. package/dist/utils/compileLLamaCpp.js.map +1 -1
  85. package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.d.ts +18 -0
  86. package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.js +61 -0
  87. package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.js.map +1 -0
  88. package/dist/utils/gbnfJson/GbnfGrammarGenerator.d.ts +1 -0
  89. package/dist/utils/gbnfJson/GbnfGrammarGenerator.js +17 -0
  90. package/dist/utils/gbnfJson/GbnfGrammarGenerator.js.map +1 -1
  91. package/dist/utils/gbnfJson/GbnfTerminal.d.ts +1 -1
  92. package/dist/utils/gbnfJson/GbnfTerminal.js.map +1 -1
  93. package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.d.ts +6 -0
  94. package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.js +21 -0
  95. package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.js.map +1 -0
  96. package/dist/utils/gbnfJson/types.d.ts +1 -1
  97. package/dist/utils/gbnfJson/types.js.map +1 -1
  98. package/dist/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.d.ts +1 -0
  99. package/dist/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.js.map +1 -1
  100. package/dist/utils/getBin.d.ts +2 -2
  101. package/dist/utils/getGbnfGrammarForGbnfJsonSchema.js +1 -15
  102. package/dist/utils/getGbnfGrammarForGbnfJsonSchema.js.map +1 -1
  103. package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.d.ts +2 -0
  104. package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.js +49 -0
  105. package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.js.map +1 -0
  106. package/dist/utils/resolveChatWrapper.d.ts +4 -0
  107. package/dist/utils/resolveChatWrapper.js +16 -0
  108. package/dist/utils/resolveChatWrapper.js.map +1 -0
  109. package/dist/utils/truncateTextAndRoundToWords.d.ts +8 -0
  110. package/dist/utils/truncateTextAndRoundToWords.js +27 -0
  111. package/dist/utils/truncateTextAndRoundToWords.js.map +1 -0
  112. package/llama/addon.cpp +24 -12
  113. package/llama/binariesGithubRelease.json +1 -1
  114. package/llama/gitRelease.bundle +0 -0
  115. package/llamaBins/linux-arm64/llama-addon.node +0 -0
  116. package/llamaBins/linux-armv7l/llama-addon.node +0 -0
  117. package/llamaBins/linux-x64/llama-addon.node +0 -0
  118. package/llamaBins/mac-arm64/llama-addon.node +0 -0
  119. package/llamaBins/mac-x64/llama-addon.node +0 -0
  120. package/llamaBins/win-x64/llama-addon.node +0 -0
  121. package/package.json +21 -9
  122. package/dist/ChatPromptWrapper.d.ts +0 -11
  123. package/dist/ChatPromptWrapper.js +0 -20
  124. package/dist/ChatPromptWrapper.js.map +0 -1
  125. package/dist/chatWrappers/ChatMLChatPromptWrapper.d.ts +0 -12
  126. package/dist/chatWrappers/ChatMLChatPromptWrapper.js +0 -22
  127. package/dist/chatWrappers/ChatMLChatPromptWrapper.js.map +0 -1
  128. package/dist/chatWrappers/EmptyChatPromptWrapper.d.ts +0 -4
  129. package/dist/chatWrappers/EmptyChatPromptWrapper.js +0 -5
  130. package/dist/chatWrappers/EmptyChatPromptWrapper.js.map +0 -1
  131. package/dist/chatWrappers/FalconChatPromptWrapper.d.ts +0 -19
  132. package/dist/chatWrappers/FalconChatPromptWrapper.js +0 -33
  133. package/dist/chatWrappers/FalconChatPromptWrapper.js.map +0 -1
  134. package/dist/chatWrappers/GeneralChatPromptWrapper.d.ts +0 -19
  135. package/dist/chatWrappers/GeneralChatPromptWrapper.js +0 -38
  136. package/dist/chatWrappers/GeneralChatPromptWrapper.js.map +0 -1
  137. package/dist/chatWrappers/LlamaChatPromptWrapper.d.ts +0 -12
  138. package/dist/chatWrappers/LlamaChatPromptWrapper.js +0 -23
  139. package/dist/chatWrappers/LlamaChatPromptWrapper.js.map +0 -1
  140. package/dist/chatWrappers/generateContextTextFromConversationHistory.d.ts +0 -15
  141. package/dist/chatWrappers/generateContextTextFromConversationHistory.js +0 -39
  142. package/dist/chatWrappers/generateContextTextFromConversationHistory.js.map +0 -1
  143. package/dist/llamaEvaluator/LlamaChatSession.js +0 -290
  144. package/dist/llamaEvaluator/LlamaChatSession.js.map +0 -1
  145. package/dist/utils/getTextCompletion.d.ts +0 -3
  146. package/dist/utils/getTextCompletion.js +0 -12
  147. package/dist/utils/getTextCompletion.js.map +0 -1
  148. package/llamaBins/mac-arm64/ggml-metal.metal +0 -2929
  149. package/llamaBins/mac-x64/ggml-metal.metal +0 -2929
@@ -0,0 +1,211 @@
1
+ import { DisposeAggregator, DisposedError, EventRelay, withLock } from "lifecycle-utils";
2
+ import { defaultChatSystemPrompt } from "../../config.js";
3
+ import { appendUserMessageToChatHistory } from "../../utils/appendUserMessageToChatHistory.js";
4
+ import { LlamaChat } from "../LlamaChat/LlamaChat.js";
5
+ export class LlamaChatSession {
6
+ /** @internal */ _disposeAggregator = new DisposeAggregator();
7
+ /** @internal */ _autoDisposeSequence;
8
+ /** @internal */ _contextShift;
9
+ /** @internal */ _chatHistory;
10
+ /** @internal */ _lastEvaluation;
11
+ /** @internal */ _chat;
12
+ onDispose = new EventRelay();
13
+ /**
14
+ * @param options
15
+ */
16
+ constructor({ contextSequence, chatWrapper = "auto", systemPrompt = defaultChatSystemPrompt, autoDisposeSequence = true, contextShift }) {
17
+ if (contextSequence == null)
18
+ throw new Error("contextSequence cannot be null");
19
+ if (contextSequence.disposed)
20
+ throw new DisposedError();
21
+ this._contextShift = contextShift;
22
+ this._chatHistory = [{
23
+ type: "system",
24
+ text: systemPrompt
25
+ }];
26
+ this._chat = new LlamaChat({
27
+ autoDisposeSequence,
28
+ chatWrapper,
29
+ contextSequence
30
+ });
31
+ this._autoDisposeSequence = autoDisposeSequence;
32
+ this._disposeAggregator.add(this._chat.onDispose.createListener(() => {
33
+ this.dispose();
34
+ }));
35
+ this._disposeAggregator.add(this.onDispose.dispatchEvent);
36
+ }
37
+ dispose({ disposeSequence = this._autoDisposeSequence } = {}) {
38
+ if (this._chat == null)
39
+ return;
40
+ this._chat.dispose({ disposeSequence });
41
+ this._chat = null;
42
+ this._disposeAggregator.dispose();
43
+ }
44
+ /** @hidden */
45
+ [Symbol.dispose]() {
46
+ return this.dispose();
47
+ }
48
+ get disposed() {
49
+ return this._chat == null || this._chat.disposed;
50
+ }
51
+ get sequence() {
52
+ if (this._chat == null)
53
+ throw new DisposedError();
54
+ return this._chat.sequence;
55
+ }
56
+ get context() {
57
+ return this.sequence.context;
58
+ }
59
+ get model() {
60
+ return this.sequence.model;
61
+ }
62
+ /**
63
+ * @param prompt
64
+ * @param [options]
65
+ */
66
+ async prompt(prompt, { functions, documentFunctionParams, onToken, signal, maxTokens, temperature, topK, topP, grammar, trimWhitespaceSuffix = false, repeatPenalty } = {}) {
67
+ const { responseText } = await this.promptWithMeta(prompt, {
68
+ // this is a workaround to allow passing both `functions` and `grammar`
69
+ functions: functions,
70
+ documentFunctionParams: documentFunctionParams,
71
+ onToken, signal, maxTokens, temperature, topK, topP, grammar, trimWhitespaceSuffix, repeatPenalty
72
+ });
73
+ return responseText;
74
+ }
75
+ /**
76
+ * @param prompt
77
+ * @param [options]
78
+ */
79
+ async promptWithMeta(prompt, { functions, documentFunctionParams, onToken, signal, maxTokens, temperature, topK, topP, grammar, trimWhitespaceSuffix = false, repeatPenalty, evaluationPriority } = {}) {
80
+ this._ensureNotDisposed();
81
+ return await withLock(this, "prompt", async () => {
82
+ this._ensureNotDisposed();
83
+ if (this._chat == null)
84
+ throw new DisposedError();
85
+ let lastEvaluation = this._lastEvaluation;
86
+ let newChatHistory = appendUserMessageToChatHistory(this._chatHistory, prompt);
87
+ let newContextWindowChatHistory = lastEvaluation?.contextWindow == null
88
+ ? undefined
89
+ : appendUserMessageToChatHistory(lastEvaluation?.contextWindow, prompt);
90
+ newChatHistory.push({
91
+ type: "model",
92
+ response: []
93
+ });
94
+ if (newContextWindowChatHistory != null)
95
+ newContextWindowChatHistory.push({
96
+ type: "model",
97
+ response: []
98
+ });
99
+ // eslint-disable-next-line no-constant-condition
100
+ while (true) {
101
+ const { functionCall, lastEvaluation: currentLastEvaluation, metadata } = await this._chat.generateResponse(newChatHistory, {
102
+ functions,
103
+ documentFunctionParams,
104
+ grammar: grammar,
105
+ onToken,
106
+ signal,
107
+ repeatPenalty,
108
+ topK,
109
+ topP,
110
+ maxTokens,
111
+ temperature,
112
+ trimWhitespaceSuffix,
113
+ contextShift: {
114
+ ...this._contextShift,
115
+ lastEvaluationMetadata: lastEvaluation?.contextShiftMetadata
116
+ },
117
+ evaluationPriority,
118
+ lastEvaluationContextWindow: {
119
+ history: newContextWindowChatHistory,
120
+ minimumOverlapPercentageToPreventContextShift: 0.01
121
+ }
122
+ });
123
+ this._ensureNotDisposed();
124
+ lastEvaluation = currentLastEvaluation;
125
+ newChatHistory = lastEvaluation.cleanHistory;
126
+ if (functionCall != null) {
127
+ const functionDefinition = functions?.[functionCall.functionName];
128
+ if (functionDefinition == null)
129
+ throw new Error(`The model tried to call function "${functionCall.functionName}" which is not defined`);
130
+ const functionCallResult = await functionDefinition.handler(functionCall.params);
131
+ this._ensureNotDisposed();
132
+ newChatHistory = addFunctionCallToChatHistory({
133
+ chatHistory: newChatHistory,
134
+ functionName: functionCall.functionName,
135
+ functionDescription: functionDefinition.description,
136
+ callParams: functionCall.params,
137
+ callResult: functionCallResult,
138
+ raw: functionCall.raw + this._chat.chatWrapper.generateFunctionCallResult(functionCall.functionName, functionCall.params, functionCallResult)
139
+ });
140
+ newContextWindowChatHistory = addFunctionCallToChatHistory({
141
+ chatHistory: lastEvaluation.contextWindow,
142
+ functionName: functionCall.functionName,
143
+ functionDescription: functionDefinition.description,
144
+ callParams: functionCall.params,
145
+ callResult: functionCallResult,
146
+ raw: functionCall.raw + this._chat.chatWrapper.generateFunctionCallResult(functionCall.functionName, functionCall.params, functionCallResult)
147
+ });
148
+ lastEvaluation.cleanHistory = newChatHistory;
149
+ lastEvaluation.contextWindow = newContextWindowChatHistory;
150
+ continue;
151
+ }
152
+ this._lastEvaluation = lastEvaluation;
153
+ this._chatHistory = newChatHistory;
154
+ const lastModelResponseItem = getLastModelResponseItem(newChatHistory);
155
+ return {
156
+ response: lastModelResponseItem.response,
157
+ responseText: lastModelResponseItem.response
158
+ .filter((item) => typeof item === "string")
159
+ .join(""),
160
+ stopReason: metadata.stopReason,
161
+ remainingGenerationAfterStop: metadata.remainingGenerationAfterStop
162
+ };
163
+ }
164
+ });
165
+ }
166
+ getChatHistory() {
167
+ return structuredClone(this._chatHistory);
168
+ }
169
+ getLastEvaluationContextWindow() {
170
+ if (this._lastEvaluation == null)
171
+ return null;
172
+ return structuredClone(this._lastEvaluation?.contextWindow);
173
+ }
174
+ setChatHistory(chatHistory) {
175
+ this._chatHistory = structuredClone(chatHistory);
176
+ this._lastEvaluation = undefined;
177
+ }
178
+ /** @internal */
179
+ _ensureNotDisposed() {
180
+ if (this.disposed)
181
+ throw new DisposedError();
182
+ }
183
+ }
184
+ function addFunctionCallToChatHistory({ chatHistory, functionName, functionDescription, callParams, callResult, raw }) {
185
+ const newChatHistory = chatHistory.slice();
186
+ if (newChatHistory.length === 0 || newChatHistory[newChatHistory.length - 1].type !== "model")
187
+ newChatHistory.push({
188
+ type: "model",
189
+ response: []
190
+ });
191
+ const lastModelResponseItem = newChatHistory[newChatHistory.length - 1];
192
+ const newLastModelResponseItem = { ...lastModelResponseItem };
193
+ newChatHistory[newChatHistory.length - 1] = newLastModelResponseItem;
194
+ const modelResponse = newLastModelResponseItem.response.slice();
195
+ newLastModelResponseItem.response = modelResponse;
196
+ modelResponse.push({
197
+ type: "functionCall",
198
+ name: functionName,
199
+ description: functionDescription,
200
+ params: callParams,
201
+ result: callResult,
202
+ raw
203
+ });
204
+ return newChatHistory;
205
+ }
206
+ function getLastModelResponseItem(chatHistory) {
207
+ if (chatHistory.length === 0 || chatHistory[chatHistory.length - 1].type !== "model")
208
+ throw new Error("Expected chat history to end with a model response");
209
+ return chatHistory[chatHistory.length - 1];
210
+ }
211
+ //# sourceMappingURL=LlamaChatSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LlamaChatSession.js","sour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AAQ,GAAG,aAAa,CAAC;IAElD,aAAa,CAAC,IAAI,CAAC;QACf,IAAI,EAAE,cAAc;QACpB,IAAI,EAAE,YAAY;QAClB,WAAW,EAAE,mBAAmB;QAChC,MAAM,EAAE,UAAU;QAClB,MAAM,EAAE,UAAU;QAClB,GAAG;KACN,CAAC,CAAC;IAEH,OAAO,cAAc,CAAC;AAC1B,CAAC;AAED,SAAS,wBAAwB,CAAC,WAA8B;IAC5D,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,IAAI,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO;QAChF,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IAE1E,OAAO,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAsB,CAAC;AACpE,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { GbnfJsonSchema, GbnfJsonSchemaToType } from "../../../utils/gbnfJson/types.js";
2
+ import { ChatSessionModelFunction } from "../../../types.js";
3
+ export declare function defineChatSessionFunction<const Params extends GbnfJsonSchema | undefined>({ description, params, handler }: {
4
+ description?: string;
5
+ params?: Params;
6
+ handler: (params: GbnfJsonSchemaToType<Params>) => any;
7
+ }): ChatSessionModelFunction<Params>;
@@ -0,0 +1,8 @@
1
+ export function defineChatSessionFunction({ description, params, handler }) {
2
+ return {
3
+ description,
4
+ params,
5
+ handler
6
+ };
7
+ }
8
+ //# sourceMappingURL=defineChatSessionFunction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineChatSessionFunction.js","sourceRoot":"","sources":["../../../../src/llamaEvaluator/LlamaChatSession/utils/defineChatSessionFunction.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,yBAAyB,CAAkD,EACvF,WAAW,EACX,MAAM,EACN,OAAO,EAKV;IACG,OAAO;QACH,WAAW;QACX,MAAM;QACN,OAAO;KACV,CAAC;AACN,CAAC"}
@@ -2,13 +2,13 @@ import { EventRelay } from "lifecycle-utils";
2
2
  import { Token } from "../../types.js";
3
3
  import { LlamaModel } from "../LlamaModel.js";
4
4
  import { LlamaGrammarEvaluationState } from "../LlamaGrammarEvaluationState.js";
5
- import { ContextShiftOptions, ContextTokensDeleteRange, EvaluationPriority, LlamaContextOptions, LlamaContextSequenceRepeatPenalty, TokenPriority } from "./types.js";
5
+ import { ContextShiftOptions, ContextTokensDeleteRange, EvaluationPriority, LlamaContextOptions, LlamaContextSequenceRepeatPenalty } from "./types.js";
6
6
  export declare class LlamaContext {
7
7
  readonly onDispose: EventRelay<void>;
8
8
  /**
9
9
  * @param options
10
10
  */
11
- constructor({ model, sequences, seed, contextSize, batchSize, f16Kv, logitsAll, embedding, threads, batching: { dispatchSchedule: batchingDispatchSchedule, itemsPrioritizingStrategy: batchingItemsPrioritizingStrategy } }: LlamaContextOptions);
11
+ constructor({ model, sequences, seed, contextSize, batchSize, logitsAll, embedding, threads, batching: { dispatchSchedule: batchingDispatchSchedule, itemsPrioritizingStrategy: batchingItemsPrioritizingStrategy } }: LlamaContextOptions);
12
12
  dispose(): void;
13
13
  /** @hidden */
14
14
  [Symbol.dispose](): void;
@@ -24,8 +24,7 @@ export declare class LlamaContext {
24
24
  * When there are no sequences left, this method will throw an error.
25
25
  * @param [options]
26
26
  */
27
- getSequence({ prependBos, contextShift: { size: contextShiftSize, strategy: contextShiftStrategy } }?: {
28
- prependBos?: boolean;
27
+ getSequence({ contextShift: { size: contextShiftSize, strategy: contextShiftStrategy } }?: {
29
28
  contextShift?: ContextShiftOptions;
30
29
  }): LlamaContextSequence;
31
30
  dispatchPendingBatch(): void;
@@ -39,10 +38,12 @@ export declare class LlamaContextSequence {
39
38
  get disposed(): boolean;
40
39
  get context(): LlamaContext;
41
40
  get model(): LlamaModel;
42
- get prependBos(): boolean;
43
41
  get nextTokenIndex(): number;
44
42
  get contextTokens(): Token[];
45
- get contextTokenPriorities(): number[];
43
+ get isLoadedToMemory(): boolean;
44
+ compareContextTokens(tokens: Token[]): {
45
+ firstDifferentIndex: number;
46
+ };
46
47
  /**
47
48
  * Clear the history of the sequence.
48
49
  * If `prependBos` was enabled, the BOS token will be prepended to the sequence again.
@@ -58,11 +59,11 @@ export declare class LlamaContextSequence {
58
59
  * @param tokens
59
60
  * @param [options]
60
61
  */
61
- evaluate(tokens: Token[], { temperature, topK, topP, grammarEvaluationState, repeatPenalty, evaluationPriority, tokenPriority }?: {
62
+ evaluate(tokens: Token[], { temperature, topK, topP, grammarEvaluationState, repeatPenalty, evaluationPriority, contextShift: { size: contextShiftSize, strategy: contextShiftStrategy }, yieldEosToken }?: {
62
63
  temperature?: number;
63
64
  topK?: number;
64
65
  topP?: number;
65
- grammarEvaluationState?: LlamaGrammarEvaluationState;
66
+ grammarEvaluationState?: LlamaGrammarEvaluationState | (() => LlamaGrammarEvaluationState | undefined);
66
67
  repeatPenalty?: LlamaContextSequenceRepeatPenalty;
67
68
  /**
68
69
  * When a lot of tokens are queued for the next batch, more than the configured `batchSize`, the tokens for each sequence will be
@@ -74,21 +75,21 @@ export declare class LlamaContextSequence {
74
75
  * is, the more likely and more tokens will be evaluated for that sequence in the next queued batch.
75
76
  */
76
77
  evaluationPriority?: EvaluationPriority;
78
+ /** Override the sequence context shift options for this evaluation */
79
+ contextShift?: ContextShiftOptions;
77
80
  /**
78
- * When the context is full, tokens will be erased based on the context shift strategy chosen.
79
- * By default, the lowest priority tokens at the beginning of the context will be erased.
80
- * To mark the priority of the evaluated tokens, use this option.
81
- * The higher the priority is, the less likely it will be erased.
82
- * The default priority is `1`.
81
+ * Yield the EOS token when it's generated.
82
+ * When `false` the generation will stop when the EOS token is generated and the EOS token won't be yielded.
83
+ * Defaults to `false`.
83
84
  */
84
- tokenPriority?: TokenPriority | TokenPriority[];
85
+ yieldEosToken?: boolean;
85
86
  }): AsyncGenerator<Token, void>;
86
87
  /**
87
88
  * Evaluate the provided tokens into the context sequence without generating new tokens.
88
89
  * @param tokens
89
90
  * @param [options]
90
91
  */
91
- evaluateWithoutGeneratingNewTokens(tokens: Token[], { evaluationPriority, tokenPriority }?: {
92
+ evaluateWithoutGeneratingNewTokens(tokens: Token[], { evaluationPriority, contextShift: { size: contextShiftSize, strategy: contextShiftStrategy } }?: {
92
93
  grammarEvaluationState?: LlamaGrammarEvaluationState;
93
94
  /**
94
95
  * When a lot of tokens are queued for the next batch, more than the configured `batchSize`, the tokens for each sequence will be
@@ -100,13 +101,7 @@ export declare class LlamaContextSequence {
100
101
  * is, the more likely and more tokens will be evaluated for that sequence in the next queued batch.
101
102
  */
102
103
  evaluationPriority?: EvaluationPriority;
103
- /**
104
- * When the context is full, tokens will be erased based on the context shift strategy chosen.
105
- * By default, the lowest priority tokens at the beginning of the context will be erased.
106
- * To mark the priority of the evaluated tokens, use this option.
107
- * The higher the priority is, the less likely it will be erased.
108
- * The default priority is `1`.
109
- */
110
- tokenPriority?: TokenPriority | TokenPriority[];
104
+ /** Override the sequence context shift options for this evaluation */
105
+ contextShift?: ContextShiftOptions;
111
106
  }): Promise<void>;
112
107
  }