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

# Microsoft Foundry 聊天模型集成

> 使用 LangChain Python 集成 AzureAIOpenAIApiChatModel 对话模型。

这将帮助您入门 `AzureAIOpenAIApiChatModel` [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。

`AzureAIOpenAIApiChatModel` 类使用 Azure AI Foundry 中提供的 OpenAI 兼容 API。AI Foundry 包含多个聊天模型，包括 AzureOpenAI、Cohere、Llama、Phi-3/4 和 DeepSeek-R1 等。您可以在 [Azure 文档](https://learn.microsoft.com/azure/ai-studio/how-to/model-catalog-overview) 中查找最新模型信息、成本、上下文窗口和输入类型。

## 概述

### 集成细节

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

### 模型特性

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token 级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md) | 原生异步 | [Token 使用量](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [对数概率](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |

## 配置

要访问 `AzureAIOpenAIApiChatModel` 模型，您需要创建 [Azure 账户](https://azure.microsoft.com/pricing/purchase-options/azure-account)、获取 API 密钥并安装 `langchain-azure-ai` 集成包。

### 凭据

前往 [Azure 文档](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/develop/sdk-overview?tabs=sync\&pivots=programming-language-python) 查看如何创建部署并生成 API 密钥。模型部署后，您在 AI Foundry 中点击 '获取端点' 按钮，这将显示您的端点和 API 密钥。完成这些设置后，配置以下环境变量：

```python theme={null}
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: "
    )
```

如果需要启用模型调用的自动化追踪，您还可以通过取消注释以下内容设置 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

### 安装

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

```python theme={null}
pip install -qU langchain-azure-ai
```

## 实例化

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

```python theme={null}
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={null}
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={null}
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={null}
print(ai_msg.content)
```

```text theme={null}
J'adore programmer.
```

***

> 官方原文：[src/oss/python/integrations/chat/azure\_ai.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/chat/azure_ai.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.