-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathembeddings.js
57 lines (48 loc) · 1.71 KB
/
embeddings.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
/**
* @file examples/embeddings/embeddings.js
* @description This example demonstrates the usage of the LLMInterface.embeddings() method. Note that not all providers support embeddings, so it is important to check the provider documentation or use a failover mechanism.
*
* To run this example, you first need to install the required module by executing:
*
* npm install dotenv
*/
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 = 'huggingface';
const apiKey = process.env.HUGGINGFACE_API_KEY;
// Example description
const description = `This example demonstrates the basic usage of the LLMInterface.embeddings() method. Note that not all providers support embeddings, so it is important to check the provider documentation or use a failover mechanism.
To run this example, you first need to install the required modules by executing:
npm install dotenv
`;
/**
* Main exampleUsage() function.
*/
async function exampleUsage() {
prettyHeader(
'Embeddings Example',
description,
simplePrompt,
interfaceName,
true,
);
LLMInterface.setApiKey(interfaceName, apiKey);
try {
console.time('Timer');
response = await LLMInterface.embeddings(interfaceName, simplePrompt);
if (Array.isArray(response.results)) {
prettyResult(response.results);
} else {
console.error('Embeddings failed.');
}
console.log();
console.timeEnd('Timer');
console.log();
} catch (error) {
console.error(error);
}
}
exampleUsage();