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

# AzureAIOpenAIApiChatModel 集成

> 使用 LangChain Python 集成 AzureAIOpenAIApiChatModel 聊天模型。

本指南将帮助您开始使用 `AzureAIOpenAIApiChatModel` [聊天模型](/oss/python/langchain/models).

The `AzureAIOpenAIApiChatModel` class uses the OpenAI-compatible API available in Azure AI Foundry. AI Foundry has several 聊天模型, including AzureOpenAI, Cohere, Llama, Phi-3/4, and DeepSeek-R1, among others. You can find information about their latest models and their costs, context windows, and supported input types in the [Azure docs](https://learn.microsoft.com/azure/ai-studio/how-to/model-catalog-overview).

## 概述

### 集成详情

| 类                           | 包                    | 可序列化 | [JS 支持](https://reference.langchain.com/javascript/langchain-openai/AzureChatOpenAI) |                                                 下载量                                                 |                                                版本                                                |
| :-------------------------- | :------------------- | :--: | :----------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------: |
| `AzureAIOpenAIApiChatModel` | `langchain-azure-ai` |   ✅  |                                           ✅                                          | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-azure-ai?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-azure-ai?style=flat-square\&label=%20) |

### 模型功能

| [Tool calling](/oss/python/langchain/tools) | [Structured output](/oss/python/langchain/structured-output) | [Image input](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [Token-level streaming](/oss/python/langchain/streaming/) | 原生异步 | [Token usage](/oss/python/langchain/models#token-usage) | [Logprobs](/oss/python/langchain/models#log-probabilities) |
| :-----------------------------------------: | :----------------------------------------------------------: | :------------------------------------------------------: | :--: | :--: | :-------------------------------------------------------: | :--: | :-----------------------------------------------------: | :--------------------------------------------------------: |
|                      ✅                      |                               ✅                              |                             ✅                            |   ❌  |   ❌  |                             ✅                             |   ✅  |                            ✅                            |                              ✅                             |

## 设置

要访问 `AzureAIOpenAIApiChatModel` models, you'll need to create an [Azure account](https://azure.microsoft.com/pricing/purchase-options/azure-account), get an API key, and install the `langchain-azure-ai` 集成包。

### 凭证

Head to the [Azure docs](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/develop/sdk-overview?tabs=sync\&pivots=programming-language-python) to see how to create your deployment 并生成 API 密钥。 Once your model is deployed, you click the 'get endpoint' button in AI Foundry. This will show you your endpoint and api key. 完成后，设置 environment variables:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if not os.getenv("AZURE_AI_PROJECT_ENDPOINT"):
    os.environ["AZURE_AI_PROJECT_ENDPOINT"] = getpass.getpass(
        "Enter your Azure AI project endpoint: "
    )
```

If you want to get automated tracing of your model calls, you can also set your [LangSmith](/langsmith/home) API key by uncommenting below:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("请输入您的 LangSmith API 密钥: ")
```

### 安装

LangChain 的 `AzureAIOpenAIApiChatModel` 集成位于 `langchain-azure-ai` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-azure-ai
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
from azure.identity import DefaultAzureCredential

llm = AzureAIOpenAIApiChatModel(
    model="gpt-4o",
    credential=DefaultAzureCredential(),
    temperature=0,
    max_tokens=None,
    max_retries=2,
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content="J'adore programmer.", additional_kwargs={}, response_metadata={'model': 'gpt-4o-2024-05-13', 'token_usage': {'input_tokens': 31, 'output_tokens': 4, 'total_tokens': 35}, 'finish_reason': 'stop'}, id='run-c082dffd-b1de-4b3f-943f-863836663ddb-0', usage_metadata={'input_tokens': 31, 'output_tokens': 4, 'total_tokens': 35})
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore programmer.
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/chat/azure_ai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
