@@ -208,7 +208,27 @@ export type LlamaOptions = {
208208 *
209209 * Defaults to `false` (no NUMA policy).
210210 */
211- numa ?: LlamaNuma
211+ numa ?: LlamaNuma ,
212+
213+ /**
214+ * A list of directories to use for internal cache to speed up processes and reduce memory usage.
215+ * Cache created by this process will be automatically cleaned up when it's no longer needed,
216+ * on process exit, and on the disposal of the Llama instance.
217+ *
218+ * The first directory that is writable from this list will be used.
219+ *
220+ * If no directories are provided or non are writable, these fallback will be used instead:
221+ * - The directory of where `node-llama-cpp` is installed inside `node_modules`.
222+ * - A `nlc.` prefixed directory under the OS's temp dir (e.g. `/tmp` on Linux).
223+ * - A `.node-llama-cpp/.temp` directory under the home directory of the current user.
224+ *
225+ * To disable the usage of any temporary directory and caching, set this option to `false`.
226+ *
227+ *
228+ * Hidden since currently unused - defaults to `false` for now
229+ * @internal
230+ */
231+ tempDir ?: string | string [ ] | false
212232} ;
213233
214234export type LastBuildOptions = {
@@ -318,7 +338,27 @@ export type LastBuildOptions = {
318338 *
319339 * Defaults to `false` (no NUMA policy).
320340 */
321- numa ?: LlamaNuma
341+ numa ?: LlamaNuma ,
342+
343+ /**
344+ * A list of directories to use for internal cache to speed up processes and reduce memory usage.
345+ * Cache created by this process will be automatically cleaned up when it's no longer needed,
346+ * on process exit, and on the disposal of the Llama instance.
347+ *
348+ * The first directory that is writable from this list will be used.
349+ *
350+ * If no directories are provided or non are writable, these fallback will be used instead:
351+ * - The directory of where `node-llama-cpp` is installed inside `node_modules`.
352+ * - A `nlc.` prefixed directory under the OS's temp dir (e.g. `/tmp` on Linux).
353+ * - A `.node-llama-cpp/.temp` directory under the home directory of the current user.
354+ *
355+ * To disable the usage of any temporary directory and caching, set this option to `false`.
356+ *
357+ *
358+ * Hidden since currently unused - defaults to `false` for now
359+ * @internal
360+ */
361+ tempDir ?: string | string [ ] | false
322362} ;
323363
324364export const getLlamaFunctionName = "getLlama" ;
@@ -335,6 +375,7 @@ export const defaultLlamaRamPadding = (totalRam: number) => {
335375const defaultBuildOption : Exclude < LlamaOptions [ "build" ] , undefined > = runningInElectron
336376 ? "never"
337377 : "auto" ;
378+ const defaultTempDir : LlamaOptions [ "tempDir" ] = false ;
338379
339380/**
340381 * Get a `llama.cpp` binding.
@@ -377,6 +418,7 @@ export async function getLlama(options?: LlamaOptions | "lastBuild", lastBuildOp
377418 ramPadding : lastBuildOptions ?. ramPadding ?? defaultLlamaRamPadding ,
378419 debug : lastBuildOptions ?. debug ?? defaultLlamaCppDebugMode ,
379420 numa : lastBuildOptions ?. numa ,
421+ tempDir : lastBuildOptions ?. tempDir ?? defaultTempDir ,
380422 dryRun
381423 } ;
382424
@@ -405,6 +447,7 @@ export async function getLlama(options?: LlamaOptions | "lastBuild", lastBuildOp
405447 ramPadding : lastBuildOptions ?. ramPadding ?? defaultLlamaRamPadding ,
406448 debug : lastBuildOptions ?. debug ?? defaultLlamaCppDebugMode ,
407449 numa : lastBuildOptions ?. numa ,
450+ tempDir : lastBuildOptions ?. tempDir ?? defaultTempDir ,
408451 skipLlamaInit : dryRun
409452 } ) ;
410453
@@ -440,6 +483,7 @@ export async function getLlamaForOptions({
440483 ramPadding = defaultLlamaRamPadding ,
441484 debug = defaultLlamaCppDebugMode ,
442485 numa = false ,
486+ tempDir = defaultTempDir ,
443487 dryRun = false
444488} : LlamaOptions , {
445489 updateLastBuildInfoOnCompile = false ,
@@ -514,6 +558,7 @@ export async function getLlamaForOptions({
514558 ramPadding,
515559 debug,
516560 numa,
561+ tempDir,
517562 dryRun
518563 } ) ;
519564 } catch ( err ) {
@@ -532,6 +577,7 @@ export async function getLlamaForOptions({
532577 ramPadding,
533578 debug,
534579 numa,
580+ tempDir,
535581 dryRun
536582 } ) ;
537583 }
@@ -579,6 +625,7 @@ export async function getLlamaForOptions({
579625 ) ,
580626 debug,
581627 numa,
628+ tempDir,
582629 pipeBinaryTestErrorLogs
583630 } ) ;
584631
@@ -614,7 +661,8 @@ export async function getLlamaForOptions({
614661 ramPadding,
615662 skipLlamaInit,
616663 debug,
617- numa
664+ numa,
665+ tempDir
618666 } ) ;
619667 } catch ( err ) {
620668 console . error (
@@ -691,7 +739,8 @@ export async function getLlamaForOptions({
691739 ramPadding,
692740 skipLlamaInit,
693741 debug,
694- numa
742+ numa,
743+ tempDir
695744 } ) ;
696745 } catch ( err ) {
697746 console . error (
@@ -737,6 +786,7 @@ async function loadExistingLlamaBinary({
737786 fallbackMessage,
738787 debug,
739788 numa,
789+ tempDir,
740790 pipeBinaryTestErrorLogs
741791} : {
742792 buildOptions : BuildOptions ,
@@ -754,6 +804,7 @@ async function loadExistingLlamaBinary({
754804 fallbackMessage : string | null ,
755805 debug : boolean ,
756806 numa ?: LlamaNuma ,
807+ tempDir : LlamaOptions [ "tempDir" ] ,
757808 pipeBinaryTestErrorLogs : boolean
758809} ) {
759810 const buildFolderName = await getBuildFolderNameForBuildOptions ( buildOptions ) ;
@@ -791,7 +842,8 @@ async function loadExistingLlamaBinary({
791842 ramPadding,
792843 skipLlamaInit,
793844 debug,
794- numa
845+ numa,
846+ tempDir
795847 } ) ;
796848 } else if ( progressLogs ) {
797849 console . warn (
@@ -857,7 +909,8 @@ async function loadExistingLlamaBinary({
857909 ramPadding,
858910 skipLlamaInit,
859911 debug,
860- numa
912+ numa,
913+ tempDir
861914 } ) ;
862915 } else if ( progressLogs ) {
863916 const binaryDescription = describeBinary ( {
@@ -913,7 +966,8 @@ async function buildAndLoadLlamaBinary({
913966 ramPadding,
914967 skipLlamaInit,
915968 debug,
916- numa
969+ numa,
970+ tempDir
917971} : {
918972 buildOptions : BuildOptions ,
919973 skipDownload : boolean ,
@@ -925,7 +979,8 @@ async function buildAndLoadLlamaBinary({
925979 ramPadding : Required < LlamaOptions > [ "ramPadding" ] ,
926980 skipLlamaInit : boolean ,
927981 debug : boolean ,
928- numa ?: LlamaNuma
982+ numa ?: LlamaNuma ,
983+ tempDir : LlamaOptions [ "tempDir" ]
929984} ) {
930985 const buildFolderName = await getBuildFolderNameForBuildOptions ( buildOptions ) ;
931986
@@ -960,7 +1015,8 @@ async function buildAndLoadLlamaBinary({
9601015 ramPadding,
9611016 skipLlamaInit,
9621017 debug,
963- numa
1018+ numa,
1019+ tempDir
9641020 } ) ;
9651021}
9661022
0 commit comments