This integration package allows LlamaIndex to utilize Azure OpenAI's Large Language Model (LLM) services. It provides a robust way to connect to and interact with models like GPT-3.5 and GPT-4 deployed on Azure, leveraging LlamaIndex's indexing and querying capabilities. The current version is 0.5.3, with its release cadence tied to updates in the core LlamaIndex library and Azure OpenAI API changes.
pip install llama-index-llms-azure-openaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the `AzureOpenAI` LLM and use it for a simple completion task. It highlights the required environment variables for Azure OpenAI authentication and configuration, as well as how to set the LLM globally using `Settings` (LlamaIndex v0.10.0+ pattern) or directly interact with the instance.
Migrate from `ServiceContext(llm=...)` to `from llama_index.core import Settings; Settings.llm = AzureOpenAI(...)`. For specific components, pass the `llm` instance directly.
Ensure `model` is set to the exact deployment name as configured in your Azure OpenAI Studio. Also verify `azure_endpoint` is the correct resource endpoint URL and `api_version` matches your deployment's supported version.
Always use a current and valid `api_version` supported by your Azure OpenAI deployment. Consult Azure's documentation for the latest recommended stable API version. Commonly '2023-05-15' or newer 'preview' versions are used.
Ensure that the `AZURE_OPENAI_API_KEY` (or `OPENAI_API_KEY`), `AZURE_OPENAI_ENDPOINT` (or `OPENAI_API_BASE`), and `OPENAI_API_VERSION` environment variables are correctly set, or pass these parameters directly to the `AzureOpenAI` constructor.
Example:
```python
import os
from llama_index.llms.azure_openai import AzureOpenAI
os.environ["OPENAI_API_KEY"] = "<your-api-key>"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<your-resource-name>.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"
# Or pass directly
llm = AzureOpenAI(
model="gpt-35-turbo",
deployment_name="my-gpt-deployment", # The 'engine' or deployment name
api_key=os.environ["OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version=os.environ["OPENAI_API_VERSION"],
)
```Verify the `deployment_name` (or `engine`) parameter exactly matches the name of your deployed model in Azure OpenAI Studio. Ensure you have deployed both a completion model (for LLMs) and an embedding model (for `AzureOpenAIEmbedding`) if both are used in your application.
Example:
```python
from llama_index.llms.azure_openai import AzureOpenAI
llm = AzureOpenAI(
model="gpt-35-turbo",
deployment_name="my-gpt-deployment", # Must match your Azure deployment name
# ... other parameters
)
```Carefully check the `azure_endpoint` (or `AZURE_OPENAI_ENDPOINT`) for any typos, ensuring it is in the correct format, such as `https://YOUR_RESOURCE_NAME.openai.azure.com/`. Confirm that your Azure OpenAI service is active and the network allows connections to the endpoint. Review related authentication and deployment settings.
Update your `llama-index` and `openai` packages to their latest versions to ensure compatibility with newly released models. Use `pip install -U llama-index llama-index-llms-azure-openai openai`. If the model is very new, you might need to wait for a subsequent library update. If it's a custom deployment, ensure the `deployment_name` is correct.