-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f773cc2
commit 7c639fa
Showing
12 changed files
with
438 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
dotnet/samples/Hello/Hello.ServiceDefaults/Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
dotnet/samples/Hello/Hello.ServiceDefaults/Hello.ServiceDefaults.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
dotnet/samples/Hello/Hello.Shared/MEAIHostingExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.