-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net: Make AzureOpenAI service/model deployment configurable #10786
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
Merged
SergeyMenshykh
merged 2 commits into
feature-agents-hosting
from
agent-hosting-configuration-for-azureopenai
Mar 4, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
33 changes: 33 additions & 0 deletions
33
...emos/Hosting/Agent/ChatWithAgent.ApiService/Extensions/WebApplicationBuilderExtensions.cs
This file contains hidden or 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,33 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Azure.Identity; | ||
using ChatWithAgent.Configuration; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.SemanticKernel; | ||
|
||
namespace ChatWithAgent.ApiService.Extensions; | ||
|
||
/// <summary> | ||
/// Web application builder extensions. | ||
/// </summary> | ||
internal static class WebApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Azure OpenAI services to the web application. | ||
/// </summary> | ||
/// <param name="builder">The web application builder.</param> | ||
/// <param name="hostConfig">The host configuration.</param> | ||
internal static void AddAzureOpenAIServices(this WebApplicationBuilder builder, HostConfig hostConfig) | ||
{ | ||
// Add AzureOpenAI client. | ||
builder.AddAzureOpenAIClient( | ||
AzureOpenAIChatConfig.ConnectionStringName, | ||
(settings) => settings.Credential = builder.Environment.IsProduction() | ||
? new DefaultAzureCredential() | ||
: new AzureCliCredential()); // Use credentials from Azure CLI for local development. | ||
|
||
// Add AzureOpenAI chat completion service. | ||
builder.Services.AddAzureOpenAIChatCompletion(hostConfig.AzureOpenAIChat.DeploymentName); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
...Hosting/Agent/ChatWithAgent.AppHost/Extensions/DistributedApplicationBuilderExtensions.cs
This file contains hidden or 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,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using ChatWithAgent.Configuration; | ||
|
||
namespace ChatWithAgent.AppHost.Extensions; | ||
|
||
/// <summary> | ||
/// Distributed application builder extensions. | ||
/// </summary> | ||
internal static class DistributedApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Azure OpenAI service and OpenAI model(s) to the distributed application. | ||
/// </summary> | ||
/// <param name="builder">The distributed application builder.</param> | ||
/// <param name="config">The host configuration.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{IResourceWithConnectionString}"/>.</returns> | ||
internal static IResourceBuilder<IResourceWithConnectionString> AddAzureOpenAI(this IDistributedApplicationBuilder builder, HostConfig config) | ||
{ | ||
if (builder.ExecutionContext.IsPublishMode) | ||
{ | ||
// Deploy and provision Azure OpenAI service with AI models | ||
return builder | ||
.AddAzureOpenAI(AzureOpenAIChatConfig.ConnectionStringName) | ||
.AddDeployment(new AzureOpenAIDeployment( | ||
name: config.AzureOpenAIChat.DeploymentName, | ||
modelName: config.AzureOpenAIChat.ModelName, | ||
modelVersion: config.AzureOpenAIChat.ModelVersion) | ||
); | ||
} | ||
|
||
// Use an existing Azure OpenAI service via connection string | ||
return builder.AddConnectionString(AzureOpenAIChatConfig.ConnectionStringName); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...samples/Demos/Hosting/Agent/ChatWithAgent.AppHost/Extensions/ResourceBuilderExtensions.cs
This file contains hidden or 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,42 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using ChatWithAgent.Configuration; | ||
|
||
namespace ChatWithAgent.AppHost.Extensions; | ||
|
||
/// <summary> | ||
/// Resource builder extensions. | ||
/// </summary> | ||
public static class ResourceBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds host configuration as environment variables to the resource. | ||
/// </summary> | ||
/// <typeparam name="T">The resource type.</typeparam> | ||
/// <param name="builder">The resource builder.</param> | ||
/// <param name="config">The host configuration.</param> | ||
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns> | ||
public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> builder, HostConfig config) where T : IResourceWithEnvironment | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
ArgumentNullException.ThrowIfNull(config); | ||
|
||
// Add configured AI chat service to the environment variables so that Api Service can access it. | ||
builder.WithEnvironment(nameof(config.AIChatService), config.AIChatService); | ||
|
||
switch (config.AIChatService) | ||
{ | ||
case AzureOpenAIChatConfig.ConfigSectionName: | ||
{ | ||
// Add Azure OpenAI chat model deployment name to environment variables so that Api Service can access it. | ||
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{AzureOpenAIChatConfig.ConfigSectionName}__{nameof(config.AzureOpenAIChat.DeploymentName)}", config.AzureOpenAIChat.DeploymentName); | ||
break; | ||
} | ||
|
||
default: | ||
throw new NotSupportedException($"AI service '{config.AIChatService}' is not supported."); | ||
} | ||
|
||
return builder; | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
dotnet/samples/Demos/Hosting/Agent/ChatWithAgent.Configuration/AzureOpenAIChatConfig.cs
This file contains hidden or 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,37 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ChatWithAgent.Configuration; | ||
|
||
/// <summary> | ||
/// Azure OpenAI chat configuration. | ||
/// </summary> | ||
public sealed class AzureOpenAIChatConfig | ||
{ | ||
/// <summary> | ||
/// Configuration section name. | ||
/// </summary> | ||
public const string ConfigSectionName = "AzureOpenAIChat"; | ||
|
||
/// <summary> | ||
/// The name of the connection string of the Azure OpenAI chat service. | ||
/// </summary> | ||
public const string ConnectionStringName = ConfigSectionName; | ||
|
||
/// <summary> | ||
/// The name of the chat deployment. | ||
/// </summary> | ||
[Required] | ||
public string DeploymentName { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The name of the chat model. | ||
/// </summary> | ||
public string ModelName { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The chat model version. | ||
/// </summary> | ||
public string ModelVersion { get; set; } = string.Empty; | ||
} |
14 changes: 14 additions & 0 deletions
14
...amples/Demos/Hosting/Agent/ChatWithAgent.Configuration/ChatWithAgent.Configuration.csproj
This file contains hidden or 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
</ItemGroup> | ||
|
||
</Project> |
43 changes: 43 additions & 0 deletions
43
dotnet/samples/Demos/Hosting/Agent/ChatWithAgent.Configuration/HostConfig.cs
This file contains hidden or 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,43 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace ChatWithAgent.Configuration; | ||
|
||
/// <summary> | ||
/// Helper class for loading host configuration settings. | ||
/// </summary> | ||
public sealed class HostConfig | ||
{ | ||
/// <summary> | ||
/// The AI services section name. | ||
/// </summary> | ||
public const string AIServicesSectionName = "AIServices"; | ||
|
||
private readonly AzureOpenAIChatConfig _azureOpenAIChat = new(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HostConfig"/> class. | ||
/// </summary> | ||
/// <param name="configurationManager">The configuration manager.</param> | ||
public HostConfig(ConfigurationManager configurationManager) | ||
{ | ||
configurationManager | ||
.GetSection($"{AIServicesSectionName}:{AzureOpenAIChatConfig.ConfigSectionName}") | ||
.Bind(this._azureOpenAIChat); | ||
configurationManager | ||
.Bind(this); | ||
} | ||
|
||
/// <summary> | ||
/// The AI chat service to use. | ||
/// </summary> | ||
[Required] | ||
public string AIChatService { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The Azure OpenAI chat configuration. | ||
/// </summary> | ||
public AzureOpenAIChatConfig AzureOpenAIChat => this._azureOpenAIChat; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.