-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimple-cache.js
73 lines (58 loc) · 2.21 KB
/
simple-cache.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
/**
* @file examples/caching/simple-cache.js
* @description This example demonstrates the usage of the SimpleCache for caching API responses.
*
* To run this example, you first need to install the required module by executing:
*
* npm install dotenv
*
* SimpleCache is the default cache engine and does not require any additional setup. To use it, simply specify an
* interfaceOptions.cacheTimeoutSeconds value. Subsequent runs of this script will be faster after the initial execution 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' });
// Setup your key and interface
const interfaceName = 'groq';
const apiKey = process.env.GROQ_API_KEY;
const args = process.argv;
const description = `This example demonstrates the usage of the SimpleCache for caching API responses. SimpleCache is the default cache engine and does not require any additional setup. To use it, simply specify an interfaceOptions.cacheTimeoutSeconds value. Subsequent runs of this script will be faster after the initial execution due to the caching mechanism.
To run this example, you first need to install the required modules by executing:
npm install dotenv
To flush the cache you can run this example with the "--flush-cache" argument.`;
/**
* Main exampleUsage() function.
*/
async function exampleUsage() {
prettyHeader(
'Simple Cache (Default Cache Engine) Example',
description,
simplePrompt,
interfaceName,
);
LLMInterface.setApiKey(interfaceName, apiKey);
LLMInterface.configureCache();
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();