-
Notifications
You must be signed in to change notification settings - Fork 287
/
azure-search-vector-sample.js
371 lines (321 loc) · 11.3 KB
/
azure-search-vector-sample.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
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
const dotenv = require("dotenv");
const fs = require("fs");
const { SearchIndexClient, SearchClient } = require("@azure/search-documents");
const { DefaultAzureCredential } = require("@azure/identity")
const { AzureKeyCredential } = require("@azure/core-auth")
const { OpenAIClient } = require("@azure/openai");
const { promisify } = require('util');
const { Option, program } = require('commander');
async function main() {
program
.option('-e, --embed', 'Recreate embeddings in text-sample.json')
.option('-u, --upload', 'Upload embeddings and data in text-sample.json to the search index')
.option('-q, --query <text>', 'Text of query to issue to search, if any')
.addOption(new Option('-k, --query-kind <kind>', 'Kind of query to issue. Defaults to hybrid').default('hybrid').choices(['text', 'vector', 'hybrid']))
.option('-c, --category-filter <category>', 'Category to filter results to')
.option('-t, --include-title', 'Search over the title field as well as the content field')
.option('--no-semantic-reranker', 'Do not use semantic reranker. Defaults to false')
.parse();
const options = program.opts()
const defaultCredential = new DefaultAzureCredential();
// Load environment variables from .env file
dotenv.config({ path: "../.env" });
// Generate document embeddings
if (options.embed) {
try {
await generateDocumentEmbeddings(defaultCredential);
} catch (err) {
console.error(
`Failed to generate embeddings: ${err.message}`
);
return;
}
}
// Upload documents to Azure AI Search
if (options.upload) {
// Create Azure AI Search index
try {
await createSearchIndex(defaultCredential);
} catch (err) {
console.error(`Failed to create index: ${err.message}`);
return;
}
try {
await uploadDocuments(defaultCredential);
} catch (err) {
console.error(
`Failed to upload documents to search index: ${err.message}`
);
return;
}
}
// Query Azure AI Search
if (options.query) {
try {
await queryDocuments(defaultCredential, options.query, options.queryKind, options.categoryFilter, options.includeTitle, options.semanticReranker);
} catch (err) {
console.error(
`Failed to issue query to search index: ${err.message}`
);
return;
}
}
}
function createSearchClient(defaultCredential) {
const searchServiceEndpoint = process.env.AZURE_SEARCH_SERVICE_ENDPOINT;
const searchServiceApiKey = process.env.AZURE_SEARCH_ADMIN_KEY;
const searchIndexName = process.env.AZURE_SEARCH_INDEX;
let credential = !searchServiceApiKey || searchServiceApiKey.trim() === '' ?
defaultCredential : new AzureKeyCredential(searchServiceApiKey);
return new SearchClient(
searchServiceEndpoint,
searchIndexName,
credential
);
}
function createOpenAiClient(defaultCredential) {
const openAiEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
const openAiKey = process.env.AZURE_OPENAI_KEY;
let credential = !openAiKey || openAiKey.trim() == '' ?
defaultCredential : new AzureKeyCredential(openAiKey);
return new OpenAIClient(openAiEndpoint, credential);
}
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
async function generateDocumentEmbeddings(defaultCredential) {
console.log("Reading data/text-sample.json...");
const fileData = await readFileAsync("../data/text-sample.json", "utf-8");
let data = JSON.parse(fileData);
console.log("Generating embeddings with Azure OpenAI...");
const client = createOpenAiClient(defaultCredential);
const openAiDeployment = process.env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT;
const titles = data.map(item => item["title"]);
const content = data.map(item => item["content"]);
const titleEmbeddings = await client.getEmbeddings(openAiDeployment, titles);
const contentEmbeddings = await client.getEmbeddings(openAiDeployment, content);
for (let i = 0; i < data.length; i++) {
data[i]["titleVector"] = titleEmbeddings.data[i].embedding;
data[i]["contentVector"] = contentEmbeddings.data[i].embedding;
}
await writeFileAsync("../data/text-sample.json", JSON.stringify(data, null, 2));
console.log("Wrote embeddings to data/text-sample.json");
}
async function createSearchIndex(defaultCredential) {
const searchServiceEndpoint = process.env.AZURE_SEARCH_SERVICE_ENDPOINT;
const searchServiceApiKey = process.env.AZURE_SEARCH_ADMIN_KEY;
const searchIndexName = process.env.AZURE_SEARCH_INDEX;
const embeddingDimensions = parseInt(process.env.AZURE_OPENAI_EMBEDDING_DIMENSIONS);
let vectorSearchDimensions = isNaN(embeddingDimensions) || embeddingDimensions <= 0 ?
1536 : embeddingDimensions;
let credential = !searchServiceApiKey || searchServiceApiKey.trim() === '' ?
defaultCredential : new AzureKeyCredential(searchServiceApiKey);
const indexClient = new SearchIndexClient(
searchServiceEndpoint,
credential
);
const index = {
name: searchIndexName,
fields: [
{
name: "id",
type: "Edm.String",
key: true,
sortable: true,
filterable: true,
facetable: true,
},
{ name: "title", type: "Edm.String", searchable: true },
{ name: "content", type: "Edm.String", searchable: true },
{
name: "category",
type: "Edm.String",
filterable: true,
searchable: true,
},
{
name: "titleVector",
type: "Collection(Edm.Single)",
searchable: true,
vectorSearchDimensions: vectorSearchDimensions,
vectorSearchProfileName: "myHnswProfile",
},
{
name: "contentVector",
type: "Collection(Edm.Single)",
searchable: true,
vectorSearchDimensions: vectorSearchDimensions,
vectorSearchProfileName: "myHnswProfile",
},
],
vectorSearch: {
algorithms: [{ name: "myHnswAlgorithm", kind: "hnsw" }],
profiles: [
{
name: "myHnswProfile",
algorithmConfigurationName: "myHnswAlgorithm",
},
],
},
semanticSearch: {
configurations: [
{
name: "my-semantic-config",
prioritizedFields: {
contentFields: [{ name: "content" }],
keywordsFields: [{ name: "category" }],
titleField: {
name: "title",
},
},
},
],
},
};
console.log("Creating index...");
await indexClient.createOrUpdateIndex(index);
}
async function uploadDocuments(defaultCredential) {
console.log("Reading data/text-sample.json...");
const fileData = await readFileAsync("../data/text-sample.json", "utf-8");
let data = JSON.parse(fileData);
const searchClient = createSearchClient(defaultCredential);
console.log("Uploading documents to the index...");
// Upload 1 document at a time
for (let i = 0; i < data.length; i++) {
await searchClient.uploadDocuments([data[i]]);
}
console.log("Finished uploading documents");
}
async function queryDocuments(defaultCredential, query, queryKind, categoryFilter, includeTitle, semanticReranker) {
const searchClient = createSearchClient(defaultCredential);
const openAiClient = createOpenAiClient(defaultCredential);
const openAiDeployment = process.env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT;
let options = {
select: ["title", "content", "category"],
top: 3
};
if (queryKind == "vector" || queryKind == "hybrid") {
let embeddingResponse = await openAiClient.getEmbeddings(openAiDeployment, [query]);
let embedding = embeddingResponse.data[0].embedding;
let vectorFields = includeTitle ? [ "contentVector", "titleVector" ] : [ "contentVector" ];
options["vectorSearchOptions"] = {
queries: [
{
kind: "vector",
vector: embedding,
kNearestNeighborsCount: 50,
fields: vectorFields
}
]
}
}
if (semanticReranker) {
if (queryKind == "text" || queryKind == "hybrid") {
options["queryType"] = "semantic";
} else {
options["semanticQuery"] = query;
}
options["semanticSearchOptions"] = {
answers: {
answerType: "extractive"
},
captions:{
captionType: "extractive"
},
configurationName: "my-semantic-config",
}
}
if (categoryFilter) {
options["filter"] = `category eq '${categoryFilter}'`;
}
const searchText = queryKind == "text" || queryKind == "hybrid" ? query : "*";
const response = await searchClient.search(searchText, options);
if (semanticReranker) {
for await (const answer of response.answers) {
if (answer.highlights) {
console.log(`Semantic answer: ${answer.highlights}`);
} else {
console.log(`Semantic answer: ${answer.text}`);
}
console.log(`Semantic answer score: ${answer.score}\n`);
}
}
for await (const result of response.results) {
console.log('----');
console.log(`Title: ${result.document.title}`);
console.log(`Score: ${result.score}`);
if (semanticReranker) {
console.log(`Reranker Score: ${result.rerankerScore}`); // Reranker score is the semantic score
}
console.log(`Content: ${result.document.content}`);
console.log(`Category: ${result.document.category}`);
if (result.captions) {
const caption = result.captions[0];
if (caption.highlights) {
console.log(`Caption: ${caption.highlights}`);
} else {
console.log(`Caption: ${caption.text}`);
}
}
console.log('----');
console.log(`\n`);
}
}
async function doSemanticHybridSearch() {
const searchServiceEndpoint = process.env.AZURE_SEARCH_ENDPOINT;
const searchServiceApiKey = process.env.AZURE_SEARCH_ADMIN_KEY;
const searchIndexName = process.env.AZURE_SEARCH_INDEX_NAME;
const searchClient = new SearchClient(
searchServiceEndpoint,
searchIndexName,
new AzureKeyCredential(searchServiceApiKey)
);
const query = "what is azure sarch?";
const response = await searchClient.search(query, {
vectorQueries: [{
kind: "vector",
vector: await generateEmbeddings(query),
kNearestNeighborsCount: 3,
fields: ["contentVector"],
}],
select: ["title", "content", "category"],
queryType: "semantic",
top: 3,
semanticSearchOptions: {
answers: {
answerType: "extractive",
count: 3
},
captions:{
captionType: "extractive",
count: 3
},
configurationName: "my-semantic-config",
}
});
console.log(`\nSemantic Hybrid search results:`);
for await (const answer of response.answers) {
if (answer.highlights) {
console.log(`Semantic answer: ${answer.highlights}`);
} else {
console.log(`Semantic answer: ${answer.text}`);
}
console.log(`Semantic answer score: ${answer.score}\n`);
}
for await (const result of response.results) {
console.log(`Title: ${result.document.title}`);
console.log(`Reranker Score: ${result.rerankerScore}`); // Reranker score is the semantic score
console.log(`Content: ${result.document.content}`);
console.log(`Category: ${result.document.category}`);
if (result.captions) {
const caption = result.captions[0];
if (caption.highlights) {
console.log(`Caption: ${caption.highlights}`);
} else {
console.log(`Caption: ${caption.text}`);
}
}
console.log(`\n`);
}
}
main();