-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path09-vector-search.ts
67 lines (56 loc) · 2.04 KB
/
09-vector-search.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
// This example demonstrates how to use embeddings to classify a text into
// a vector and search for similar vectors.
//
// We use a different model to generate embeddings for the query and the texts.
// See https://github.com/marketplace/models/azure-openai/text-embedding-3-small
// for more information about the model.
import process from "node:process";
import { OpenAI } from "openai";
import { LocalIndex } from "vectra";
const query = "fruit";
const texts = [
"The red fox jumped over the lazy dog.",
"A green apple a day keeps the doctor away.",
"The blue whale is the largest animal in the ocean.",
"Oranges are rich in vitamin C and taste sweet.",
"My cat loves playing with a yellow ball.",
"Bananas are a popular fruit among monkeys.",
"The black panther moves silently in the night.",
"The computer screen displayed a vibrant purple background.",
"Cherries are small, red, and very sweet.",
"The golden retriever is a friendly and loyal dog.",
];
const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});
// Create a vector database
const index = new LocalIndex(".vectordb");
// Only ingest the texts and generate vectors once
if (!(await index.isIndexCreated())) {
await index.createIndex();
// Generate vectors for the texts
const embeddings = await openai.embeddings.create({
model: "text-embedding-3-small",
input: texts,
});
// Insert the vectors into the database
for (let i = 0; i < texts.length; i++) {
await index.insertItem({
vector: embeddings.data[i].embedding,
metadata: { text: texts[i] },
});
}
}
// Transform the query into a vector
const queryEmbedding = await openai.embeddings.create({
model: "text-embedding-3-small",
input: query,
});
const vector = queryEmbedding.data[0].embedding;
// Search for similar vectors
const results = await index.queryItems(vector, 3);
console.log(`Top 3 matches for "${query}":`);
for (const result of results) {
console.log(`[score: ${result.score}] ${result.item.metadata.text}`);
}