Skip to content

Commit

Permalink
Add samples resources back
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Dec 9, 2024
1 parent f773cc2 commit 7c639fa
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 45 deletions.
7 changes: 7 additions & 0 deletions dotnet/AutoGen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Core.Grpc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Runtime.Grpc.Tests", "test\Microsoft.AutoGen.Runtime.Grpc.Tests\Microsoft.AutoGen.Runtime.Grpc.Tests.csproj", "{8881C07D-5B57-4D7A-81D2-65A727315A2F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.ServiceDefaults", "samples\Hello\Hello.ServiceDefaults\Hello.ServiceDefaults.csproj", "{FACFAB26-0C21-4D9E-A855-875AEFF5948B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -146,6 +148,10 @@ Global
{8881C07D-5B57-4D7A-81D2-65A727315A2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8881C07D-5B57-4D7A-81D2-65A727315A2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8881C07D-5B57-4D7A-81D2-65A727315A2F}.Release|Any CPU.Build.0 = Release|Any CPU
{FACFAB26-0C21-4D9E-A855-875AEFF5948B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FACFAB26-0C21-4D9E-A855-875AEFF5948B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FACFAB26-0C21-4D9E-A855-875AEFF5948B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FACFAB26-0C21-4D9E-A855-875AEFF5948B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -172,6 +178,7 @@ Global
{6EF624FB-4247-138B-67FC-9ECC925FC4FA} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{FCCE32DE-8162-47F3-835D-2EF78D4381C3} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{8881C07D-5B57-4D7A-81D2-65A727315A2F} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{FACFAB26-0C21-4D9E-A855-875AEFF5948B} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
Expand Down
120 changes: 120 additions & 0 deletions dotnet/samples/Hello/Hello.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Extensions.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
88 changes: 88 additions & 0 deletions dotnet/samples/Hello/Hello.Shared/AgentsApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// AgentsApp.cs

using Google.Protobuf;
using Microsoft.AspNetCore.Builder;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Diagnostics.CodeAnalysis;

public static class AgentsApp
{
// need a variable to store the runtime instance
public static WebApplication? Host { get; private set; }

[MemberNotNull(nameof(Host))]
public static async ValueTask<WebApplication> StartAsync(WebApplicationBuilder? builder = null, AgentTypes? agentTypes = null, bool local = false)
{
builder ??= WebApplication.CreateBuilder();
if (local)
{
// start the server runtime
builder.AddInMemoryWorker();
builder.AddAgentHost();
builder.AddAgents(agentTypes);
}

//builder.AddServiceDefaults();
var app = builder.Build();
if (local)
{
// app.MapAgentService(local: true, useGrpc: false);
}
app.MapDefaultEndpoints();
Host = app;
await app.StartAsync().ConfigureAwait(false);
return Host;
}
public static async ValueTask<WebApplication> PublishMessageAsync(
string topic,
IMessage message,
WebApplicationBuilder? builder = null,
AgentTypes? agents = null,
bool local = false)
{
if (Host == null)
{
await StartAsync(builder, agents, local);
}
var client = Host.Services.GetRequiredService<Client>() ?? throw new InvalidOperationException("Host not started");
await client.PublishEventAsync(message, topic, new CancellationToken()).ConfigureAwait(true);
return Host;
}
public static async ValueTask ShutdownAsync()
{
if (Host == null)
{
throw new InvalidOperationException("Host not started");
}
await Host.StopAsync();
}

private static IHostApplicationBuilder AddAgents(this IHostApplicationBuilder builder, AgentTypes? agentTypes)
{
agentTypes ??= AgentTypes.GetAgentTypesFromAssembly()
?? throw new InvalidOperationException("No agent types found in the assembly");
foreach (var type in agentTypes.Types)
{
builder.AddAgent(type.Key, type.Value);
}
return builder;
}
}
public sealed class AgentTypes(Dictionary<string, Type> types)
{
public Dictionary<string, Type> Types { get; } = types;
public static AgentTypes? GetAgentTypesFromAssembly()
{
var agents = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => ReflectionHelper.IsSubclassOfGeneric(type, typeof(Agent))
&& !type.IsAbstract
&& !type.Name.Equals(nameof(Client)))
.ToDictionary(type => type.Name, type => type);

return new AgentTypes(agents);
}
}
16 changes: 16 additions & 0 deletions dotnet/samples/Hello/Hello.Shared/Hello.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
<PackageReference Include="Grpc.AspNetCore" />
<PackageReference Include="Grpc.Net.ClientFactory" />
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
<PackageReference Include="Aspire.Azure.AI.OpenAI" />
<PackageReference Include="Azure.AI.OpenAI" />
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.AI" />
<PackageReference Include="Microsoft.Extensions.AI.AzureAIInference" />
<PackageReference Include="Microsoft.Extensions.AI.Ollama" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="System.Text.Json" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Microsoft.AutoGen.Core\Microsoft.AutoGen.Core.csproj" />
<ProjectReference Include="..\Hello.ServiceDefaults\Hello.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions dotnet/samples/Hello/Hello.Shared/MEAIHostingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// MEAIHostingExtensions.cs

using Microsoft.Extensions.AI;
using Microsoft.Extensions.Hosting;

public static class MEAIHostingExtensions
{
public static IHostApplicationBuilder AddChatCompletionService(this IHostApplicationBuilder builder, string serviceName)
{
var pipeline = (ChatClientBuilder pipeline) => pipeline
.UseLogging()
.UseFunctionInvocation()
.UseOpenTelemetry(configure: c => c.EnableSensitiveData = true);

if (builder.Configuration[$"{serviceName}:ModelType"] == "ollama")
{
builder.AddOllamaChatClient(serviceName, pipeline);
}
else if (builder.Configuration[$"{serviceName}:ModelType"] == "openai" || builder.Configuration[$"{serviceName}:ModelType"] == "azureopenai")
{
builder.AddOpenAIChatClient(serviceName, pipeline);
}
else if (builder.Configuration[$"{serviceName}:ModelType"] == "azureaiinference")
{
builder.AddAzureChatClient(serviceName, pipeline);
}
else
{
throw new InvalidOperationException("Did not find a valid model implementation for the given service name ${serviceName}, valid supported implemenation types are ollama, openai, azureopenai, azureaiinference");
}
return builder;
}
}
Loading

0 comments on commit 7c639fa

Please sign in to comment.