From dd7141890de374bbd7d5e378ebf3e648aa941055 Mon Sep 17 00:00:00 2001 From: Nicola Paro Date: Wed, 7 Feb 2024 21:57:45 +0100 Subject: [PATCH] fixed client factory --- .../Components/ChatGptStream.razor | 38 +++++++++++++++++- samples/ChatGptBlazor.Wasm/Program.cs | 24 +---------- .../Properties/launchSettings.json | 40 ++++++++----------- src/ChatGptNet/ChatGptClientFactory.cs | 12 +++--- ...atGptFactoryServiceCollectionExtensions.cs | 34 +++------------- src/ChatGptNet/ChatGptOptionsBuilder.cs | 2 +- src/ChatGptNet/IChatGptClientFactory.cs | 4 +- 7 files changed, 69 insertions(+), 85 deletions(-) diff --git a/samples/ChatGptBlazor.Wasm/Components/ChatGptStream.razor b/samples/ChatGptBlazor.Wasm/Components/ChatGptStream.razor index 4d73032..15d95b8 100644 --- a/samples/ChatGptBlazor.Wasm/Components/ChatGptStream.razor +++ b/samples/ChatGptBlazor.Wasm/Components/ChatGptStream.razor @@ -1,14 +1,33 @@ @using ChatGptNet.Extensions; @using ChatGptNet.Models; @using ChatGptNet; +@using ChatGptNet.ServiceConfigurations @using Markdig; -@inject IChatGptClient chatGptClient +@inject IChatGptClientFactory chatGptClientFactory @inject IJSRuntime jsRuntime
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
@foreach (var message in messages) @@ -62,6 +81,12 @@ private string? message; private bool isAsking; + private string azureResource; + private string apiKey; + private string model; + + private IChatGptClient? chatGptClient = null; + private bool CanSendMessage() => !string.IsNullOrWhiteSpace(message) && !isAsking; @@ -73,6 +98,17 @@ } } + private void Connect() + { + chatGptClient = chatGptClientFactory.CreateClient(options => + { + options.UseAzure(azureResource, apiKey, authenticationType: AzureAuthenticationType.ApiKey); + options.DefaultModel = model; + options.MessageLimit = 16; // Default: 10 + options.MessageExpiration = TimeSpan.FromMinutes(5); // Default: 1 hour + }); + } + private async Task AskAsync() { if (!isAsking) diff --git a/samples/ChatGptBlazor.Wasm/Program.cs b/samples/ChatGptBlazor.Wasm/Program.cs index 8369e49..595caf9 100644 --- a/samples/ChatGptBlazor.Wasm/Program.cs +++ b/samples/ChatGptBlazor.Wasm/Program.cs @@ -1,5 +1,6 @@ using ChatGptBlazor.Wasm; using ChatGptNet; +using ChatGptNet.ServiceConfigurations; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; @@ -10,27 +11,6 @@ builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); // Adds ChatGPT service and configure options via code. -builder.Services.AddChatGptClientFactory(options => -{ - // OpenAI. - //options.UseOpenAI(apiKey: "", organization: ""); - - // Azure OpenAI Service. - options.UseAzure(resourceName: "", apiKey: "", authenticationType: AzureAuthenticationType.ApiKey); - - options.DefaultModel = "my-model"; - options.MessageLimit = 16; // Default: 10 - options.MessageExpiration = TimeSpan.FromMinutes(5); // Default: 1 hour -}, -httpClient => -{ - // Configures retry policy on the inner HttpClient using Polly. - httpClient.AddStandardResilienceHandler(options => - { - options.AttemptTimeout.Timeout = TimeSpan.FromMinutes(1); - options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(3); - options.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(3); - }); -}); +builder.Services.AddChatGptClientFactory(); await builder.Build().RunAsync(); diff --git a/samples/ChatGptBlazor.Wasm/Properties/launchSettings.json b/samples/ChatGptBlazor.Wasm/Properties/launchSettings.json index f9af232..deec97b 100644 --- a/samples/ChatGptBlazor.Wasm/Properties/launchSettings.json +++ b/samples/ChatGptBlazor.Wasm/Properties/launchSettings.json @@ -1,40 +1,32 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:8206", - "sslPort": 44385 - } - }, "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "http://localhost:5297", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "http://localhost:5297" }, "https": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:7021;http://localhost:5297", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, + }, + "dotnetRunMessages": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "applicationUrl": "https://localhost:7021;http://localhost:5297" + } + }, + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:8206", + "sslPort": 44385 } } -} +} \ No newline at end of file diff --git a/src/ChatGptNet/ChatGptClientFactory.cs b/src/ChatGptNet/ChatGptClientFactory.cs index 24d7f8e..3d0736a 100644 --- a/src/ChatGptNet/ChatGptClientFactory.cs +++ b/src/ChatGptNet/ChatGptClientFactory.cs @@ -6,25 +6,25 @@ internal class ChatGptClientFactory : IChatGptClientFactory { private readonly IServiceProvider services; private readonly IChatGptCache chatGptCache; - private readonly ChatGptOptions defaultOptions; + private readonly ChatGptOptionsBuilder defaultOptions; - public ChatGptClientFactory(IServiceProvider services, IChatGptCache chatGptCache, ChatGptOptions defaultOptions) + public ChatGptClientFactory(IServiceProvider services, IChatGptCache chatGptCache, ChatGptOptionsBuilder defaultOptions) { this.services = services; this.chatGptCache = chatGptCache; this.defaultOptions = defaultOptions; } - public IChatGptClient CreateClient(Action? setupAction) + public IChatGptClient CreateClient(Action? setupAction) { var options = defaultOptions with { }; if (setupAction is not null) setupAction(services, options); - return new ChatGptClient(new HttpClient(), chatGptCache, options); + return new ChatGptClient(new HttpClient(), chatGptCache, options.Build()); } - public IChatGptClient CreateClient(Action? setupAction) + public IChatGptClient CreateClient(Action? setupAction) { if (setupAction is null) return CreateClient(); @@ -33,6 +33,6 @@ public IChatGptClient CreateClient(Action? setupAction) } public IChatGptClient CreateClient() { - return CreateClient((Action?)null); + return CreateClient((Action?)null); } } diff --git a/src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs b/src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs index 06bf59d..4523299 100644 --- a/src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs +++ b/src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs @@ -42,44 +42,20 @@ public static IServiceCollection AddChatGptClientFactory(this IServiceCollection return services.AddChatGptClientFactory(null); } - /// - /// Registers a instance reading configuration from the specified source. - /// - /// The to add services to. - /// The being bound. - /// The name of the configuration section that holds ChatGPT settings (default: ChatGPT). - /// A reference to this instance after the operation has completed. - public static IServiceCollection AddChatGptClientFactory(this IServiceCollection services, IConfiguration configuration, string sectionName = "ChatGPT") - { - ArgumentNullException.ThrowIfNull(services); - ArgumentNullException.ThrowIfNull(configuration); - - var options = new ChatGptOptions(); - var configurationSection = configuration.GetSection(sectionName); - configurationSection.Bind(options); - - // Creates the service configuration (OpenAI or Azure) according to the configuration settings. - options.ServiceConfiguration = ChatGptServiceConfiguration.Create(configurationSection); - - SetMissingDefaults(options); - - services.AddChatGptClientFactoryCore(options); - - return services; - } - - private static void AddChatGptClientFactoryCore(this IServiceCollection services, ChatGptOptionsBuilder options) + private static IServiceCollection AddChatGptClientFactoryCore(this IServiceCollection services, ChatGptOptionsBuilder deafultOptions) { services.AddMemoryCache(); services.AddSingleton(); var httpClientBuilder = services.AddHttpClient(); services.AddSingleton( - s => new ChatGptClientFactory(s, s.GetRequiredService(), options) + s => new ChatGptClientFactory(s, s.GetRequiredService(), deafultOptions) ); + + return services; } - private static void SetMissingDefaults(ChatGptOptionsBuilder options) + private static void SetMissingDefaults(ChatGptOptions options) { // If the provider is OpenAI and no default model has been specified, uses gpt-3.5-turbo by default. if (options.ServiceConfiguration is OpenAIChatGptServiceConfiguration && string.IsNullOrWhiteSpace(options.DefaultModel)) diff --git a/src/ChatGptNet/ChatGptOptionsBuilder.cs b/src/ChatGptNet/ChatGptOptionsBuilder.cs index 721a801..5b13769 100644 --- a/src/ChatGptNet/ChatGptOptionsBuilder.cs +++ b/src/ChatGptNet/ChatGptOptionsBuilder.cs @@ -8,7 +8,7 @@ namespace ChatGptNet; /// /// Builder class to define settings for configuring ChatGPT. /// -public class ChatGptOptionsBuilder +public record ChatGptOptionsBuilder { /// /// Gets or sets the configuration settings for accessing the service. diff --git a/src/ChatGptNet/IChatGptClientFactory.cs b/src/ChatGptNet/IChatGptClientFactory.cs index 4115e98..ed5f31b 100644 --- a/src/ChatGptNet/IChatGptClientFactory.cs +++ b/src/ChatGptNet/IChatGptClientFactory.cs @@ -10,14 +10,14 @@ public interface IChatGptClientFactory /// /// The to configure the provided . /// A new - IChatGptClient CreateClient(Action? setupAction); + IChatGptClient CreateClient(Action? setupAction); /// /// Creates a new instance of a ChatGptClient configured with the supplied action. /// /// The to configure the provided . /// A new - IChatGptClient CreateClient(Action? setupAction); + IChatGptClient CreateClient(Action? setupAction); /// /// Creates a new instance of a ChatGptClient.