-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathLlamaChatSession.ts
985 lines (850 loc) · 38.3 KB
/
LlamaChatSession.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
import {DisposeAggregator, DisposedError, EventRelay, withLock} from "lifecycle-utils";
import {ChatWrapper} from "../../ChatWrapper.js";
import {
ChatHistoryItem, ChatModelFunctionCall, ChatModelFunctions, ChatModelResponse, ChatSessionModelFunction, ChatSessionModelFunctions,
Token
} from "../../types.js";
import {appendUserMessageToChatHistory} from "../../utils/appendUserMessageToChatHistory.js";
import {LlamaContextSequence} from "../LlamaContext/LlamaContext.js";
import {LlamaGrammar} from "../LlamaGrammar.js";
import {
LlamaChat, LLamaChatContextShiftOptions, LlamaChatResponse, LlamaChatResponseFunctionCall, LlamaChatResponseChunk
} from "../LlamaChat/LlamaChat.js";
import {EvaluationPriority} from "../LlamaContext/types.js";
import {TokenBias} from "../TokenBias.js";
import {LlamaText, LlamaTextJSON} from "../../utils/LlamaText.js";
import {wrapAbortSignal} from "../../utils/wrapAbortSignal.js";
import {safeEventCallback} from "../../utils/safeEventCallback.js";
import {
LLamaChatPromptCompletionEngineOptions, LlamaChatSessionPromptCompletionEngine
} from "./utils/LlamaChatSessionPromptCompletionEngine.js";
export type LlamaChatSessionOptions = {
contextSequence: LlamaContextSequence,
/** `"auto"` is used by default */
chatWrapper?: "auto" | ChatWrapper,
systemPrompt?: string,
/**
* Add the system prompt even on models that don't support a system prompt.
*
* Each chat wrapper has its own workaround for adding a system prompt to a model that doesn't support it,
* but forcing the system prompt on unsupported models may not always work as expected.
*
* Use with caution.
*/
forceAddSystemPrompt?: boolean,
/**
* Automatically dispose the sequence when the session is disposed.
*
* Defaults to `false`.
*/
autoDisposeSequence?: boolean,
contextShift?: LlamaChatSessionContextShiftOptions
};
export type LlamaChatSessionContextShiftOptions = {
/**
* The number of tokens to delete from the context window to make space for new ones.
* Defaults to 10% of the context size.
*/
size?: LLamaChatContextShiftOptions["size"],
/**
* The strategy to use when deleting tokens from the context window.
*
* Defaults to `"eraseFirstResponseAndKeepFirstSystem"`.
*/
strategy?: LLamaChatContextShiftOptions["strategy"]
};
export type LLamaChatPromptOptions<Functions extends ChatSessionModelFunctions | undefined = ChatSessionModelFunctions | undefined> = {
/**
* Called as the model generates the main response with the generated text chunk.
*
* Useful for streaming the generated response as it's being generated.
*
* Includes only the main response without any text segments (like thoughts).
* For streaming the response with segments, use {@link onResponseChunk `onResponseChunk`}.
*/
onTextChunk?: (text: string) => void,
/**
* Called as the model generates the main response with the generated tokens.
*
* Preferably, you'd want to use {@link onTextChunk `onTextChunk`} instead of this.
*
* Includes only the main response without any segments (like thoughts).
* For streaming the response with segments, use {@link onResponseChunk `onResponseChunk`}.
*/
onToken?: (tokens: Token[]) => void,
/**
* Called as the model generates a response with the generated text and tokens,
* including segment information (when the generated output is part of a segment).
*
* Useful for streaming the generated response as it's being generated, including the main response and all segments.
*
* Only use this function when you need the segmented texts, like thought segments (chain of thought text).
*/
onResponseChunk?: (chunk: LlamaChatResponseChunk) => void,
signal?: AbortSignal,
/**
* When a response already started being generated and then the signal is aborted,
* the generation will stop and the response will be returned as is instead of throwing an error.
*
* Defaults to `false`.
*/
stopOnAbortSignal?: boolean,
maxTokens?: number,
/**
* Temperature is a hyperparameter that controls the randomness of the generated text.
* It affects the probability distribution of the model's output tokens.
*
* A higher temperature (e.g., 1.5) makes the output more random and creative,
* while a lower temperature (e.g., 0.5) makes the output more focused, deterministic, and conservative.
*
* The suggested temperature is 0.8, which provides a balance between randomness and determinism.
*
* At the extreme, a temperature of 0 will always pick the most likely next token, leading to identical outputs in each run.
*
* Set to `0` to disable.
* Disabled by default (set to `0`).
*/
temperature?: number,
/**
* From the next token candidates, discard the percentage of tokens with the lowest probability.
* For example, if set to `0.05`, 5% of the lowest probability tokens will be discarded.
* This is useful for generating more high-quality results when using a high temperature.
* Set to a value between `0` and `1` to enable.
*
* Only relevant when `temperature` is set to a value greater than `0`.
* Disabled by default.
*/
minP?: number,
/**
* Limits the model to consider only the K most likely next tokens for sampling at each step of sequence generation.
* An integer number between `1` and the size of the vocabulary.
* Set to `0` to disable (which uses the full vocabulary).
*
* Only relevant when `temperature` is set to a value greater than 0.
*/
topK?: number,
/**
* Dynamically selects the smallest set of tokens whose cumulative probability exceeds the threshold P,
* and samples the next token only from this set.
* A float number between `0` and `1`.
* Set to `1` to disable.
*
* Only relevant when `temperature` is set to a value greater than `0`.
*/
topP?: number,
/**
* Used to control the randomness of the generated text.
*
* Change the seed to get different results.
*
* Only relevant when using `temperature`.
*/
seed?: number,
/**
* Trim whitespace from the end of the generated text
* Disabled by default.
*/
trimWhitespaceSuffix?: boolean,
/**
* Force a given text prefix to be the start of the model response, to make the model follow a certain direction.
*
* May cause some models to not use the given functions in some scenarios where they would have been used otherwise,
* so avoid using it together with function calling if you notice unexpected behavior.
*/
responsePrefix?: string,
/**
* See the parameter `evaluationPriority` on the `LlamaContextSequence.evaluate()` function for more information.
*/
evaluationPriority?: EvaluationPriority,
repeatPenalty?: false | LlamaChatSessionRepeatPenalty,
/**
* Adjust the probability of tokens being generated.
* Can be used to bias the model to generate tokens that you want it to lean towards,
* or to avoid generating tokens that you want it to avoid.
*/
tokenBias?: TokenBias | (() => TokenBias),
/**
* Custom stop triggers to stop the generation of the response when any of the provided triggers are found.
*/
customStopTriggers?: (LlamaText | string | (string | Token)[])[]
} & ({
grammar?: LlamaGrammar,
functions?: never,
documentFunctionParams?: never,
maxParallelFunctionCalls?: never
} | {
grammar?: never,
functions?: Functions | ChatSessionModelFunctions,
documentFunctionParams?: boolean,
maxParallelFunctionCalls?: number
});
export type LLamaChatCompletePromptOptions = {
/**
* Generate a completion for the given user prompt up to the given number of tokens.
*
* Defaults to `256` or half the context size, whichever is smaller.
*/
maxTokens?: LLamaChatPromptOptions["maxTokens"],
/**
* When a completion already started being generated and then the given `signal` is aborted,
* the generation will stop and the completion will be returned as-is instead of throwing an error.
*
* Defaults to `false`.
*/
stopOnAbortSignal?: LLamaChatPromptOptions["stopOnAbortSignal"],
/**
* Called as the model generates a completion with the generated text chunk.
*
* Useful for streaming the generated completion as it's being generated.
*/
onTextChunk?: LLamaChatPromptOptions["onTextChunk"],
/**
* Called as the model generates a completion with the generated tokens.
*
* Preferably, you'd want to use `onTextChunk` instead of this.
*/
onToken?: LLamaChatPromptOptions["onToken"],
signal?: LLamaChatPromptOptions["signal"],
temperature?: LLamaChatPromptOptions["temperature"],
minP?: LLamaChatPromptOptions["minP"],
topK?: LLamaChatPromptOptions["topK"],
topP?: LLamaChatPromptOptions["topP"],
seed?: LLamaChatPromptOptions["seed"],
trimWhitespaceSuffix?: LLamaChatPromptOptions["trimWhitespaceSuffix"],
evaluationPriority?: LLamaChatPromptOptions["evaluationPriority"],
repeatPenalty?: LLamaChatPromptOptions["repeatPenalty"],
tokenBias?: LLamaChatPromptOptions["tokenBias"],
customStopTriggers?: LLamaChatPromptOptions["customStopTriggers"],
grammar?: LlamaGrammar,
/**
* Functions are not used by the model here,
* but are used for keeping the instructions given to the model about the functions in the current context state,
* to avoid context shifts.
*
* It's best to provide the same functions that were used for the previous prompt here.
*/
functions?: ChatSessionModelFunctions,
/**
* Functions are not used by the model here,
* but are used for keeping the instructions given to the model about the functions in the current context state,
* to avoid context shifts.
*
* It's best to provide the same value that was used for the previous prompt here.
*/
documentFunctionParams?: boolean
};
export type LLamaChatPreloadPromptOptions = {
signal?: LLamaChatCompletePromptOptions["signal"],
evaluationPriority?: LLamaChatCompletePromptOptions["evaluationPriority"],
functions?: LLamaChatCompletePromptOptions["functions"],
documentFunctionParams?: LLamaChatCompletePromptOptions["documentFunctionParams"]
};
export type LlamaChatSessionRepeatPenalty = {
/**
* Number of recent tokens generated by the model to apply penalties to repetition of.
* Defaults to `64`.
*/
lastTokens?: number,
punishTokensFilter?: (tokens: Token[]) => Token[],
/**
* Penalize new line tokens.
* Enabled by default.
*/
penalizeNewLine?: boolean,
/**
* The relative amount to lower the probability of the tokens in `punishTokens` by
* Defaults to `1.1`.
* Set to `1` to disable.
*/
penalty?: number,
/**
* For n time a token is in the `punishTokens` array, lower its probability by `n * frequencyPenalty`
* Disabled by default (`0`).
* Set to a value between `0` and `1` to enable.
*/
frequencyPenalty?: number,
/**
* Lower the probability of all the tokens in the `punishTokens` array by `presencePenalty`
* Disabled by default (`0`).
* Set to a value between `0` and `1` to enable.
*/
presencePenalty?: number
};
/**
* @see [Using `LlamaChatSession`](https://node-llama-cpp.withcat.ai/guide/chat-session) tutorial
*/
export class LlamaChatSession {
/** @internal */ private readonly _disposeAggregator = new DisposeAggregator();
/** @internal */ private readonly _autoDisposeSequence: boolean;
/** @internal */ private readonly _contextShift?: LlamaChatSessionContextShiftOptions;
/** @internal */ private readonly _forceAddSystemPrompt: boolean;
/** @internal */ private readonly _systemPrompt?: string;
/** @internal */ private readonly _chatLock = {};
/** @internal */ private _chatHistory: ChatHistoryItem[];
/** @internal */ private _lastEvaluation?: LlamaChatResponse["lastEvaluation"];
/** @internal */ private _chat: LlamaChat | null;
/** @internal */ public _chatHistoryStateRef = {};
/** @internal */ public readonly _preloadAndCompleteAbortControllers = new Set<AbortController>();
public readonly onDispose = new EventRelay<void>();
public constructor(options: LlamaChatSessionOptions) {
const {
contextSequence,
chatWrapper = "auto",
systemPrompt,
forceAddSystemPrompt = false,
autoDisposeSequence = false,
contextShift
} = options;
if (contextSequence == null)
throw new Error("contextSequence cannot be null");
if (contextSequence.disposed)
throw new DisposedError();
this._contextShift = contextShift;
this._forceAddSystemPrompt = forceAddSystemPrompt;
this._systemPrompt = systemPrompt;
this._chat = new LlamaChat({
autoDisposeSequence,
chatWrapper,
contextSequence
});
const chatWrapperSupportsSystemMessages = this._chat.chatWrapper.settings.supportsSystemMessages;
if (chatWrapperSupportsSystemMessages == null || chatWrapperSupportsSystemMessages || this._forceAddSystemPrompt)
this._chatHistory = this._chat.chatWrapper.generateInitialChatHistory({systemPrompt: this._systemPrompt});
else
this._chatHistory = [];
this._autoDisposeSequence = autoDisposeSequence;
this._disposeAggregator.add(
this._chat.onDispose.createListener(() => {
this.dispose();
})
);
this._disposeAggregator.add(this.onDispose.dispatchEvent);
}
public dispose({disposeSequence = this._autoDisposeSequence}: {disposeSequence?: boolean} = {}) {
if (this._chat == null)
return;
this._chat.dispose({disposeSequence});
this._chat = null;
this._disposeAggregator.dispose();
}
/** @hidden */
public [Symbol.dispose]() {
return this.dispose();
}
public get disposed() {
return this._chat == null || this._chat.disposed;
}
public get chatWrapper() {
if (this._chat == null)
throw new DisposedError();
return this._chat.chatWrapper;
}
public get sequence() {
if (this._chat == null)
throw new DisposedError();
return this._chat.sequence;
}
public get context() {
return this.sequence.context;
}
public get model() {
return this.sequence.model;
}
public async prompt<const Functions extends ChatSessionModelFunctions | undefined = undefined>(
prompt: string,
options: LLamaChatPromptOptions<Functions> = {}
) {
const {
functions,
documentFunctionParams,
maxParallelFunctionCalls,
onTextChunk,
onToken,
onResponseChunk,
signal,
stopOnAbortSignal = false,
maxTokens,
temperature,
minP,
topK,
topP,
seed,
grammar,
trimWhitespaceSuffix = false,
responsePrefix,
repeatPenalty,
tokenBias,
customStopTriggers
} = options;
const {responseText} = await this.promptWithMeta<Functions>(prompt, {
// this is a workaround to allow passing both `functions` and `grammar`
functions: functions as undefined,
documentFunctionParams: documentFunctionParams as undefined,
maxParallelFunctionCalls: maxParallelFunctionCalls as undefined,
onTextChunk, onToken, onResponseChunk, signal, stopOnAbortSignal, maxTokens, temperature, minP, topK, topP, seed, grammar,
trimWhitespaceSuffix, responsePrefix, repeatPenalty, tokenBias, customStopTriggers
});
return responseText;
}
/**
* @param prompt
* @param [options]
*/
public async promptWithMeta<const Functions extends ChatSessionModelFunctions | undefined = undefined>(prompt: string, {
functions,
documentFunctionParams,
maxParallelFunctionCalls,
onTextChunk,
onToken,
onResponseChunk,
signal,
stopOnAbortSignal = false,
maxTokens,
temperature,
minP,
topK,
topP,
seed,
grammar,
trimWhitespaceSuffix = false,
responsePrefix,
repeatPenalty,
tokenBias,
customStopTriggers,
evaluationPriority
}: LLamaChatPromptOptions<Functions> = {}) {
this._ensureNotDisposed();
if (grammar != null && grammar._llama !== this.model._llama)
throw new Error("The LlamaGrammar used by passed to this function was created with a different Llama instance than the one used by this sequence's model. Make sure you use the same Llama instance for both the model and the grammar.");
this._stopAllPreloadAndPromptCompletions();
return await withLock(this._chatLock, "evaluation", signal, async () => {
this._ensureNotDisposed();
this._stopAllPreloadAndPromptCompletions();
if (this._chat == null)
throw new DisposedError();
const supportsParallelFunctionCalling = this._chat.chatWrapper.settings.functions.parallelism != null;
const [abortController, disposeAbortController] = wrapAbortSignal(signal);
let lastEvaluation = this._lastEvaluation;
let newChatHistory = appendUserMessageToChatHistory(this._chatHistory, prompt);
let newContextWindowChatHistory = lastEvaluation?.contextWindow == null
? undefined
: appendUserMessageToChatHistory(lastEvaluation?.contextWindow, prompt);
const resolvedResponsePrefix = (responsePrefix != null && responsePrefix !== "")
? responsePrefix
: undefined;
newChatHistory.push({
type: "model",
response: resolvedResponsePrefix != null
? [resolvedResponsePrefix]
: []
});
if (newContextWindowChatHistory != null)
newContextWindowChatHistory.push({
type: "model",
response: resolvedResponsePrefix != null
? [resolvedResponsePrefix]
: []
});
if (resolvedResponsePrefix != null) {
safeEventCallback(onToken)?.(this.model.tokenize(resolvedResponsePrefix));
safeEventCallback(onTextChunk)?.(resolvedResponsePrefix);
safeEventCallback(onResponseChunk)?.({
type: undefined,
segmentType: undefined,
text: resolvedResponsePrefix,
tokens: this.model.tokenize(resolvedResponsePrefix)
});
}
try {
while (true) {
const functionCallsAndResults: Array<Promise<null | {
functionCall: LlamaChatResponseFunctionCall<Functions extends ChatModelFunctions ? Functions : ChatModelFunctions>,
functionDefinition: ChatSessionModelFunction<any>,
functionCallResult: any
}>> = [];
let canThrowFunctionCallingErrors = false;
let abortedOnFunctionCallError = false;
const initialOutputTokens = this._chat.sequence.tokenMeter.usedOutputTokens;
const {
lastEvaluation: currentLastEvaluation,
metadata
} = await this._chat.generateResponse<Functions>(newChatHistory, {
functions,
documentFunctionParams,
maxParallelFunctionCalls,
grammar: grammar as undefined, // this is a workaround to allow passing both `functions` and `grammar`
onTextChunk: safeEventCallback(onTextChunk),
onToken: safeEventCallback(onToken),
onResponseChunk: safeEventCallback(onResponseChunk),
signal: abortController.signal,
stopOnAbortSignal,
repeatPenalty,
minP,
topK,
topP,
seed,
tokenBias,
customStopTriggers,
maxTokens,
temperature,
trimWhitespaceSuffix,
contextShift: {
...this._contextShift,
lastEvaluationMetadata: lastEvaluation?.contextShiftMetadata
},
evaluationPriority,
lastEvaluationContextWindow: {
history: newContextWindowChatHistory,
minimumOverlapPercentageToPreventContextShift: 0.5
},
onFunctionCall: async (functionCall) => {
functionCallsAndResults.push(
(async () => {
try {
const functionDefinition = functions?.[functionCall.functionName];
if (functionDefinition == null)
throw new Error(
`The model tried to call function "${functionCall.functionName}" which is not defined`
);
const functionCallResult = await functionDefinition.handler(functionCall.params as any);
return {
functionCall,
functionDefinition,
functionCallResult
};
} catch (err) {
if (!abortController.signal.aborted) {
abortedOnFunctionCallError = true;
abortController.abort(err);
}
if (canThrowFunctionCallingErrors)
throw err;
return null;
}
})()
);
}
});
this._ensureNotDisposed();
if (abortController.signal.aborted && (abortedOnFunctionCallError || !stopOnAbortSignal))
throw abortController.signal.reason;
if (maxTokens != null)
maxTokens = Math.max(0, maxTokens - (this._chat.sequence.tokenMeter.usedOutputTokens - initialOutputTokens));
lastEvaluation = currentLastEvaluation;
newChatHistory = lastEvaluation.cleanHistory;
if (functionCallsAndResults.length > 0) {
canThrowFunctionCallingErrors = true;
const functionCallResultsPromise = Promise.all(functionCallsAndResults);
const raceEventAbortController = new AbortController();
await Promise.race([
functionCallResultsPromise,
new Promise<void>((accept, reject) => {
abortController.signal.addEventListener("abort", () => {
if (abortedOnFunctionCallError || !stopOnAbortSignal)
reject(abortController.signal.reason);
else
accept();
}, {signal: raceEventAbortController.signal});
if (abortController.signal.aborted) {
if (abortedOnFunctionCallError || !stopOnAbortSignal)
reject(abortController.signal.reason);
else
accept();
}
})
]);
raceEventAbortController.abort();
this._ensureNotDisposed();
if (!abortController.signal.aborted) {
const functionCallResults = (await functionCallResultsPromise)
.filter((result): result is Exclude<typeof result, null> => result != null);
this._ensureNotDisposed();
if (abortController.signal.aborted && (abortedOnFunctionCallError || !stopOnAbortSignal))
throw abortController.signal.reason;
newContextWindowChatHistory = lastEvaluation.contextWindow;
let startNewChunk = supportsParallelFunctionCalling;
for (const {functionCall, functionDefinition, functionCallResult} of functionCallResults) {
newChatHistory = addFunctionCallToChatHistory({
chatHistory: newChatHistory,
functionName: functionCall.functionName,
functionDescription: functionDefinition.description,
callParams: functionCall.params,
callResult: functionCallResult,
rawCall: functionCall.raw,
startsNewChunk: startNewChunk
});
newContextWindowChatHistory = addFunctionCallToChatHistory({
chatHistory: newContextWindowChatHistory,
functionName: functionCall.functionName,
functionDescription: functionDefinition.description,
callParams: functionCall.params,
callResult: functionCallResult,
rawCall: functionCall.raw,
startsNewChunk: startNewChunk
});
startNewChunk = false;
}
lastEvaluation.cleanHistory = newChatHistory;
lastEvaluation.contextWindow = newContextWindowChatHistory;
if (abortController.signal.aborted && !abortedOnFunctionCallError && stopOnAbortSignal) {
metadata.stopReason = "abort";
metadata.remainingGenerationAfterStop = undefined;
} else
continue;
}
}
this._lastEvaluation = lastEvaluation;
this._chatHistory = newChatHistory;
this._chatHistoryStateRef = {};
const lastModelResponseItem = getLastModelResponseItem(newChatHistory);
const responseText = lastModelResponseItem.response
.filter((item): item is string => typeof item === "string")
.join("");
if (metadata.stopReason === "customStopTrigger")
return {
response: lastModelResponseItem.response,
responseText,
stopReason: metadata.stopReason,
customStopTrigger: metadata.customStopTrigger,
remainingGenerationAfterStop: metadata.remainingGenerationAfterStop
};
return {
response: lastModelResponseItem.response,
responseText,
stopReason: metadata.stopReason,
remainingGenerationAfterStop: metadata.remainingGenerationAfterStop
};
}
} finally {
disposeAbortController();
}
});
}
/**
* Preload a user prompt into the current context sequence state to make later inference of the model response begin sooner
* and feel faster.
*
* > **Note:** Preloading a long user prompt can incur context shifts, so consider limiting the length of prompts you preload
* @param prompt - the prompt to preload
* @param [options]
*/
public async preloadPrompt(prompt: string, options: LLamaChatPreloadPromptOptions = {}): Promise<void> {
await this.completePromptWithMeta(prompt, {
...options,
maxTokens: 0
});
}
/**
* Preload a user prompt into the current context sequence state and generate a completion for it.
*
* > **Note:** Preloading a long user prompt and completing a user prompt with a high number of `maxTokens` can incur context shifts,
* > so consider limiting the length of prompts you preload.
* >
* > Also, it's recommended to limit the number of tokens generated to a reasonable amount by configuring `maxTokens`.
* @param prompt - the prompt to preload
* @param [options]
*/
public async completePrompt(prompt: string, options: LLamaChatCompletePromptOptions = {}): Promise<string> {
const {completion} = await this.completePromptWithMeta(prompt, options);
return completion;
}
/**
* Create a smart completion engine that caches the prompt completions
* and reuses them when the user prompt matches the beginning of the cached prompt or completion.
*
* All completions are made and cache is used only for the current chat session state.
* You can create a single completion engine for an entire chat session.
*/
public createPromptCompletionEngine(options?: LLamaChatPromptCompletionEngineOptions) {
return LlamaChatSessionPromptCompletionEngine._create(this, options);
}
/**
* See `completePrompt` for more information.
* @param prompt
* @param [options]
*/
public async completePromptWithMeta(prompt: string, {
maxTokens,
stopOnAbortSignal = false,
functions,
documentFunctionParams,
onTextChunk,
onToken,
signal,
temperature,
minP,
topK,
topP,
seed,
grammar,
trimWhitespaceSuffix = false,
repeatPenalty,
tokenBias,
customStopTriggers,
evaluationPriority
}: LLamaChatCompletePromptOptions = {}) {
this._ensureNotDisposed();
if (grammar != null) {
if (grammar._llama == null)
throw new Error("The grammar passed to this function is not a LlamaGrammar instance.");
else if (grammar._llama !== this.model._llama)
throw new Error("The LlamaGrammar used by passed to this function was created with a different Llama instance than the one used by this sequence's model. Make sure you use the same Llama instance for both the model and the grammar.");
}
const [abortController, disposeAbortController] = wrapAbortSignal(signal);
this._preloadAndCompleteAbortControllers.add(abortController);
try {
return await withLock(this._chatLock, "evaluation", abortController.signal, async () => {
this._ensureNotDisposed();
if (this._chat == null)
throw new DisposedError();
const {completion, lastEvaluation, metadata} = await this._chat.loadChatAndCompleteUserMessage(
asWithLastUserMessageRemoved(this._chatHistory),
{
initialUserPrompt: prompt,
functions,
documentFunctionParams,
grammar,
onTextChunk,
onToken,
signal: abortController.signal,
stopOnAbortSignal: true,
repeatPenalty,
minP,
topK,
topP,
seed,
tokenBias,
customStopTriggers,
maxTokens,
temperature,
trimWhitespaceSuffix,
contextShift: {
...this._contextShift,
lastEvaluationMetadata: this._lastEvaluation?.contextShiftMetadata
},
evaluationPriority,
lastEvaluationContextWindow: {
history: asWithLastUserMessageRemoved(this._lastEvaluation?.contextWindow),
minimumOverlapPercentageToPreventContextShift: 0.8
}
}
);
this._ensureNotDisposed();
this._lastEvaluation = {
cleanHistory: this._chatHistory,
contextWindow: lastEvaluation.contextWindow,
contextShiftMetadata: lastEvaluation.contextShiftMetadata
};
if (!stopOnAbortSignal && metadata.stopReason === "abort" && abortController.signal?.aborted)
throw abortController.signal.reason;
if (metadata.stopReason === "customStopTrigger")
return {
completion: completion,
stopReason: metadata.stopReason,
customStopTrigger: metadata.customStopTrigger,
remainingGenerationAfterStop: metadata.remainingGenerationAfterStop
};
return {
completion: completion,
stopReason: metadata.stopReason,
remainingGenerationAfterStop: metadata.remainingGenerationAfterStop
};
});
} finally {
this._preloadAndCompleteAbortControllers.delete(abortController);
disposeAbortController();
}
}
public getChatHistory() {
return structuredClone(this._chatHistory);
}
public getLastEvaluationContextWindow() {
if (this._lastEvaluation == null)
return null;
return structuredClone(this._lastEvaluation?.contextWindow);
}
public setChatHistory(chatHistory: ChatHistoryItem[]) {
this._chatHistory = structuredClone(chatHistory);
this._chatHistoryStateRef = {};
this._lastEvaluation = undefined;
}
/** Clear the chat history and reset it to the initial state. */
public resetChatHistory() {
if (this._chat == null || this.disposed)
throw new DisposedError();
const chatWrapperSupportsSystemMessages = this._chat.chatWrapper.settings.supportsSystemMessages;
if (chatWrapperSupportsSystemMessages == null || chatWrapperSupportsSystemMessages || this._forceAddSystemPrompt)
this.setChatHistory(
this._chat.chatWrapper.generateInitialChatHistory({systemPrompt: this._systemPrompt})
);
else
this.setChatHistory([]);
}
/** @internal */
private _stopAllPreloadAndPromptCompletions() {
for (const abortController of this._preloadAndCompleteAbortControllers)
abortController.abort();
this._preloadAndCompleteAbortControllers.clear();
}
/** @internal */
private _ensureNotDisposed() {
if (this.disposed)
throw new DisposedError();
}
}
function addFunctionCallToChatHistory({
chatHistory,
functionName,
functionDescription,
callParams,
callResult,
rawCall,
startsNewChunk
}: {
chatHistory: ChatHistoryItem[],
functionName: string,
functionDescription?: string,
callParams: any,
callResult: any,
rawCall?: LlamaTextJSON,
startsNewChunk?: boolean
}) {
const newChatHistory = chatHistory.slice();
if (newChatHistory.length === 0 || newChatHistory[newChatHistory.length - 1]!.type !== "model")
newChatHistory.push({
type: "model",
response: []
});
const lastModelResponseItem = newChatHistory[newChatHistory.length - 1] as ChatModelResponse;
const newLastModelResponseItem = {...lastModelResponseItem};
newChatHistory[newChatHistory.length - 1] = newLastModelResponseItem;
const modelResponse = newLastModelResponseItem.response.slice();
newLastModelResponseItem.response = modelResponse;
const functionCall: ChatModelFunctionCall = {
type: "functionCall",
name: functionName,
description: functionDescription,
params: callParams,
result: callResult,
rawCall
};
if (startsNewChunk)
functionCall.startsNewChunk = true;
modelResponse.push(functionCall);
return newChatHistory;
}
function getLastModelResponseItem(chatHistory: ChatHistoryItem[]) {
if (chatHistory.length === 0 || chatHistory[chatHistory.length - 1]!.type !== "model")
throw new Error("Expected chat history to end with a model response");
return chatHistory[chatHistory.length - 1] as ChatModelResponse;
}
function asWithLastUserMessageRemoved(chatHistory: ChatHistoryItem[]): ChatHistoryItem[];
function asWithLastUserMessageRemoved(chatHistory: ChatHistoryItem[] | undefined): ChatHistoryItem[] | undefined;
function asWithLastUserMessageRemoved(chatHistory?: ChatHistoryItem[]) {
if (chatHistory == null)
return chatHistory;
const newChatHistory = chatHistory.slice();
while (newChatHistory.at(-1)?.type === "user")
newChatHistory.pop();
return newChatHistory;
}