Azure AI Foundry 模型响应代理

Microsoft代理框架支持使用通过 OpenAI 响应兼容 API 部署的模型创建代理,因此 OpenAI 客户端库可用于访问 Foundry 模型。

入门

将所需的 NuGet 包添加到项目。

dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease

使用 Foundry 模型创建 OpenAI 响应代理

首先需要创建客户端以连接到 OpenAI 服务。

由于代码未使用默认 OpenAI 服务,因此需要通过 OpenAIClientOptions 提供 OpenAI 兼容的 Foundry 服务的 URI。

using System;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;

var clientOptions = new OpenAIClientOptions() { Endpoint = new Uri("https://<myresource>.services.ai.azure.com/openai/v1/") };

#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
OpenAIClient client = new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), clientOptions);
#pragma warning restore OPENAI001
// You can optionally authenticate with an API key
// OpenAIClient client = new OpenAIClient(new ApiKeyCredential("<your_api_key>"), clientOptions);

然后,可以使用模型部署名称创建响应的客户端。

#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
var responseClient = client.GetOpenAIResponseClient("gpt-4o-mini");
#pragma warning restore OPENAI001

最后,可以在CreateAIAgent上使用ResponseClient扩展方法创建代理。

AIAgent agent = responseClient.CreateAIAgent(
    instructions: "You are good at telling jokes.",
    name: "Joker");

// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));

使用代理

代理是标准的AIAgent,支持所有标准AIAgent操作。

有关如何运行和与代理交互的详细信息,请参阅 代理入门教程

即将推出更多文档。

后续步骤