-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcache-manager.js
99 lines (79 loc) · 3.34 KB
/
cache-manager.js
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
/**
* @file examples/caching/cache-manager.js
* @description This example demonstrates the basic usage of the "cache-manager" module with a filesystem storage mechanism.
*
* To run this example, you first need to install the required modules by executing:
*
* npm install [email protected] cache-manager-fs-hash dotenv
*
* In this example, "cache-manager" is configured using the LLMInterface.configureCache() method. Subsequent calls to LLMInterface.sendMessage()
* with the interfaceOptions.cacheTimeoutSeconds parameter will utilize the caching mechanism, significantly improving performance by reducing redundant requests.
*
* Note: This script will run faster on subsequent executions after the initial run due to the caching mechanism.
*/
const { LLMInterface } = require('../../src/index.js');
const { simplePrompt } = require('../../src/utils/defaults.js');
const { prettyHeader, prettyResult } = require('../../src/utils/utils.js');
require('dotenv').config({ path: '../../.env' });
// No need to include the base "cache-manager" module as it is auto-injected.
// Example: const cacheManager = require('cache-manager'); // This line is not needed.
// Include the storage mechanism dependencies you want to use.
// Example: const fsStore = require('cache-manager-fs-hash');
const fsStore = require('cache-manager-fs-hash');
// Setup your key and interface
const interfaceName = 'groq';
const apiKey = process.env.GROQ_API_KEY;
// Example description
const description = `This example demonstrates the basic usage of the "cache-manager" module with a filesystem storage mechanism.
To run this example, you first need to install the required modules by executing:
npm install [email protected] cache-manager-fs-hash dotenv
In this example, "cache-manager" is configured using the LLMInterface.configureCache() method. Subsequent calls to LLMInterface.sendMessage()
with the interfaceOptions.cacheTimeoutSeconds parameter will utilize the caching mechanism, significantly improving performance by reducing redundant requests.
To flush the cache you can run this example with the "--flush-cache" argument.
Note: This script will run faster on subsequent executions after the initial run due to the caching mechanism.`;
/**
* Main exampleUsage() function.
*/
async function exampleUsage() {
prettyHeader(
'Cache Manager Example',
description,
simplePrompt,
interfaceName,
);
LLMInterface.setApiKey(interfaceName, apiKey);
LLMInterface.configureCache({
cache: 'cache-manager',
config: {
store: fsStore,
options: {
path: '../../cache', // Path to the directory where cache files will be stored
ttl: 60 * 60, // Time to live in seconds (1 hour)
subdirs: true, // Create subdirectories to reduce the number of files per directory
zip: false, // Compress files to save space
},
},
});
const args = process.argv;
try {
console.time('Timer');
const response = await LLMInterface.sendMessage(
interfaceName,
simplePrompt,
{
max_tokens: 100,
},
{ cacheTimeoutSeconds: 86400 },
);
prettyResult(response.results);
console.timeEnd('Timer');
console.log();
} catch (error) {
console.error(error);
}
if (args.includes('--flush-cache')) {
console.log('Cache flushed.');
LLMInterface.flushCache();
}
}
exampleUsage();