-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.ts
More file actions
40 lines (35 loc) · 1.26 KB
/
Copy pathembed.ts
File metadata and controls
40 lines (35 loc) · 1.26 KB
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
/**
* Génère des embeddings via l'API Voyage AI (recommandée par Anthropic
* pour le RAG). Un compte gratuit suffit pour tester : https://voyageai.com
*
* Vous pouvez remplacer ce fichier par n'importe quel autre fournisseur
* d'embeddings (OpenAI, Cohere, un modèle local...) — le reste du code
* ne dépend que de la fonction embed() ci-dessous.
*/
const VOYAGE_API_KEY = process.env.VOYAGE_API_KEY;
const VOYAGE_URL = "https://api.voyageai.com/v1/embeddings";
export async function embed(
texts: string[],
inputType: "document" | "query"
): Promise<number[][]> {
if (!VOYAGE_API_KEY) {
throw new Error("VOYAGE_API_KEY manquant dans .env");
}
const res = await fetch(VOYAGE_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${VOYAGE_API_KEY}`,
},
body: JSON.stringify({
input: texts,
model: "voyage-3.5", // modèle généraliste, bon rapport qualité/prix
input_type: inputType, // "document" à l'ingestion, "query" à la recherche
}),
});
if (!res.ok) {
throw new Error(`Voyage API error ${res.status}: ${await res.text()}`);
}
const data = (await res.json()) as { data: { embedding: number[] }[] };
return data.data.map((d) => d.embedding);
}