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標頭中以 Bearer 代碼形式傳遞您的密鑰:
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 進行測試呼叫。
請用你的 Foundry 資源名稱替換 <resource> 。 你可以在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 class。
安裝 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 class。
使用 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 repository 以獲取最新版本及安裝說明。
關於 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 class。
請在 參考部分 探索 API 設計,看看有哪些參數可用。 將驗證 (Bearer) 代碼插入Authorization標頭中。
例如, 回應 API 參考章節詳細說明如何利用該 /responses 路由產生預測。
/openai/v1/路徑包含在 URL 的根節點中:
請求
將 <resource> 替換成你的 Foundry 資源名稱(可在 Azure 入口網站找到,或執行 az cognitiveservices account list)。 請用你實際的部署名稱來取代 deepseek-v3-0324 。
基礎網址同時接受兩種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 環境變數裡。
參考資料: Responses API