Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@
"ChatWithCustomData-CSharp.AppHost/**",
"ChatWithCustomData-CSharp.ServiceDefaults/**",
"ChatWithCustomData-CSharp.Web/Program.Aspire.cs",
"ChatWithCustomData-CSharp.Web/ResponseClientHelper.cs",
"README.Aspire.md",
"*.sln"
]
},
{
"condition": "(IsAspire && !IsOpenAI && !IsAzureOpenAI)",
"exclude": [
"ChatWithCustomData-CSharp.Web/ResponseClientHelper.cs"
]
},
{
"condition": "(IsAspire)",
"exclude": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>d5681fae-b21b-4114-b781-48180f08c0c4</UserSecretsId>
<!--#if (IsAzureOpenAI || IsOpenAI)
<NoWarn>OPENAI001</NoWarn>
#endif -->
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,22 @@
var responseText = new TextContent("");
currentResponseMessage = new ChatMessage(ChatRole.Assistant, [responseText]);
currentResponseCancellation = new();
@*#if (IsAzureOpenAI || IsOpenAI)
await foreach (var update in ChatClient.GetStreamingResponseAsync([userMessage], chatOptions, currentResponseCancellation.Token))
{
messages.AddMessages(update, filter: c => c is not TextContent);
responseText.Text += update.Text;
chatOptions.ConversationId = update.ConversationId; //Update conversation ID from current response
ChatMessageItem.NotifyChanged(currentResponseMessage);
}
#else*@
await foreach (var update in ChatClient.GetStreamingResponseAsync([.. messages], chatOptions, currentResponseCancellation.Token))
{
messages.AddMessages(update, filter: c => c is not TextContent);
responseText.Text += update.Text;
ChatMessageItem.NotifyChanged(currentResponseMessage);
}
@*#endif*@

// Store the final response in the conversation, and begin getting suggestions
messages.Add(currentResponseMessage!);
Expand All @@ -96,6 +106,9 @@
CancelAnyCurrentResponse();
messages.Clear();
messages.Add(new(ChatRole.System, SystemPrompt));
@*#if (IsAzureOpenAI || IsOpenAI)
chatOptions.ConversationId = null;
#endif*@
chatSuggestions?.Clear();
await chatInput!.FocusAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#else
var openai = builder.AddAzureOpenAIClient("openai");
#endif
openai.AddChatClient("gpt-4o-mini")
openai.AddResponsesChatClient("gpt-4o-mini")
.UseFunctionInvocation()
.UseOpenTelemetry(configure: c =>
c.EnableSensitiveData = builder.Environment.IsDevelopment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
// dotnet user-secrets set OpenAI:Key YOUR-API-KEY
var openAIClient = new OpenAIClient(
new ApiKeyCredential(builder.Configuration["OpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: OpenAI:Key. See the README for details.")));
var chatClient = openAIClient.GetChatClient("gpt-4o-mini").AsIChatClient();
var chatClient = openAIClient.GetOpenAIResponseClient("gpt-4o-mini").AsIChatClient();
var embeddingGenerator = openAIClient.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
#elif (IsAzureAiFoundry)

Expand All @@ -66,7 +66,7 @@
#else
new ApiKeyCredential(builder.Configuration["AzureOpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAi:Key. See the README for details.")));
#endif
var chatClient = azureOpenAi.GetChatClient("gpt-4o-mini").AsIChatClient();
var chatClient = azureOpenAi.GetOpenAIResponseClient("gpt-4o-mini").AsIChatClient();
var embeddingGenerator = azureOpenAi.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.AI;
using Aspire.OpenAI;
using OpenAI;

//namespace chat.aspire.Web;
namespace Microsoft.Extensions.Hosting;

public static class ResponseClientHelper
{

public static ChatClientBuilder AddResponsesChatClient(this AspireOpenAIClientBuilder builder, string? deploymentName)
{
ArgumentNullException.ThrowIfNull(builder, "builder");

return builder.HostBuilder.Services.AddChatClient((IServiceProvider services) => CreateInnerChatClient(services, builder, deploymentName));
}
private static IChatClient CreateInnerChatClient(IServiceProvider services, AspireOpenAIClientBuilder builder, string? deploymentName)
{
OpenAIClient openAIClient = builder.ServiceKey is null ? services.GetRequiredService<OpenAIClient>() : services.GetRequiredKeyedService<OpenAIClient>(builder.ServiceKey);

if (deploymentName is null)
{
deploymentName = "gpt-4o-mini";
}

IChatClient chatClient = openAIClient.GetOpenAIResponseClient(deploymentName).AsIChatClient();

if (builder.DisableTracing)
{
return chatClient;
}

var loggerFactory = services.GetService<ILoggerFactory>();
return new OpenTelemetryChatClient(chatClient, loggerFactory?.CreateLogger(typeof(OpenTelemetryChatClient)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@
var responseText = new TextContent("");
currentResponseMessage = new ChatMessage(ChatRole.Assistant, [responseText]);
currentResponseCancellation = new();
await foreach (var update in ChatClient.GetStreamingResponseAsync([.. messages], chatOptions, currentResponseCancellation.Token))
await foreach (var update in ChatClient.GetStreamingResponseAsync([userMessage], chatOptions, currentResponseCancellation.Token))
{
messages.AddMessages(update, filter: c => c is not TextContent);
responseText.Text += update.Text;
chatOptions.ConversationId = update.ConversationId; //Update conversation ID from current response
ChatMessageItem.NotifyChanged(currentResponseMessage);
}

Expand All @@ -96,6 +97,7 @@
CancelAnyCurrentResponse();
messages.Clear();
messages.Add(new(ChatRole.System, SystemPrompt));
chatOptions.ConversationId = null;
chatSuggestions?.Clear();
await chatInput!.FocusAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using aichatweb.Web.Services.Ingestion;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();

var openai = builder.AddAzureOpenAIClient("openai");
openai.AddChatClient("gpt-4o-mini")
openai.AddResponsesChatClient("gpt-4o-mini")
.UseFunctionInvocation()
.UseOpenTelemetry(configure: c =>
c.EnableSensitiveData = builder.Environment.IsDevelopment());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.AI;
using Aspire.OpenAI;
using OpenAI;

//namespace chat.aspire.Web;
namespace Microsoft.Extensions.Hosting;

public static class ResponseClientHelper
{

public static ChatClientBuilder AddResponsesChatClient(this AspireOpenAIClientBuilder builder, string? deploymentName)
{
ArgumentNullException.ThrowIfNull(builder, "builder");

return builder.HostBuilder.Services.AddChatClient((IServiceProvider services) => CreateInnerChatClient(services, builder, deploymentName));
}
private static IChatClient CreateInnerChatClient(IServiceProvider services, AspireOpenAIClientBuilder builder, string? deploymentName)
{
OpenAIClient openAIClient = builder.ServiceKey is null ? services.GetRequiredService<OpenAIClient>() : services.GetRequiredKeyedService<OpenAIClient>(builder.ServiceKey);

if (deploymentName is null)
{
deploymentName = "gpt-4o-mini";
}

IChatClient chatClient = openAIClient.GetOpenAIResponseClient(deploymentName).AsIChatClient();

if (builder.DisableTracing)
{
return chatClient;
}

var loggerFactory = services.GetService<ILoggerFactory>();
return new OpenTelemetryChatClient(chatClient, loggerFactory?.CreateLogger(typeof(OpenTelemetryChatClient)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>secret</UserSecretsId>
<NoWarn>OPENAI001</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
builder.Services.AddRazorComponents().AddInteractiveServerComponents();

var openai = builder.AddAzureOpenAIClient("openai");
openai.AddChatClient("gpt-4o-mini")
openai.AddResponsesChatClient("gpt-4o-mini")
.UseFunctionInvocation()
.UseOpenTelemetry(configure: c =>
c.EnableSensitiveData = builder.Environment.IsDevelopment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@
var responseText = new TextContent("");
currentResponseMessage = new ChatMessage(ChatRole.Assistant, [responseText]);
currentResponseCancellation = new();
await foreach (var update in ChatClient.GetStreamingResponseAsync([.. messages], chatOptions, currentResponseCancellation.Token))
await foreach (var update in ChatClient.GetStreamingResponseAsync([userMessage], chatOptions, currentResponseCancellation.Token))
{
messages.AddMessages(update, filter: c => c is not TextContent);
responseText.Text += update.Text;
chatOptions.ConversationId = update.ConversationId; //Update conversation ID from current response
ChatMessageItem.NotifyChanged(currentResponseMessage);
}

Expand All @@ -96,6 +97,7 @@
CancelAnyCurrentResponse();
messages.Clear();
messages.Add(new(ChatRole.System, SystemPrompt));
chatOptions.ConversationId = null;
chatSuggestions?.Clear();
await chatInput!.FocusAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// dotnet user-secrets set OpenAI:Key YOUR-API-KEY
var openAIClient = new OpenAIClient(
new ApiKeyCredential(builder.Configuration["OpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: OpenAI:Key. See the README for details.")));
var chatClient = openAIClient.GetChatClient("gpt-4o-mini").AsIChatClient();
var chatClient = openAIClient.GetOpenAIResponseClient("gpt-4o-mini").AsIChatClient();
var embeddingGenerator = openAIClient.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();

// You will need to set the endpoint and key to your own values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>secret</UserSecretsId>
<NoWarn>OPENAI001</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading