-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
37 lines (32 loc) · 1.15 KB
/
utils.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
// utils.js
const fs = require('fs');
const encoder = new TextEncoder();
const RATE_LIMIT_DELAY = 100;
async function chunkAndEmbedText(filePath, openai, chunkSize = 8191) {
const text = fs.readFileSync(filePath, 'utf-8');
const encoded = encoder.encode(text);
const chunks = [];
for (let i = 0; i < encoded.length; i += chunkSize) {
const chunk = encoded.slice(i, i + chunkSize);
chunks.push(new TextDecoder().decode(chunk));
}
const embeddingPromises = chunks.map(async (chunk, index) => {
try {
// Introduce a delay before each request
await new Promise(resolve => setTimeout(resolve, index * RATE_LIMIT_DELAY));
const embeddingResponse = await openai.embeddings.create({
input: chunk,
model: "text-embedding-ada-002",
});
return {
text: chunk,
embedding: embeddingResponse.data[0].embedding,
};
} catch (error) {
console.error(error);
throw error;
}
});
return await Promise.all(embeddingPromises);
}
module.exports = { chunkAndEmbedText };