Microsoft Agent 框架支持使用通过与 OpenAI Chat Completion 兼容的 API 部署的 Azure AI Foundry 模型来创建代理,因此可以使用 OpenAI 客户端库访问 Foundry 模型。
Azure AI Foundry 支持部署 各种模型,包括开源模型。
注释
这些模型的功能可能会限制代理的功能。 例如,许多开源模型不支持函数调用,因此基于此类模型的任何代理都无法使用函数工具。
入门
将所需的 NuGet 包添加到项目。
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
使用 Foundry 模型创建 OpenAI ChatCompletion 代理
首先需要创建客户端以连接到 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);
然后,可以使用模型部署名称创建聊天完成的客户端。
var chatCompletionClient = client.GetChatClient("gpt-4o-mini");
最后,可以在CreateAIAgent上使用ChatCompletionClient扩展方法创建代理。
AIAgent agent = chatCompletionClient.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操作。
有关如何运行和与代理交互的详细信息,请参阅 代理入门教程 。
即将推出更多文档。