-
Notifications
You must be signed in to change notification settings - Fork 453
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
11e9f58
commit c565c53
Showing
13 changed files
with
155 additions
and
6 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
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
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
1 change: 0 additions & 1 deletion
1
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor
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
37 changes: 37 additions & 0 deletions
37
playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/UseIChatClient.razor
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,37 @@ | ||
@page "/useichatclient" | ||
@using Microsoft.Extensions.AI | ||
@inject IChatClient aiClient | ||
@inject ILogger<Home> logger | ||
@inject IConfiguration configuration | ||
|
||
<div class="storybox" style="margin: 25%"> | ||
@foreach (var message in chatMessages.Where(m => m.Role == ChatRole.Assistant)) | ||
{ | ||
<p style="font-size: 3em;">@message.Text</p> | ||
} | ||
|
||
<button @onclick="GenerateNextParagraph" autofocus>Generate</button> | ||
</div> | ||
|
||
@code { | ||
private List<ChatMessage> chatMessages = new List<ChatMessage> | ||
{ | ||
new(ChatRole.System, "Pick a random topic and write a sentence of a fictional story about it.") | ||
}; | ||
|
||
private async Task GenerateNextParagraph() | ||
{ | ||
if (chatMessages.Count > 1) | ||
{ | ||
chatMessages.Add(new (ChatRole.User, "Write the next sentence in the story.")); | ||
} | ||
|
||
var response = await aiClient.CompleteAsync(chatMessages); | ||
chatMessages.Add(response.Message); | ||
} | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await GenerateNextParagraph(); | ||
} | ||
} |
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
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
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
84 changes: 84 additions & 0 deletions
84
src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIChatClientExtensions.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,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Data.Common; | ||
using Aspire.Azure.AI.OpenAI; | ||
using Azure.AI.OpenAI; | ||
using Azure.Core.Extensions; | ||
using Microsoft.Extensions.AI; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenAI; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Provides extension methods for registering <see cref="IChatClient"/> as a singleton in the services provided by the <see cref="IHostApplicationBuilder"/>. | ||
/// </summary> | ||
public static class AspireAzureOpenAIChatClientExtensions | ||
{ | ||
private const string DeployentKey = "Deployment"; | ||
private const string ModelKey = "Model"; | ||
|
||
/// <summary> | ||
/// Registers a singleton <see cref="IChatClient"/> in the services provided by the <paramref name="builder"/>. | ||
/// | ||
/// Additionally, registers the underlying <see cref="AzureOpenAIClient"/> and <see cref="OpenAIClient"/> as singleton services. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> | ||
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param> | ||
/// <param name="configurePipeline">An optional method that can be used for customizing the <see cref="IChatClient"/> pipeline.</param> | ||
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="AzureOpenAISettings"/>. It's invoked after the settings are read from the configuration.</param> | ||
/// <param name="configureClientBuilder">An optional method that can be used for customizing the <see cref="IAzureClientBuilder{AzureOpenAIClient, AzureOpenAIClientOptions}"/>.</param> | ||
/// <param name="deploymentName">Optionally specifies the deployment name. If not specified, a value will be taken from the connection string.</param> | ||
/// <remarks>Reads the configuration from "Aspire.Azure.AI.OpenAI" section.</remarks> | ||
public static void AddAzureOpenAIChatClient( | ||
this IHostApplicationBuilder builder, | ||
string connectionName, | ||
Func<ChatClientBuilder, ChatClientBuilder>? configurePipeline = null, | ||
Action<AzureOpenAISettings>? configureSettings = null, | ||
Action<IAzureClientBuilder<AzureOpenAIClient, AzureOpenAIClientOptions>>? configureClientBuilder = null, | ||
string? deploymentName = null) | ||
{ | ||
builder.AddAzureOpenAIClient(connectionName, configureSettings, configureClientBuilder); | ||
|
||
builder.Services.AddSingleton(services => | ||
{ | ||
var chatClientBuilder = new ChatClientBuilder(services); | ||
configurePipeline?.Invoke(chatClientBuilder); | ||
deploymentName ??= GetRequiredDeploymentName(builder.Configuration, connectionName); | ||
var innerClient = chatClientBuilder.Services | ||
.GetRequiredService<AzureOpenAIClient>() | ||
.AsChatClient(deploymentName); | ||
return chatClientBuilder.Use(innerClient); | ||
}); | ||
} | ||
|
||
private static string GetRequiredDeploymentName(IConfiguration configuration, string connectionName) | ||
{ | ||
string? deploymentName = null; | ||
|
||
if (configuration.GetConnectionString(connectionName) is string connectionString) | ||
{ | ||
var connectionBuilder = new DbConnectionStringBuilder { ConnectionString = connectionString }; | ||
deploymentName = (connectionBuilder[DeployentKey] ?? connectionBuilder[ModelKey]).ToString(); | ||
} | ||
|
||
var configurationSectionName = AspireAzureOpenAIExtensions.DefaultConfigSectionName; | ||
if (string.IsNullOrEmpty(deploymentName)) | ||
{ | ||
var configSection = configuration.GetSection(configurationSectionName); | ||
deploymentName = configSection[DeployentKey]; | ||
} | ||
|
||
if (string.IsNullOrEmpty(deploymentName)) | ||
{ | ||
throw new InvalidOperationException($"An {nameof(IChatClient)} could not be configured. Ensure a '{DeployentKey}' or '{ModelKey}' value is provided in 'ConnectionStrings:{connectionName}', or specify a '{DeployentKey}' in the '{configurationSectionName}' configuration section, or specify a '{nameof(deploymentName)}' in the call to {nameof(AddAzureOpenAIChatClient)}."); | ||
} | ||
|
||
return deploymentName; | ||
} | ||
} |
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
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
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
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,13 @@ | ||
<Project> | ||
<ItemGroup> | ||
<!-- | ||
Microsoft.Extensions.AI depends on 9.x packages, even on net8.0, so we have to override central package management | ||
to avoid "package downgrade" build errors. This is only used when referencing Aspire.OpenAI and doesn't break | ||
compatibility with net8.0. | ||
--> | ||
<PackageReference Include="Microsoft.Extensions.Primitives" VersionOverride="9.0.0-*" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" VersionOverride="9.0.0-*" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" VersionOverride="9.0.0-*" /> | ||
<PackageReference Include="System.Text.Json" VersionOverride="9.0.0-*" /> | ||
</ItemGroup> | ||
</Project> |