Microsoft Foundry Models 通过单个终结点和一组凭据,提供对来自众多提供商的丰富多样模型的访问。 此功能使你可以在模型之间进行切换,并在应用程序中使用它们,而无需更改代码。
可以使用 Foundry 资源的 API 密钥对推理请求进行身份验证。 API 密钥可以快速设置,但他们授予对资源的完全访问权限,很难限定特定用户或操作的范围,并且需要手动轮换才能保持安全。 对于生产工作负载,请改用配合 Microsoft Entra ID 的无密钥身份验证。
使用 pip 安装 openai 包:
pip install openai --upgrade
创建指向 Azure OpenAI v1 终结点的客户端,然后生成响应。
/openai/v1/ 路由使用隐式版本控制,因此你无需传递 api-version。 在 model 字段中传递部署名称:
import os
from openai import OpenAI
client = OpenAI(
base_url="https://<resource>.openai.azure.com/openai/v1/",
api_key=os.environ["AZURE_INFERENCE_CREDENTIAL"],
)
response = client.responses.create(
model="deepseek-v3-0324", # Replace with your model deployment name.
input="Explain the Riemann hypothesis in one paragraph.",
)
print(response.output_text)
使用 npm 安装 openai 软件包:
npm install openai
创建指向 Azure OpenAI v1 终结点的客户端,然后生成响应:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://<resource>.openai.azure.com/openai/v1/",
apiKey: process.env.AZURE_INFERENCE_CREDENTIAL,
});
const response = await client.responses.create({
model: "deepseek-v3-0324", // Replace with your model deployment name.
input: "Explain the Riemann hypothesis in one paragraph.",
});
console.log(response.output_text);
安装 OpenAI 库:
dotnet add package OpenAI
创建指向 Azure OpenAI v1 终结点的客户端,然后生成响应:
using System.ClientModel;
using OpenAI;
using OpenAI.Responses;
OpenAIClient client = new(
new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL")),
new OpenAIClientOptions
{
Endpoint = new Uri("https://<resource>.openai.azure.com/openai/v1/")
});
OpenAIResponseClient responseClient = client.GetResponsesClient("deepseek-v3-0324");
OpenAIResponse response = responseClient.CreateResponse(
"Explain the Riemann hypothesis in one paragraph.");
Console.WriteLine(response.GetOutputText());
将 OpenAI Java SDK 添加到项目。 检查 OpenAI Java存储库以获取最新版本。
创建指向 Azure OpenAI v1 终结点的客户端,然后生成响应:
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://<resource>.openai.azure.com/openai/v1/")
.apiKey(System.getenv("AZURE_INFERENCE_CREDENTIAL"))
.build();
Response response = client.responses().create(
ResponseCreateParams.builder()
.model("deepseek-v3-0324") // Replace with your model deployment name.
.input("Explain the Riemann hypothesis in one paragraph.")
.build());
// The Responses API has no single output-text accessor; concatenate the output items.
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
将请求直接发送到 v1 路由。 路径 /openai/v1/ 使用隐式版本控制,因此不包含 api-version 查询参数。 将密钥作为持有者令牌在 Authorization 标头中传递:
curl -X POST https://<resource>.openai.azure.com/openai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_INFERENCE_CREDENTIAL" \
-d '{
"model": "deepseek-v3-0324",
"input": "Explain the Riemann hypothesis in one paragraph."
}'
已部署的 Foundry 模型支持使用 Microsoft Entra ID 的无密钥授权。 无密钥授权可增强安全性、简化用户体验、降低操作复杂性并提供可靠的合规性支持。 如果组织使用安全且可缩放的标识管理解决方案,请使用无密钥授权。
使用包管理器(如 pip)安装 OpenAI SDK:
pip install openai
若要Microsoft Entra ID身份验证,请安装:
pip install azure-identity
使用包来消耗模型。 以下示例演示如何使用 Microsoft Entra ID 和模型部署创建客户端并对响应 API 进行测试调用。
请将 <resource> 替换为您的 Foundry 资源名称。 在 Azure 门户中,或通过运行 az cognitiveservices account list,找到它。 将 deepseek-v3-0324 替换为您的实际部署名称。
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://ai.azure.com/.default"
)
client = OpenAI(
base_url="https://<resource>.openai.azure.com/openai/v1/",
api_key=token_provider,
)
response = client.responses.create(
model="deepseek-v3-0324", # Replace with your model deployment name.
input="What is Azure AI?",
)
print(response.output_text)
预期输出
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
参考:OpenAI Python SDK 和 DefaultAzureCredential 类。
安装 OpenAI SDK:
dotnet add package OpenAI
若要进行Microsoft Entra ID身份验证,请安装 Azure.Identity 包:
dotnet add package Azure.Identity
然后,使用包来调用模型。 以下示例演示如何使用 Microsoft Entra ID 和模型部署创建客户端并对响应 API 进行测试调用。
将 <resource> 替换为 Foundry 资源名称(在 Azure 门户中找到)。 将 deepseek-v3-0324 替换为您的实际部署名称。
using Azure.Identity;
using OpenAI;
using OpenAI.Responses;
using System.ClientModel.Primitives;
#pragma warning disable OPENAI001
BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://ai.azure.com/.default"
);
OpenAIResponseClient client = new(
model: "deepseek-v3-0324", // Replace with your model deployment name.
authenticationPolicy: tokenPolicy,
options: new OpenAIClientOptions()
{
Endpoint = new Uri("https://<resource>.openai.azure.com/openai/v1/")
}
);
OpenAIResponse response = client.CreateResponse("What is Azure AI?");
Console.WriteLine(response.GetOutputText());
预期输出:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
参考:OpenAI .NET SDK 和 DefaultAzureCredential 类。
使用 npm 安装 OpenAI SDK:
npm install openai
若要Microsoft Entra ID身份验证,请安装:
npm install @azure/identity
然后,使用包来调用模型。 以下示例演示如何使用 Microsoft Entra ID 和模型部署创建客户端并对响应 API 进行测试调用。
将 <resource> 替换为 Foundry 资源名称(在 Azure 门户中或通过运行 az cognitiveservices account list 查找)。 将 deepseek-v3-0324 替换为您的实际部署名称。
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { OpenAI } from "openai";
const tokenProvider = getBearerTokenProvider(
new DefaultAzureCredential(),
'https://ai.azure.com/.default'
);
const client = new OpenAI({
baseURL: "https://<resource>.openai.azure.com/openai/v1/",
apiKey: tokenProvider
});
const response = await client.responses.create({
model: "deepseek-v3-0324", // Replace with your model deployment name.
input: "What is Azure AI?"
});
console.log(response.output_text);
预期输出:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
参考: OpenAI Node.js SDK 和 DefaultAzureCredential 类。
将 OpenAI SDK 添加到项目。 有关最新版本和安装说明,请查看 OpenAI Java GitHub 存储库。
对于Microsoft Entra ID身份验证,还添加:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.0</version>
</dependency>
然后,使用包来调用模型。 以下示例演示如何使用 Microsoft Entra ID 和模型部署创建客户端并对响应 API 进行测试调用。
将 <resource> 替换为 Foundry 资源名称(在 Azure 门户中找到)。 将 deepseek-v3-0324 替换为您的实际部署名称。
import com.azure.identity.AuthenticationUtil;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.credential.BearerTokenCredential;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
DefaultAzureCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://<resource>.openai.azure.com/openai/v1/")
.credential(BearerTokenCredential.create(
AuthenticationUtil.getBearerTokenSupplier(
tokenCredential,
"https://ai.azure.com/.default"
)
))
.build();
ResponseCreateParams params = ResponseCreateParams.builder()
.model("deepseek-v3-0324") // Replace with your model deployment name.
.input("What is Azure AI?")
.build();
Response response = client.responses().create(params);
// The Responses API has no single output-text accessor; concatenate the output items.
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
预期输出:
Azure AI is a comprehensive suite of artificial intelligence services and tools from Microsoft that enables developers to build intelligent applications. It includes services for natural language processing, computer vision, speech recognition, and machine learning capabilities.
参考:OpenAI Java SDK 和 DefaultAzureCredential 类。
浏览 参考部分中 的 API 设计,了解哪些参数可用。 将身份验证(Bearer)令牌插入到 Authorization 标头中。
例如, 响应 API 参考部分详细介绍了如何使用 /responses 路由来生成预测。 路径 /openai/v1/ 包含在 URL 的根目录中:
请求
将 <resource> 替换为 Foundry 资源名称(在 Azure 门户中或通过运行 az cognitiveservices account list 查找)。 将 deepseek-v3-0324 替换为您的实际部署名称。
基础 URL 支持 https://<resource>.openai.azure.com/openai/v1/ 和 https://<resource>.services.ai.azure.com/openai/v1/ 两种格式。
curl -X POST https://<resource>.openai.azure.com/openai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "deepseek-v3-0324",
"input": "Explain what the bitter lesson is?"
}'
响应
如果身份验证成功,则会收到一个 200 OK 响应,响应结果包含在响应正文中:
{
"id": "resp_...",
"object": "response",
"created_at": 1738368234,
"model": "deepseek-v3-0324",
"status": "completed",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "The bitter lesson refers to a key insight in AI research that emphasizes the importance of general-purpose learning methods that leverage computation, rather than human-designed domain-specific approaches. It suggests that methods which scale with increased computation tend to be more effective in the long run."
}
]
}
],
"usage": {
"input_tokens": 28,
"output_tokens": 52,
"total_tokens": 80
}
}
令牌必须带有作用域 https://ai.azure.com/.default。
出于测试目的,获取用户帐户的有效令牌的最简单方法是使用Azure CLI。 在控制台中,通过运行以下Azure CLI命令登录并请求令牌:
az login
az account get-access-token --resource https://ai.azure.com --query "accessToken" --output tsv
此命令输出可在环境变量中 $AZURE_OPENAI_AUTH_TOKEN 存储的访问令牌。
参考: 响应 API