> ## Documentation Index
> Fetch the complete documentation index at: https://llm-tools.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure OpenAI

In order to be able to use OpenAI models on Azure, they first need to be deployed.
Please refer to [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/) on how to deploy a model on Azure.

Once these models are deployed, using Azure OpenAI is easy to do. Just follow these steps -

* Set the following environment variables -

```bash theme={null}
AZURE_OPENAI_API_INSTANCE_NAME=<YOUR_INSTANCE_NAME>
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=<YOUR_EMBEDDINGS_DEPLOYMENT_NAME>
AZURE_OPENAI_API_VERSION="2024-02-01" #or a newer version
AZURE_OPENAI_API_KEY=<YOUR_KEY>
```

## Install OpenAI addon

```bash theme={null}
npm install @llm-tools/embedjs-openai
```

## Usage

```ts theme={null}
import { RAGApplicationBuilder } from '@llm-tools/embedjs';
import { AzureOpenAiEmbeddings } from '@llm-tools/embedjs-openai';
import { HNSWDb } from '@llm-tools/embedjs-hnswlib';

const app = await new RAGApplicationBuilder()
.setEmbeddingModel(new AzureOpenAiEmbeddings({
    model: 'text-embedding-3-large'
}))
```

<Note>
  The following embedding models do not require you to pass a dimension parameter -

  * `text-embedding-3-large`
  * `text-embedding-3-small`
  * `text-embedding-ada-002`

  All other models require you provide one.
</Note>

<Note>
  To use Azure Managed Identity, you can use something similar to the following code snippet -

  ```ts theme={null}
  import {
    DefaultAzureCredential,
    getBearerTokenProvider,
  } from "@azure/identity";
  import { AzureOpenAiEmbeddings } from '@llm-tools/embedjs-openai';

  const credentials = new DefaultAzureCredential();
  const azureADTokenProvider = getBearerTokenProvider(
    credentials,
    "https://cognitiveservices.azure.com/.default"
  );

  const app = await new RAGApplicationBuilder()
  .setEmbeddingModel(new AzureOpenAiEmbeddings({
      azureADTokenProvider,
      azureOpenAIApiInstanceName: "<your_instance_name>",
      azureOpenAIApiEmbeddingsDeploymentName: "<your_embeddings_deployment_name>",
      azureOpenAIApiVersion: "<api_version>",
  }))
  ```
</Note>
