Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

本指南将帮助您开始使用 AzureAIOpenAIApiChatModel 聊天模型. 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.

概述

集成详情

可序列化JS 支持下载量版本
AzureAIOpenAIApiChatModellangchain-azure-aiPyPI - DownloadsPyPI - Version

模型功能

Tool callingStructured outputImage input音频输入视频输入Token-level streaming原生异步Token usageLogprobs

设置

要访问 AzureAIOpenAIApiChatModel models, you’ll need to create an Azure account, get an API key, and install the langchain-azure-ai 集成包。

凭证

Head to the Azure docs 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:
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 API key by uncommenting below:
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("请输入您的 LangSmith API 密钥: ")

安装

LangChain 的 AzureAIOpenAIApiChatModel 集成位于 langchain-azure-ai 包中:
pip install -qU langchain-azure-ai

实例化

现在我们可以实例化模型对象并生成聊天补全:
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,
)

调用

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
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})
print(ai_msg.content)
J'adore programmer.