Skip to content

Commit

Permalink
Fix sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Oct 31, 2024
1 parent 9a26a02 commit 14cec51
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
20 changes: 14 additions & 6 deletions playground/OpenAIEndToEnd/OpenAIEndToEnd.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -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<CognitiveServicesAccount>().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<CognitiveServicesAccount>().Single();
cognitiveAccount.Properties.DisableLocalAuth = false;
})
.WithDeployment(new("modelB1", "gpt-4o", "2024-05-13"))
.WithDeployment(new("modelB2", "gpt-4o", "2024-05-13"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@inject IConfiguration configuration

<div class="storybox" style="margin: 25%">
@foreach (var message in chatMessages)
@foreach (var message in chatMessages.Where(x => x.Role == ChatRole.Assistant))
{
<p style="font-size: 3em;">@message.Text</p>
}
Expand All @@ -14,27 +14,23 @@
</div>

@code {
[Inject(Key = "modelA1")]
public required IChatClient chatClient { get; set; }
[Inject(Key = "modelA1")]
public required IChatClient chatClient { get; set; }

private List<ChatMessage> chatMessages = new List<ChatMessage>();
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()
{
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatMessage> chatMessages = new List<ChatMessage>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}

0 comments on commit 14cec51

Please sign in to comment.