Microsoft Agent Framework 支援兩種 Azure OpenAI 用戶端類型,每種針對不同的 API 表面及不同工具功能。 Responses 是推薦的主要客戶端:它支援完整的託管工具組合。 當你需要廣泛的模型相容性支援,或需要保留現有的 Chat Completions 整合時,請使用 Chat Completion。
Warning
將模型或嵌入輸出,以及支援時由模型產生的工具呼叫,視為不可信的應用程式輸入。 在程式碼產生副作用或渲染內容之前,先驗證它們。 如果你設定自訂端點,請確認你信任它的 URL 和營運者。
| 客戶類型 | API | 最適合 |
|---|---|---|
| 回應(建議) | 回應 API | 具備完整功能代理,並附有託管工具(程式碼解譯器、檔案搜尋、網頁搜尋、託管 MCP) |
| 聊天完成 | 聊天完成 API | 簡單的代理,廣泛的模型支援 |
Tip
關於直接的 OpenAI 對應物(OpenAIChatClient, OpenAIChatCompletionClient),請參見 OpenAI 提供者頁面。 工具支援完全相同。
備註
Azure OpenAI 助理 API 已遭淘汰。 新程式碼應該使用回應客戶端。 如果你是從現有的 Assistants 應用程式遷移過來,請參考 語意核心 遷移指南。
使用者入門
將必要的 NuGet 套件新增至您的專案。
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
所有 Azure OpenAI 用戶端類型皆始於建立AzureOpenAIClient:
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI.Chat;
using OpenAI.Responses;
AzureOpenAIClient client = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com"),
new DefaultAzureCredential());
Warning
DefaultAzureCredential 開發方便,但在生產過程中需謹慎考量。 在生產環境中,建議使用特定的憑證(例如 ManagedIdentityCredential),以避免延遲問題、意外的憑證探測,以及備援機制帶來的安全風險。
回覆用戶端
Responses 用戶端是推薦的主要用戶端,提供最豐富的工具支援,包括程式碼直譯器、檔案搜尋、網頁搜尋及託管 MCP。
var responsesClient = client.GetResponsesClient();
AIAgent agent = responsesClient.AsAIAgent(
model: "gpt-4o-mini",
instructions: "You are a helpful coding assistant.",
name: "CodeHelper");
Console.WriteLine(await agent.RunAsync("Write a Python function to sort a list."));
支援工具: 功能工具、工具審核、程式碼解譯器、檔案搜尋、網頁搜尋、託管 MCP、本地 MCP 工具。
聊天完成客戶端
聊天完成客戶端提供了一種直接使用聊天完成 API 來建立代理的方式。 當你需要廣泛的模型相容性或已有 Chat Completions 整合時,可以使用它。
var chatClient = client.GetChatClient("gpt-4o-mini");
AIAgent agent = chatClient.AsAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
支援工具: 功能工具、網頁搜尋、本地 MCP 工具。
助理用戶端
備註
Azure OpenAI 助理 API 已遭淘汰。 Agent Framework 不再提供 Assistants 用戶端的說明文件;新程式碼請改用上述的 Responses 用戶端。 關於遷移現有應用程式,請參閱 語意核心 遷移指南。
功能工具
你可以為任何 Azure OpenAI 代理提供自訂函式工具:
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI.Chat;
[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
=> $"The weather in {location} is cloudy with a high of 15°C.";
AIAgent agent = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential())
.GetChatClient(deploymentName)
.AsAIAgent(instructions: "You are a helpful assistant", tools: [AIFunctionFactory.Create(GetWeather)]);
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
串流回應
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate."))
{
Console.Write(update);
}
Tip
完整可執行範例請參閱 .NET 範例 。
使用代理程式
兩種用戶端類型都會產生一個標準的 AIAgent,支援相同的代理程式操作(串流、執行緒、中介軟體)。
更多資訊請參閱 入門教學。
Tools
Azure OpenAI .NET 用戶端會與相應的 OpenAI 用戶端共享其工具表面。 完整的各用戶端對照矩陣請參見 OpenAI 提供者頁面——Azure 版本的 Responses 和 Chat Completions 與其對應的直接 OpenAI 版本一致。
| Tool | 回應 | 聊天完成 |
|---|---|---|
| 函式工具 | ✅ | ✅ |
| 工具核准 | ✅ | ✅ |
| 程式碼解譯器 | ✅ | ❌ |
| 檔案搜尋 | ✅ | ❌ |
| 網路搜尋 | ✅ | ✅ |
| 託管 MCP 工具 | ✅ | ❌ |
| 本地 MCP 工具 | ✅ | ✅ |
備註
工具核准 由框架的函式調用聊天客戶端提供,因此無論底層 API 為何,都能對任何函式工具呼叫有效。
Python 指引
這很重要
Python Azure OpenAI 指引現已存於 OpenAI 提供者頁面。 使用該頁面進行OpenAIChatCompletionClient、OpenAIChatClient,以及OpenAIEmbeddingClient、部署名稱到model對應、明確的 Azure 路由輸入,如credential或azure_endpoint、選取 Azure 後的api_version設定,加上提供完整.../openai/v1 URL 的base_url指引。 如果 OPENAI_API_KEY 也存在,則一般用戶端會停留在 OpenAI,除非您提供明確的 Azure 路由輸入。 如果只有 AZURE_OPENAI_* 設定,Azure 環境備援仍然有效。 舊的 Python AzureOpenAI* 相容類別已從現有 agent_framework.azure 命名空間移除,因此將舊程式碼遷移至 agent_framework.openai。 對於新的 Python 解決方案,我們建議使用 Microsoft Foundry 來部署模型,並透過 FoundryChatClient 進行連接,而非局限於 Azure OpenAI 專用路徑。 如果你需要 Foundry 專案端點或 Foundry 代理服務,請參閱 Foundry 提供者頁面。 如需更廣泛的遷移清單,請參閱 Python 重大變更指南。
Tools
Python Azure OpenAI 使用與直接 OpenAI 相同的agent_framework.openai客戶端,因此工具表面相同。 如需查看完整的各用戶端對照矩陣,請參閱 OpenAI 提供者頁面的工具區段。
Azure OpenAI
在 Go 中,Azure OpenAI 使用與 Direct OpenAI 相同的openaiprovider套件,並具備 Azure 專屬的用戶端初始化。
Installation
go get github.com/microsoft/agent-framework-go
建立 Azure OpenAI 代理程式
import (
"cmp"
"fmt"
"os"
"github.com/microsoft/agent-framework-go/agent"
"github.com/microsoft/agent-framework-go/provider/openaiprovider"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
openai "github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/azure"
)
endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
deployment := os.Getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
apiVersion := cmp.Or(os.Getenv("AZURE_OPENAI_API_VERSION"), "2025-01-01-preview")
token, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
a := openaiprovider.NewChatCompletionsAgent(
openai.NewClient(
azure.WithEndpoint(endpoint, apiVersion),
azure.WithTokenCredential(token),
),
openaiprovider.AgentConfig{
Model: deployment,
Instructions: "You are a helpful assistant.",
Config: agent.Config{
Name: "AzureAgent",
},
},
)
resp, err := a.RunText(ctx, "Hello!").Collect()
Warning
azidentity.NewDefaultAzureCredential 開發方便,但在生產過程中需謹慎考量。 在生產環境中,建議使用特定的憑證,例如 azidentity.NewManagedIdentityCredential,以避免延遲問題、意外的憑證探測,以及備用機制帶來的安全風險。
使用回應 API
當您的 Azure OpenAI 部署支援回應 API 時,請使用openaiprovider.NewResponsesAgent同一個 Azure 設定的 OpenAI 用戶端:
responsesAgent := openaiprovider.NewResponsesAgent(
openai.NewClient(
azure.WithEndpoint(endpoint, apiVersion),
azure.WithTokenCredential(token),
),
openaiprovider.AgentConfig{
Model: deployment,
Instructions: "You are a helpful assistant.",
Config: agent.Config{
Name: "AzureResponsesAgent",
},
},
)
response, err := responsesAgent.RunText(ctx, "Summarize the latest deployment status.").Collect()
if err != nil {
return err
}
fmt.Println(response.String())
環境變數
| Variable | 說明 |
|---|---|
AZURE_OPENAI_ENDPOINT |
您的 Azure OpenAI 資源端點 |
AZURE_OPENAI_DEPLOYMENT_NAME |
部署/型號名稱 |
AZURE_OPENAI_API_VERSION |
API 版本(例如, 2025-01-01-preview) |
Tip
完整範例請參閱 Azure OpenAI 聊天完成範例與回應範例。