Skip to main content

Class: AzureAISearchVectorStore<T>

Azure AI Search vector store.

Example

import { DefaultAzureCredential, getBearerTokenProvider} from "@azure/identity";
import {KnownAnalyzerNames, KnownVectorSearchAlgorithmKind } from "@azure/search-documents";

// 1- Setup Azure OpenAI
const azureADTokenProvider = getBearerTokenProvider(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default",
);

// IMPORTANT: You need to deploy your own embedding model as well as your own chat completion model
// NOTE: You can use whatever embedding model and language model that is supported in LlamaIndex
const azure = {
azureADTokenProvider,
deployment: process.env.AZURE_DEPLOYMENT_NAME,
};
Settings.llm = new OpenAI({ azure });
Settings.embedModel = new OpenAIEmbedding({
model: process.env.EMBEDDING_MODEL,
azure: {
...azure,
deployment: process.env.EMBEDDING_MODEL,
},
});

// ---------------------------------------------------------
// 2- Setup Azure AI Search
// Define env variables in .env file
// AZURE_AI_SEARCH_ENDPOINT=
// AZURE_AI_SEARCH_KEY=
// AZURE_OPENAI_ENDPOINT=
// EMBEDDING_MODEL=text-embedding-ada-002
// AZURE_DEPLOYMENT_NAME=gpt-4
// AZURE_API_VERSION=2024-09-01-preview

// Define index name
const indexName = "llamaindex-vector-store";

// ---------------------------------------------------------
// 3a- Create Index (if it does not exist)
// id: Edm.String
// chunk: Edm.String
// embedding: Collection(Edm.Single)
// metadata: Edm.String
// doc_id: Edm.String
// author: Edm.String
// theme: Edm.String
// director: Edm.String

// Define metadata fields with their respective configurations
const metadataFields = {
author: "author",
theme: ["theme", MetadataIndexFieldType.STRING],
director: "director",
};

// Define index parameters and vector store configuration
// Index validation:
// - IndexManagement.VALIDATE_INDEX: will validate before creating emnbedding index and will throw a runtime error if the index does not exist
// - IndexManagement.NO_VALIDATION: will try to access the index and will throw a runtime error if the index does not exist
// - IndexManagement.CREATE_IF_NOT_EXISTS: will create the index if it does not exist

const vectorStore = new AzureAISearchVectorStore({
filterableMetadataFieldKeys:
metadataFields as unknown as FilterableMetadataFieldKeysType,
indexName,
indexManagement: IndexManagement.CREATE_IF_NOT_EXISTS,
idFieldKey: "id",
chunkFieldKey: "chunk",
embeddingFieldKey: "embedding",
metadataStringFieldKey: "metadata",
docIdFieldKey: "doc_id",
embeddingDimensionality: 1536,
hiddenFieldKeys: ["embedding"],
languageAnalyzer: KnownAnalyzerNames.EnLucene,
// store vectors on disk
vectorAlgorithmType: KnownVectorSearchAlgorithmKind.ExhaustiveKnn,

// Optional: Set to "scalar" or "binary" if using HNSW
compressionType: KnownVectorSearchCompressionKind.BinaryQuantization,
});

// ---------------------------------------------------------
// 3a- Loading documents
// Load the documents stored in the data/paul_graham/ using the SimpleDirectoryReader
// NOTE: You can use whatever reader that is supported in LlamaIndex

// Load documents using a directory reader
const documents = await new SimpleDirectoryReader().loadData(
"data/paul_graham/",
);
const storageContext = await storageContextFromDefaults({ vectorStore });

// Create index from documents with the specified storage context
const index = await VectorStoreIndex.fromDocuments(documents, {
storageContext,
docStoreStrategy: DocStoreStrategy.UPSERTS,
});

const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What did the author do growing up?",
similarityTopK: 3,
} as any);
console.log({ response });

## Extends

- [`BaseVectorStore`](BaseVectorStore.md)

## Type Parameters

**T** *extends* `R`

## Constructors

### new AzureAISearchVectorStore()

> **new AzureAISearchVectorStore**\<`T`\>(`options`): [`AzureAISearchVectorStore`](AzureAISearchVectorStore.md)\<`T`\>

#### Parameters

**options**: [`AzureAISearchOptions`](../interfaces/AzureAISearchOptions.md)\<`T`\> & [`VectorStoreBaseParams`](../type-aliases/VectorStoreBaseParams.md)

#### Returns

[`AzureAISearchVectorStore`](AzureAISearchVectorStore.md)\<`T`\>

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`constructor`](BaseVectorStore.md#constructors)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:304](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L304)

## Properties

### embedModel

> **embedModel**: [`BaseEmbedding`](BaseEmbedding.md)

#### Inherited from

[`BaseVectorStore`](BaseVectorStore.md).[`embedModel`](BaseVectorStore.md#embedmodel)

#### Defined in

[packages/llamaindex/src/vector-store/types.ts:92](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/types.ts#L92)

***

### flatMetadata

> **flatMetadata**: `boolean` = `true`

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:291](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L291)

***

### isEmbeddingQuery?

> `optional` **isEmbeddingQuery**: `boolean`

#### Inherited from

[`BaseVectorStore`](BaseVectorStore.md).[`isEmbeddingQuery`](BaseVectorStore.md#isembeddingquery)

#### Defined in

[packages/llamaindex/src/vector-store/types.ts:94](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/types.ts#L94)

***

### searchClient

> **searchClient**: `undefined` \| `SearchClient`\<`T`\>

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:279](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L279)

***

### storesText

> **storesText**: `boolean` = `true`

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`storesText`](BaseVectorStore.md#storestext)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:278](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L278)

## Methods

### add()

> **add**(`nodes`): `Promise`\<`string`[]\>

Add nodes to index associated with the configured search client.

#### Parameters

**nodes**: [`BaseNode`](BaseNode.md)\<[`Metadata`](../type-aliases/Metadata.md)\>[]

List of nodes with embeddings to add to the index

#### Returns

`Promise`\<`string`[]\>

List of node IDs that were added to the index

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`add`](BaseVectorStore.md#add)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:1047](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L1047)

***

### client()

> **client**(): `undefined` \| `SearchClient`\<`T`\>

Get search client

#### Returns

`undefined` \| `SearchClient`\<`T`\>

Azure AI Search client. See SearchClient

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`client`](BaseVectorStore.md#client)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:1038](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L1038)

***

### delete()

> **delete**(`refDocId`): `Promise`\<`void`\>

Delete documents from the AI Search Index with docIdFieldKey (doc_id) field equal to refDocId.

#### Parameters

**refDocId**: `string`

The reference document ID to delete from the index

#### Returns

`Promise`\<`void`\>

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`delete`](BaseVectorStore.md#delete)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:1112](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L1112)

***

### getNodes()

> **getNodes**(`nodeIds`?, `filters`?, `limit`?): `Promise`\<[`BaseNode`](BaseNode.md)\<[`Metadata`](../type-aliases/Metadata.md)\>[]\>

Get nodes asynchronously from the Azure AI Search index.

#### Parameters

**nodeIds?**: `string`[]

List of node IDs to retrieve from the index

**filters?**: [`MetadataFilters`](../interfaces/MetadataFilters.md)

Metadata filters to apply to the search

**limit?**: `number`

Maximum number of nodes to retrieve

#### Returns

`Promise`\<[`BaseNode`](BaseNode.md)\<[`Metadata`](../type-aliases/Metadata.md)\>[]\>

List of nodes retrieved from the index

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:1159](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L1159)

***

### query()

> **query**(`query`): `Promise`\<[`VectorStoreQueryResult`](../interfaces/VectorStoreQueryResult.md)\>

#### Parameters

**query**: [`VectorStoreQuery`](../interfaces/VectorStoreQuery.md) & `object`

#### Returns

`Promise`\<[`VectorStoreQueryResult`](../interfaces/VectorStoreQueryResult.md)\>

#### Overrides

[`BaseVectorStore`](BaseVectorStore.md).[`query`](BaseVectorStore.md#query)

#### Defined in

[packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts:1212](https://github.com/run-llama/LlamaIndexTS/blob/8e7c154e8b2218c0ef334e390b74bab8db9d3ab3/packages/llamaindex/src/vector-store/azure/AzureAISearchVectorStore.ts#L1212)