From 14cec51ff003611a117919b3ea35d75895b7f0cf Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Thu, 31 Oct 2024 12:53:07 -0700 Subject: [PATCH] Fix sample --- .../OpenAIEndToEnd.AppHost/Program.cs | 20 +++++++++---- .../Components/Pages/Home.razor | 26 +++++++---------- .../Components/Pages/UseIChatClient.razor | 4 +-- ...OpenAIClientBuilderChatClientExtensions.cs | 29 ++++++++++--------- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs b/playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs index 1660f7532e..861348d753 100644 --- a/playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs +++ b/playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs @@ -1,17 +1,25 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Azure.Provisioning.CognitiveServices; + var builder = DistributedApplication.CreateBuilder(args); -var openaiA = builder.ExecutionContext.IsPublishMode - ? builder.AddAzureOpenAI("openaiA") - : builder.AddAzureOpenAIConnection("openaiA") +var openaiA = builder.AddAzureOpenAI("openaiA") + .ConfigureInfrastructure(infra => + { + var cognitiveAccount = infra.GetProvisionableResources().OfType().Single(); + cognitiveAccount.Properties.DisableLocalAuth = false; + }) .WithDeployment(new("modelA1", "gpt-4o", "2024-05-13")) ; -var openaiB = builder.ExecutionContext.IsPublishMode - ? builder.AddAzureOpenAI("openaiB") - : builder.AddAzureOpenAIConnection("openaiB") +var openaiB = builder.AddAzureOpenAI("openaiB") + .ConfigureInfrastructure(infra => + { + var cognitiveAccount = infra.GetProvisionableResources().OfType().Single(); + cognitiveAccount.Properties.DisableLocalAuth = false; + }) .WithDeployment(new("modelB1", "gpt-4o", "2024-05-13")) .WithDeployment(new("modelB2", "gpt-4o", "2024-05-13")); diff --git a/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor b/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor index 3dd091d0e9..f2e6badb4c 100644 --- a/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor +++ b/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/Home.razor @@ -5,7 +5,7 @@ @inject IConfiguration configuration
- @foreach (var message in chatMessages) + @foreach (var message in chatMessages.Where(x => x.Role == ChatRole.Assistant)) {

@message.Text

} @@ -14,27 +14,23 @@
@code { - [Inject(Key = "modelA1")] - public required IChatClient chatClient { get; set; } + [Inject(Key = "modelA1")] + public required IChatClient chatClient { get; set; } - private List chatMessages = new List(); + private List chatMessages = new List + { + new(ChatRole.System, "Pick a random topic and write a sentence of a fictional story about it.") + }; private async Task GenerateNextParagraph() { - string chatText = ""; - - if (chatMessages.Count == 0) + if (chatMessages.Count > 1) { - chatMessages.Add(new ChatMessage(ChatRole.System, chatText = "Pick a random topic and write a sentence of a fictional story about it.")); + chatMessages.Add(new(ChatRole.User, "Write the next sentence in the story.")); } - else - { - chatMessages.Add(new ChatMessage(ChatRole.User, chatText = "Write the next sentence in the story.")); - } - - var result = await chatClient.CompleteAsync(chatText); - chatMessages.Add(new ChatMessage(ChatRole.Assistant, result.Message.Text)); + var response = await chatClient.CompleteAsync(chatMessages); + chatMessages.Add(response.Message); this.StateHasChanged(); } diff --git a/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/UseIChatClient.razor b/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/UseIChatClient.razor index f7916c91c6..bfeb6705a5 100644 --- a/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/UseIChatClient.razor +++ b/playground/OpenAIEndToEnd/OpenAIEndToEnd.WebStory/Components/Pages/UseIChatClient.razor @@ -15,8 +15,8 @@ @code { - [Inject(Key = "modelA1")] - public required IChatClient aiClient { get; set; } + [Inject(Key = "modelA1")] + public required IChatClient aiClient { get; set; } private List chatMessages = new List { diff --git a/src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIClientBuilderChatClientExtensions.cs b/src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIClientBuilderChatClientExtensions.cs index 5c9eb0c84e..2eecd7d1f4 100644 --- a/src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIClientBuilderChatClientExtensions.cs +++ b/src/Components/Aspire.Azure.AI.OpenAI/AspireAzureOpenAIClientBuilderChatClientExtensions.cs @@ -47,7 +47,7 @@ public static AspireAzureOpenAIClientBuilder AddKeyedChatClient( { builder.HostBuilder.Services.TryAddKeyedSingleton( serviceKey, - (services, _) => CreateChatClient(services, builder, builder.ConnectionName, configurePipeline)); + (services, _) => CreateChatClient(services, builder, serviceKey, configurePipeline)); return builder; } @@ -65,34 +65,35 @@ private static IChatClient CreateChatClient( var chatClientBuilder = new ChatClientBuilder(services); configurePipeline?.Invoke(chatClientBuilder); + var deploymentSettings = GetDeployments(builder.HostBuilder.Configuration, builder.ConnectionName); + // If no deployment name is provided, we search for the first one (and maybe only one) in configuration if (deploymentName is null) { - deploymentName = GetRequiredDeploymentName(builder.HostBuilder.Configuration, builder.ConnectionName); + deploymentName = deploymentSettings.Models.Keys.FirstOrDefault(); + + if (string.IsNullOrEmpty(deploymentName)) + { + throw new InvalidOperationException($"An {nameof(IChatClient)} could not be configured. Ensure a deployment was defined ."); + } } - deploymentName ??= GetRequiredDeploymentName(builder.HostBuilder.Configuration, builder.ConnectionName); + if (!deploymentSettings.Models.TryGetValue(deploymentName, out var _)) + { + throw new InvalidOperationException($"An {nameof(IChatClient)} could not be configured. Ensure the deployment name '{deploymentName}' was defined ."); + } return chatClientBuilder.Use(openAiClient.AsChatClient(deploymentName)); } - private static string GetRequiredDeploymentName(IConfiguration configuration, string connectionName) + private static DeploymentModelSettings GetDeployments(IConfiguration configuration, string connectionName) { - string? deploymentName = null; - var configurationSectionName = $"{AspireAzureOpenAIExtensions.DefaultConfigSectionName}:{connectionName}"; var configSection = configuration.GetSection(configurationSectionName); var settings = new DeploymentModelSettings(); configSection.Bind(settings); - deploymentName = settings.Models.Keys.FirstOrDefault(); - - if (string.IsNullOrEmpty(deploymentName)) - { - throw new InvalidOperationException($"An {nameof(IChatClient)} could not be configured. Ensure a deployment was defined ."); - } - - return deploymentName; + return settings; } }