Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AgentHost as a container + sample #4187

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,6 @@ samples/apps/autogen-studio/autogenstudio/models/test/
notebook/coding

# dotnet artifacts
artifacts
artifacts

certs/**
24 changes: 24 additions & 0 deletions dotnet/AutoGen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Extension
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Agents.Tests", "test\Microsoft.AutoGen.Agents.Tests\Microsoft.AutoGen.Agents.Tests.csproj", "{394FDAF8-74F9-4977-94A5-3371737EB774}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HelloDistributed", "HelloDistributed", "{80A6C47B-0567-49F6-843A-667B96FA4BB4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppHost", "samples\Hello-distributed\AppHost\AppHost.csproj", "{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.AgentHost", "src\Microsoft.AutoGen\AgentHost\Microsoft.AutoGen.AgentHost.csproj", "{FEA16F7A-9CFB-475D-9B96-0F498D5415E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "samples\Hello-distributed\Api\Api.csproj", "{286A2449-E9D3-75B7-2CA8-25C9291E75EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -342,6 +350,18 @@ Global
{394FDAF8-74F9-4977-94A5-3371737EB774}.Debug|Any CPU.Build.0 = Debug|Any CPU
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.ActiveCfg = Release|Any CPU
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.Build.0 = Release|Any CPU
{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF}.Release|Any CPU.Build.0 = Release|Any CPU
{FEA16F7A-9CFB-475D-9B96-0F498D5415E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEA16F7A-9CFB-475D-9B96-0F498D5415E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEA16F7A-9CFB-475D-9B96-0F498D5415E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEA16F7A-9CFB-475D-9B96-0F498D5415E2}.Release|Any CPU.Build.0 = Release|Any CPU
{286A2449-E9D3-75B7-2CA8-25C9291E75EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{286A2449-E9D3-75B7-2CA8-25C9291E75EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{286A2449-E9D3-75B7-2CA8-25C9291E75EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{286A2449-E9D3-75B7-2CA8-25C9291E75EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -402,6 +422,10 @@ Global
{64EF61E7-00A6-4E5E-9808-62E10993A0E5} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
{65059914-5527-4A00-9308-9FAF23D5E85A} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{394FDAF8-74F9-4977-94A5-3371737EB774} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{80A6C47B-0567-49F6-843A-667B96FA4BB4} = {686480D7-8FEC-4ED3-9C5D-CEBE1057A7ED}
{10C07AB6-45FE-4183-A6C6-D0656DB0F4BF} = {80A6C47B-0567-49F6-843A-667B96FA4BB4}
{FEA16F7A-9CFB-475D-9B96-0F498D5415E2} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{286A2449-E9D3-75B7-2CA8-25C9291E75EE} = {80A6C47B-0567-49F6-843A-667B96FA4BB4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
Expand Down
12 changes: 6 additions & 6 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<ItemGroup>
<PackageVersion Include="Aspire.Hosting" Version="9.0.0" />
<PackageVersion Include="AspNetCore.Authentication.ApiKey" Version="8.0.1" />
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="8.0.1-preview.8.24267.1" />
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="9.0.0-preview.5.24551.3" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Azure.ApplicationInsights" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Azure.CognitiveServices" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.NodeJs" Version="8.2.0" />
<PackageVersion Include="Aspire.Hosting.NodeJs" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Orleans" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Qdrant" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Redis" Version="8.2.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.1.0-beta.2" />
<PackageVersion Include="Aspire.Hosting.Redis" Version="9.0.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version=" 2.1.0-beta.2" />
<PackageVersion Include="Azure.AI.Inference" Version="1.0.0-beta.1" />
<PackageVersion Include="Azure.Data.Tables" Version="12.9.1" />
<PackageVersion Include="Azure.Identity" Version="1.13.1" />
Expand All @@ -37,7 +37,7 @@
<PackageVersion Include="Grpc.Core" Version="2.46.6" />
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.67.0" />
<PackageVersion Include="Grpc.Tools" Version="2.67.0" />
<PackageVersion Include="Grpc.Net.Client" Version="2.65.0" />
<PackageVersion Include="Grpc.Net.Client" Version="2.67.0" />
<PackageVersion Include="Google.Protobuf" Version="3.28.3" />
<PackageVersion Include="Microsoft.AspNetCore.App" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
Expand Down Expand Up @@ -121,4 +121,4 @@
<PackageVersion Include="Microsoft.PowerShell.SDK" Version="7.4.5" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.11" />
</ItemGroup>
</Project>
</Project>
34 changes: 34 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Agents/HelloAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// HelloAgent.cs

using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Agents;

namespace Api.Agents;

[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<HelloAgent> logger) : AgentBase(
context,
typeRegistry, logger),
IHandle<NewGreetingRequested>
{
public async Task Handle(NewGreetingRequested item)
{
_logger.LogInformation($"HelloAgent with Id: {AgentId} received NewGreetingRequested with {item.Message}");
var response = await SayHello(item.Message).ConfigureAwait(false);
var greeting = new NewGreetingGenerated
{
UserId = AgentId.Key,
UserMessage = response
};
await PublishMessageAsync(greeting).ConfigureAwait(false);
}

public async Task<string> SayHello(string ask)
{
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
return response;
}
}
21 changes: 21 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Agents/OutputAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// OutputAgent.cs

using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Agents;

namespace Api.Agents;

[TopicSubscription("HelloAgents")]
public class OutputAgent(
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<OutputAgent> logger) : AgentBase(
context,
typeRegistry, logger),
IHandle<NewGreetingGenerated>
{
public async Task Handle(NewGreetingGenerated item)
{
_logger.LogInformation($"OutputAgent with Id: {AgentId} received NewGreetingGenerated with {item.UserMessage}");
}
}
30 changes: 30 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Grpc.Net.Client" />
<PackageReference Include="Grpc.Tools" >
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Extensions\Aspire\Microsoft.AutoGen.Extensions.Aspire.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Extensions\SemanticKernel\Microsoft.AutoGen.Extensions.SemanticKernel.csproj" />
</ItemGroup>

<ItemGroup>
<Protobuf Include=".\Protos\messages.proto" GrpcServices="Server;Client" Link="Protos\messages.proto" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@Api_HostAddress = http://localhost:5042

GET {{Api_HostAddress}}/weatherforecast/
Accept: application/json

###
43 changes: 43 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs

using Api;
using Api.Agents;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AutoGen.Agents;
using Microsoft.AutoGen.Extensions.SemanticKernel;
using Microsoft.AutoGen.Abstractions;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.AddServiceDefaults();
builder.ConfigureSemanticKernel();

var agentHostUrl = builder.Configuration["AGENT_HOST"]!;
builder.AddAgentWorker(agentHostUrl)
.AddAgent<HelloAgent>(nameof(HelloAgent))
.AddAgent<OutputAgent>(nameof(OutputAgent));

builder.Services.AddSingleton<AgentWorker>();

var app = builder.Build();

app.MapDefaultEndpoints();

app.UseSwagger();
app.UseSwaggerUI();

app.MapPost("/sessions", async ([FromBody] string message, Client client) =>
{
var session = Guid.NewGuid().ToString();
await client.PublishEventAsync(new NewGreetingRequested { Message = message }.ToCloudEvent(session));
return session;
});

app.Run();

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35459",
"sslPort": 44302
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5042",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7238;http://localhost:5042",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
15 changes: 15 additions & 0 deletions dotnet/samples/Hello-distributed/Api/Protos/messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package helloWorld;

option csharp_namespace = "Api";

message NewGreetingRequested {
string message = 1;
}

message NewGreetingGenerated {
string user_id = 1;
string user_message = 2;
}

9 changes: 9 additions & 0 deletions dotnet/samples/Hello-distributed/Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
23 changes: 23 additions & 0 deletions dotnet/samples/Hello-distributed/AppHost/AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>e8874200-80ab-41e3-bb56-b5bb93974eea</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Api\Api.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Properties\launchSettings.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions dotnet/samples/Hello-distributed/AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs

var builder = DistributedApplication.CreateBuilder(args);

var agentHost = builder.AddContainer("agent-host", "autogen-host")
.WithEnvironment("ASPNETCORE_URLS", "https://+;http://+")
.WithEnvironment("ASPNETCORE_HTTPS_PORTS", "5001")
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", "mysecurepass")
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "/https/devcert.pfx")
.WithBindMount("./certs", "/https/",true)
.WithHttpsEndpoint(targetPort: 5001);

var agentHostHttps = agentHost.GetEndpoint("https");

builder.AddProject<Projects.Api>("api")
.WithEnvironment("AGENT_HOST", $"{agentHostHttps.Property(EndpointProperty.Url)}")
.WithEnvironment("OpenAI__Key", builder.Configuration["OpenAI:Key"])
.WithEnvironment("OpenAI__Endpoint", builder.Configuration["OpenAI:Endpoint"])
.WaitFor(agentHost);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17035;http://localhost:15044",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21250",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22031"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15044",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19106",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20097"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
11 changes: 11 additions & 0 deletions dotnet/samples/Hello-distributed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Hello world sample using the packaged agent host

To run this sample, we'll need to generate self-signed certificate, as the gRPC server on the agenthost is configured to use HTTPS.

Run the following commands from `\dotnet\samples\Hello-distributed\AppHost`

```bash
mkdir certs
dotnet dev-certs https -ep ./certs/devcert.pfx -p mysecurepass
dotnet dev-certs https --trust
```
Loading
Loading