本教程介绍如何根据 Azure OpenAI 聊天完成服务使用代理框架创建和运行代理。
重要
代理框架支持多种不同类型的代理。 本教程使用基于聊天完成服务的代理,但所有其他代理类型都以相同的方式运行。 有关其他代理类型以及如何构造它们的详细信息,请参阅 Agent Framework 用户指南。
先决条件
开始之前,请确保具备以下先决条件:
- .NET 8.0 SDK 或更高版本
- 已配置 Azure OpenAI 服务终结点和部署
- 已安装 并 经过身份验证的 Azure CLI(用于 Azure 凭据身份验证)
-
用户具有 Azure OpenAI 资源的
Cognitive Services OpenAI User或Cognitive Services OpenAI Contributor角色。
注释
Microsoft .NET 的所有主动支持版本都支持代理框架。 对于此示例,我们建议使用 .NET 8 SDK 或更高版本。
重要
本教程使用 Azure OpenAI 进行聊天完成服务,但可以使用提供 IChatClient 实现的任何推理服务。
安装 NuGet 包
若要将 Microsoft Agent Framework 与 Azure OpenAI 配合使用,需要安装以下 NuGet 包:
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
创建智能体
- 首先,为 Azure OpenAI 创建客户端,方法是提供 Azure OpenAI 终结点,并使用与在 先决条件 步骤中使用 Azure CLI 进行身份验证时所用相同的登录名。
- 然后,获取一个用于与对话生成服务进行通信的聊天客户端,在其中也指定要使用的特定模型部署。 使用在 先决条件 步骤中创建的部署之一。
- 最后,创建代理,提供代理的说明和名称。
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
AIAgent agent = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com"),
new AzureCliCredential())
.GetChatClient("gpt-4o-mini")
.CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker");
运行代理
若要运行代理,请在代理实例上调用 RunAsync 该方法,并提供用户输入。
代理将返回一个 AgentRunResponse 对象,并调用 .ToString() 或 .Text 对此响应对象提供代理的文本结果。
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
示例输出:
Why did the pirate go to school?
Because he wanted to improve his "arrr-ticulation"! 🏴☠️
使用流式处理运行代理
若要使用流式处理运行代理,请在代理实例上调用 RunStreamingAsync 该方法,并提供用户输入。
代理将返回流 AgentRunResponseUpdate 对象,调用 .ToString() 或 .Text 对每个更新对象提供该更新中包含的文本结果的一部分。
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate."))
{
Console.WriteLine(update);
}
示例输出:
Why
did
the
pirate
go
to
school
?
To
improve
his
"
ar
rrrr
rr
tic
ulation
!"
使用 ChatMessages 运行代理
可以向ChatMessage和RunAsync方法提供一个或多个RunStreamingAsync对象,而不是简单的字符串。
下面是包含单个用户消息的示例:
ChatMessage message = new(ChatRole.User, [
new TextContent("Tell me a joke about this image?"),
new UriContent("https://upload.wikimedia.org/wikipedia/commons/1/11/Joseph_Grimaldi.jpg", "image/jpeg")
]);
Console.WriteLine(await agent.RunAsync(message));
示例输出:
Why did the clown bring a bottle of sparkling water to the show?
Because he wanted to make a splash!
下面是一个包含系统和用户消息的示例:
ChatMessage systemMessage = new(
ChatRole.System,
"""
If the user asks you to tell a joke, refuse to do so, explaining that you are not a clown.
Offer the user an interesting fact instead.
""");
ChatMessage userMessage = new(ChatRole.User, "Tell me a joke about a pirate.");
Console.WriteLine(await agent.RunAsync([systemMessage, userMessage]));
示例输出:
I'm not a clown, but I can share an interesting fact! Did you know that pirates often revised the Jolly Roger flag? Depending on the pirate captain, it could feature different symbols like skulls, bones, or hourglasses, each representing their unique approach to piracy.
本教程介绍如何根据 Azure OpenAI 聊天完成服务使用代理框架创建和运行代理。
重要
代理框架支持多种不同类型的代理。 本教程使用基于聊天完成服务的代理,但所有其他代理类型都以相同的方式运行。 有关其他代理类型以及如何构造它们的详细信息,请参阅 Agent Framework 用户指南。
先决条件
开始之前,请确保具备以下先决条件:
- Python 3.10 或更高版本
- 已配置 Azure OpenAI 服务终结点和部署
- 已安装 并 经过身份验证的 Azure CLI(用于 Azure 凭据身份验证)
-
用户具有 Azure OpenAI 资源的
Cognitive Services OpenAI User或Cognitive Services OpenAI Contributor角色。
重要
本教程使用 Azure OpenAI 进行聊天完成服务,但可以使用任何与 Agent Framework 聊天客户端协议兼容的推理服务。
安装 Python 包
若要将 Microsoft Agent Framework 与 Azure OpenAI 配合使用,需要安装以下 Python 包:
pip install agent-framework
创建智能体
- 首先,创建用于与 Azure OpenAI 通信的聊天客户端,并在 先决条件 步骤中使用与 Azure CLI 进行身份验证时所用的登录名。
- 然后,创建代理,提供代理的说明和名称。
import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
运行代理
若要运行代理,请在代理实例上调用 run 该方法,并提供用户输入。
代理将返回响应对象,并通过访问和使用 .text 该属性来获得代理的文本结果。
async def main():
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())
使用流式处理运行代理
若要使用流式处理运行代理,请在代理实例上调用 run_stream 该方法,并提供用户输入。
代理将传输更新对象列表,并通过访问每个更新对象上的 .text 属性可提供该更新中包含的文本结果的一部分。
async def main():
async for update in agent.run_stream("Tell me a joke about a pirate."):
if update.text:
print(update.text, end="", flush=True)
print() # New line after streaming is complete
asyncio.run(main())
使用 ChatMessage 运行代理
可以向ChatMessage和run方法提供一个或多个run_stream对象,而不是简单的字符串。
from agent_framework import ChatMessage, TextContent, UriContent, Role
message = ChatMessage(
role=Role.USER,
contents=[
TextContent(text="Tell me a joke about this image?"),
UriContent(uri="https://samplesite.org/clown.jpg", media_type="image/jpeg")
]
)
async def main():
result = await agent.run(message)
print(result.text)
asyncio.run(main())