AutoGen Core
.NET 的 AutoGen Core 遵循与其 Python 版本相同的概念和约定。 事实上,为了理解 .NET 版本中的概念,我们建议首先阅读 Python 文档。 除非另有说明,Python 版本中的概念都映射到 .NET。
语言版本之间的任何重要差异都记录在 与 Python 的差异 部分中。 对于仅影响给定语言的内容,例如依赖注入或主机构建器模式,这些将不会在差异文档中指定。
开始使用
您可以通过 nuget 包或克隆存储库来获取 SDK。 该 SDK 可在 NuGet 上获得。 至少您需要以下内容
dotnet add package Microsoft.AutoGen.Contracts
dotnet add package Microsoft.AutoGen.Core
有关安装所有相关软件包的更详细说明,请参见 安装。
您可以查看存储库的 samples 目录中的示例,从而快速开始使用。
创建代理
要创建代理,您可以继承自 BaseAgent 并为您关心的事件实现事件处理程序。 这是一个演示如何从 BaseAgent 继承并实现事件处理程序的最小示例
public class MyAgent : BaseAgent, IHandle<MyMessage>
{
// ...
public async ValueTask HandleAsync(MyMessage item, MessageContext context)
{
// ...logic here...
}
}
通过重写 BaseAgent,您可以访问运行时和日志记录实用程序,并通过实现 IHandle
在应用程序中运行代理
要在应用程序中运行您的代理,您可以使用 AgentsAppBuilder
类。 这是一个如何在应用程序中运行代理“HelloAgent”的示例
AgentsAppBuilder appBuilder = new AgentsAppBuilder()
.UseInProcessRuntime(deliverToSelf: true)
.AddAgent<HelloAgent>("HelloAgent");
var app = await appBuilder.BuildAsync();
// start the app by publishing a message to the runtime
await app.PublishMessageAsync(new NewMessageReceived
{
Message = "Hello from .NET"
}, new TopicId("HelloTopic"));
// Wait for shutdown
await app.WaitForShutdownAsync();
.NET SDK 运行时
.NET SDK 包括一个内存中的单进程运行时和一个远程、分布式运行时,旨在在云中运行您的代理。 分布式运行时支持在 Python 和 .NET 中运行代理,允许这些代理相互通信。 分布式运行时使用 Microsoft Orleans 来提供弹性、持久性和与 Azure 事件中心等消息服务的集成。 xlang 功能要求您的代理的消息可以序列化为 CloudEvents。 消息作为 CloudEvents 通过 Grpc 交换,运行时负责确保消息传递到正确的代理。
要使用分布式运行时,您需要将以下包添加到您的项目中
dotnet add package Microsoft.AutoGen.Core.Grpc
这是在具有代理的应用程序中运行并连接到分布式系统的包。
要运行后端/服务器端,您需要
dotnet add package Microsoft.AutoGen.RuntimeGateway
dotnet add package Microsoft.AutoGen.AgentHost
您可以单独运行后端
dotnet run --project Microsoft.AutoGen.AgentHost
或者您可以将其包含在您自己的应用程序中
using Microsoft.AutoGen.RuntimeGateway;
using Microsoft.AutoGen.AgentHost;
var autogenBackend = await Microsoft.AutoGen.RuntimeGateway.Grpc.Host.StartAsync(local: false, useGrpc: true).ConfigureAwait(false);
您还可以将运行时安装为 dotnet 工具
dotnet pack --no-build --configuration Release --output './output/release' -bl\n
dotnet tool install --add-source ./output/release Microsoft.AutoGen.AgentHost
# run the tool
# dotnet agenthost
# or just...
agenthost
在单独的进程中使用 .NET Aspire 运行多个代理和运行时
Hello.AppHost 项目 演示了如何使用 .NET Aspire 在单独的进程中编排具有多个代理和运行时的分布式系统。 它还指向一个 Python 代理,该代理演示了如何在同一分布式系统中以不同的语言运行代理。
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs
using Microsoft.Extensions.Hosting;
var builder = DistributedApplication.CreateBuilder(args);
var backend = builder.AddProject<Projects.Microsoft_AutoGen_AgentHost>("backend").WithExternalHttpEndpoints();
var client = builder.AddProject<Projects.HelloAgent>("HelloAgentsDotNET")
.WithReference(backend)
.WithEnvironment("AGENT_HOST", backend.GetEndpoint("https"))
.WithEnvironment("STAY_ALIVE_ON_GOODBYE", "true")
.WaitFor(backend);
// xlang is over http for now - in prod use TLS between containers
builder.AddPythonApp("HelloAgentsPython", "../../../../python/samples/core_xlang_hello_python_agent", "hello_python_agent.py", "../../.venv")
.WithReference(backend)
.WithEnvironment("AGENT_HOST", backend.GetEndpoint("http"))
.WithEnvironment("STAY_ALIVE_ON_GOODBYE", "true")
.WithEnvironment("GRPC_DNS_RESOLVER", "native")
.WithOtlpExporter()
.WaitFor(client);
using var app = builder.Build();
await app.StartAsync();
var url = backend.GetEndpoint("http").Url;
Console.WriteLine("Backend URL: " + url);
await app.WaitForShutdownAsync();
您可以在 Microsoft.AutoGen.Integration.Tests.AppHost 目录中找到更多关于如何使用 Aspire 和 XLang 代理的示例。
配置日志记录
SDK 使用 Microsoft.Extensions.Logging 框架进行日志记录。 这是一个具有一些有用默认值的示例 appsettings.json 文件
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.AspNetCore": "Information",
"Microsoft": "Information",
"Microsoft.Orleans": "Warning",
"Orleans.Runtime": "Error",
"Grpc": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}
在 Protocol Buffers 中定义消息类型
定义要在 python 和 .NET 代理中使用的通用事件或消息类型的便捷方法是定义您的事件。 这在以下位置进行了介绍:使用 Protocol Buffers 定义消息类型。