-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedding.ts
432 lines (351 loc) · 10.9 KB
/
embedding.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import { neon } from "@neondatabase/serverless";
import OpenAI from "openai";
import dotEnv from "dotenv";
import fs from "fs/promises";
import path from "path";
import { Tiktoken } from "@dqbd/tiktoken";
import cl100k_base from "@dqbd/tiktoken/encoders/cl100k_base.json";
dotEnv.config();
const dataBaseUrl = process.env.DATABASE_URL;
const openAiKey = process.env.OPENAI_KEY;
if (!dataBaseUrl || !openAiKey) {
throw new Error("missing environment variables");
}
const openAi = new OpenAI({
apiKey: openAiKey,
});
const sql = neon(dataBaseUrl);
const encoding = new Tiktoken(
cl100k_base.bpe_ranks,
cl100k_base.special_tokens,
cl100k_base.pat_str
);
let i = 0;
// -----------
// Step 1
// Create array with texts and fileName
// -----------
type TextFile = {
filePath: string;
text: string;
};
async function processFiles(folder: string): Promise<TextFile[]> {
const files: TextFile[] = [];
const folderPath = `./data/${folder}`;
const entries = await fs.readdir(folderPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(folderPath, entry.name); //entry.name = "fileName"
if (entry.isDirectory()) {
continue;
}
const text = await fs.readFile(fullPath, "utf-8");
files.push({
filePath: entry.name.replace(".txt", ""),
text,
});
}
return files;
}
// -----------
// Step 2
// tokenized all texts
// -----------
type TextFileToken = TextFile & { token: Uint32Array; tokenLength: number };
const tiktokenizer = async (files: TextFile[]): Promise<TextFileToken[]> => {
const textFileTokens: TextFileToken[] = [];
for (const file of files) {
const token: Uint32Array = encoding.encode(file.text);
textFileTokens.push({
...file,
token: token,
tokenLength: token.length,
});
}
return textFileTokens;
};
// -----------
// Step 3
// shorten all texts"
// -----------
const MAX_TOKENS = 1500;
const MAX_TOKENS_PER_CHUNK = 3600;
interface TextChunk extends TextFile {
tokenLength: number;
}
interface TextChunk extends TextFile {
tokenLength: number;
}
async function splitTextToMany(text: TextFile): Promise<TextChunk[]> {
const tagRegex = /\[sous-titre\]/g; // Expression régulière pour trouver les balises
const filePath = text.filePath;
// Fonction pour découper le texte en morceaux plus petits
function splitText(text: string): TextChunk[] {
const chunks: TextChunk[] = [];
let startIdx = 0;
// Rechercher les balises [sous-titre] et découper juste avant
let match;
while ((match = tagRegex.exec(text)) !== null) {
const endIdx = match.index;
const chunkText = text.substring(startIdx, endIdx).trim();
const chunkTokenLength = encoding.encode(chunkText).length;
if (chunkTokenLength > 0) {
chunks.push({
filePath: filePath,
text: chunkText,
tokenLength: chunkTokenLength,
});
}
startIdx = endIdx;
}
// Ajouter le dernier morceau
const lastChunkText = text.substring(startIdx).trim();
const lastChunkTokenLength = encoding.encode(lastChunkText).length;
if (lastChunkTokenLength > 0) {
chunks.push({
filePath: filePath,
text: lastChunkText,
tokenLength: lastChunkTokenLength,
});
}
return chunks;
}
// Découper le texte initial en morceaux plus petits
let chunks: TextChunk[] = splitText(text.text);
const totalTokenLength = chunks.reduce(
(acc, chunk) => acc + chunk.tokenLength,
0
);
// Vérifier si le texte initial est inférieur à la limite de tokens globale
if (totalTokenLength <= MAX_TOKENS) {
return [
{
filePath: filePath,
text: text.text,
tokenLength: totalTokenLength,
},
];
}
const result: TextChunk[] = [];
// Boucle pour traiter chaque chunk
for (const chunk of chunks) {
// Vérifier si le chunk dépasse la limite de tokens par morceau
if (chunk.tokenLength > MAX_TOKENS_PER_CHUNK) {
// Diviser le chunk en deux parties égales
const middleIndex = Math.floor(chunk.text.length / 2);
const firstHalfText = chunk.text.substring(0, middleIndex).trim();
const secondHalfText = chunk.text.substring(middleIndex).trim();
const firstHalfTokenLength = encoding.encode(firstHalfText).length;
const secondHalfTokenLength = encoding.encode(secondHalfText).length;
// Ajouter les deux parties à la liste des résultats
result.push({
filePath: chunk.filePath,
text: firstHalfText,
tokenLength: firstHalfTokenLength,
});
result.push({
filePath: chunk.filePath,
text: secondHalfText,
tokenLength: secondHalfTokenLength,
});
} else {
// Ajouter le chunk tel quel au résultat
result.push(chunk);
}
}
return result;
}
async function splitTexts(texts: TextFileToken[]): Promise<TextFile[]> {
const shortened: TextFile[] = [];
for (const file of texts) {
if (file.token.length > MAX_TOKENS) {
const chunks = await splitTextToMany(file);
shortened.push(...chunks);
} else {
shortened.push(file);
}
}
return shortened;
}
// -----------
// 4 embed all texts with openai
// -----------
type TextFileEmbedding = TextFile & { embedding: number[] };
async function processEmbeddings(
texts: TextFileToken[]
): Promise<TextFileEmbedding[]> {
const embededs: TextFileEmbedding[] = [];
let i = 0;
for await (const file of texts) {
const result = await openAi.embeddings.create({
model: "text-embedding-3-small",
input: file.text,
encoding_format: "float",
});
const embeddings = result.data[0].embedding;
embededs.push({
...file,
embedding: embeddings,
});
i += 1;
console.log(
"📀 embedding in progress ...",
file.filePath,
i,
" / ",
texts.length
);
}
console.log("🛟 embeding done");
return embededs;
}
// -----------
// 5 save our embeddings data in the database
// -----------
async function saveToDatabse(texts: TextFileEmbedding[]) {
let totalSave = 0;
for await (const row of texts) {
let { text, filePath, embedding } = row;
// for model text-embedding-3-small and ada v2
const vectorSize = 1536; // size of vector configured in our database.
// create a vector of 1536/3072 padded with embeddings data and 0
const vectorPadded = new Array(vectorSize).fill(0);
vectorPadded.splice(0, embedding.length, ...embedding);
const tokens = encoding.encode(text);
const tokensLength = tokens.length;
const insertQuery = `
INSERT INTO documents (text, n_tokens, file_path, embeddings) values ($1, $2, $3, $4)
`;
await sql(insertQuery, [
text,
tokensLength,
filePath,
JSON.stringify(vectorPadded),
]);
totalSave++;
console.log(
"📌 Saved to database",
filePath,
"total saved",
totalSave,
" / ",
texts.length
);
}
}
async function main() {
const FOLDER = "nextjs";
// Step 1 Create array with texts and fileName and save it to a json file (texts.json)
const texts = await cache_withFile(
() => processFiles(FOLDER),
"./processed/texts.json"
);
// Step 2 tokenized all texts and save it to a json file (textsTokens.json)
const textTokens: TextFileToken[] = await cache_withFile(
() => tiktokenizer(texts),
"./processed/textsTokens.json"
);
// Step 3 shorten all texts and save it to a json file (shortenedTexts.json) To contains max 500 tokens by text
const textsTokensShortened: TextFileToken[] = await cache_withFile(
() => splitTexts(textTokens),
"processed/textsTokensShortened.json"
);
// Display stats optional
// displayTokenLengthStats(textTokens);
// displayTokenLengthStats(textsTokensShortened);
// Step 4 embed all texts
const textsEmbeddings: TextFileEmbedding[] = await cache_withFile(
() => processEmbeddings(textsTokensShortened),
"processed/textsEmbeddings.json"
);
// Step 5 save our embeddings data in the database
await saveToDatabse(textsEmbeddings);
}
main();
// -----------
// Utils
// -----------
interface TextObject {
tokenLength: number;
filePath: string;
}
function displayTokenLengthStats(objects: TextObject[]): void {
let under500: number = 0;
let between500And1000: number = 0;
let between1000And1500: number = 0;
let between1500And2000: number = 0;
let between2000And2500: number = 0;
let between2500And3700: number = 0;
let above3700: number = 0;
let maxToken: number = 0;
let tokenstotal = 0;
objects.forEach((t) => (tokenstotal += t.tokenLength));
console.log("total tokens", tokenstotal);
objects.forEach((obj) => {
const tokenLength = obj.tokenLength;
if (tokenLength > maxToken) {
maxToken = tokenLength;
}
if (tokenLength < 500) {
under500++;
} else if (tokenLength >= 500 && tokenLength < 1000) {
between500And1000++;
} else if (tokenLength >= 1000 && tokenLength < 1500) {
between1000And1500++;
} else if (tokenLength >= 1500 && tokenLength < 2000) {
between1500And2000++;
} else if (tokenLength >= 2000 && tokenLength < 2500) {
between2000And2500++;
} else if (tokenLength >= 2500 && tokenLength < 3700) {
between2500And3700++;
} else {
console.log(obj.filePath, tokenLength);
above3700++;
}
});
console.log("Statistiques sur le nombre de tokens par objet :");
console.log(`- Nombre d'objets avec moins de 500 tokens : ${under500}`);
console.log(
`- Nombre d'objets avec 500 à 1000 tokens : ${between500And1000}`
);
console.log(
`- Nombre d'objets avec 1000 à 1500 tokens : ${between1000And1500}`
);
console.log(
`- Nombre d'objets avec 1500 à 2000 tokens : ${between1500And2000}`
);
console.log(
`- Nombre d'objets avec 2000 à 2500 tokens : ${between2000And2500}`
);
console.log(
`- Nombre d'objets avec 2500 à 3700 tokens : ${between2500And3700}`
);
console.log(`- Nombre d'objets avec plus de 3700 tokens : ${above3700}`);
console.log(`- Nombre maximal de tokens : ${maxToken}`);
}
async function cache_withFile<
T extends (TextFile | TextFileToken | TextFileEmbedding)[]
>(func: () => Promise<T>, filePath: string) {
try {
await fs.access(filePath);
const fileData = await fs.readFile(filePath, "utf-8");
console.log("🛟 using Cache file");
const parsedData = JSON.parse(fileData).map((item: any) => {
if (item.token && Array.isArray(item.token)) {
return { ...item, token: new Uint32Array(item.token) };
}
return item;
});
return parsedData;
} catch {
const data = await func();
console.log("🚀 writing cache file");
const dataToWrite = data.map((item: TextFile | TextFileToken) => {
if ("token" in item && item.token instanceof Uint32Array) {
return { ...item, token: Array.from(item.token) };
}
return item;
});
await fs.writeFile(filePath, JSON.stringify(dataToWrite));
return data;
}
}