-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
…nt (#45)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace ChatGptNet; | ||
|
||
internal class ChatGptClientFactory : IChatGptClientFactory | ||
{ | ||
private readonly IServiceProvider services; | ||
private readonly IChatGptCache chatGptCache; | ||
private readonly ChatGptOptionsBuilder defaultOptions; | ||
|
||
public ChatGptClientFactory(IServiceProvider services, IChatGptCache chatGptCache, ChatGptOptionsBuilder defaultOptions) | ||
{ | ||
this.services = services; | ||
this.chatGptCache = chatGptCache; | ||
this.defaultOptions = defaultOptions; | ||
} | ||
|
||
public IChatGptClient CreateClient(Action<IServiceProvider, ChatGptOptionsBuilder>? setupAction) | ||
{ | ||
var options = defaultOptions with { }; | ||
|
||
if (setupAction is not null) | ||
setupAction(services, options); | ||
|
||
return new ChatGptClient(new HttpClient(), chatGptCache, options.Build()); | ||
} | ||
public IChatGptClient CreateClient(Action<ChatGptOptionsBuilder>? setupAction) | ||
{ | ||
if (setupAction is null) | ||
return CreateClient(); | ||
|
||
return CreateClient((s, o) => setupAction(o)); | ||
} | ||
public IChatGptClient CreateClient() | ||
{ | ||
return CreateClient((Action<IServiceProvider, ChatGptOptionsBuilder>?)null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using ChatGptNet.Models; | ||
using ChatGptNet.ServiceConfigurations; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace ChatGptNet; | ||
/// <summary> | ||
/// Provides extension methods for adding ChatGPT Client Factory support in NET applications. | ||
/// </summary> | ||
public static class ChatGptFactoryServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a <see cref="ChatGptClientFactory"/> instance with the specified options. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> | ||
/// <param name="setupAction">The <see cref="Action{ChatGptOptions}"/> to configure the provided <see cref="ChatGptOptions"/>.</param> | ||
Check warning on line 22 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
Check warning on line 22 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
|
||
/// <returns>A reference to this instance after the operation has completed.</returns> | ||
public static IServiceCollection AddChatGptClientFactory(this IServiceCollection services, Action<ChatGptOptionsBuilder> builder) | ||
Check warning on line 24 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
Check warning on line 24 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
Check warning on line 24 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
|
||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
var options = new ChatGptOptionsBuilder(); | ||
if (builder is not null) | ||
builder.Invoke(options); | ||
|
||
return AddChatGptClientFactoryCore(services, options); | ||
} | ||
|
||
/// <summary> | ||
/// Registers a <see cref="ChatGptClientFactory"/> instance. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> | ||
/// <returns>A reference to this instance after the operation has completed.</returns> | ||
public static IServiceCollection AddChatGptClientFactory(this IServiceCollection services) | ||
{ | ||
return services.AddChatGptClientFactory(null); | ||
Check warning on line 42 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
Check warning on line 42 in src/ChatGptNet/ChatGptFactoryServiceCollectionExtensions.cs GitHub Actions / Analyze (csharp)
|
||
} | ||
|
||
private static IServiceCollection AddChatGptClientFactoryCore(this IServiceCollection services, ChatGptOptionsBuilder deafultOptions) | ||
{ | ||
services.AddMemoryCache(); | ||
services.AddSingleton<IChatGptCache, ChatGptMemoryCache>(); | ||
|
||
var httpClientBuilder = services.AddHttpClient<IChatGptClientFactory, ChatGptClientFactory>(); | ||
services.AddSingleton<IChatGptClientFactory, ChatGptClientFactory>( | ||
s => new ChatGptClientFactory(s, s.GetRequiredService<IChatGptCache>(), deafultOptions) | ||
); | ||
|
||
return services; | ||
} | ||
|
||
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)) | ||
{ | ||
options.DefaultModel = OpenAIChatGptModels.Gpt35Turbo; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Represents the default builder for configuring ChatGPT client factory. | ||
/// </summary> | ||
/// <seealso cref="IChatGptBuilder"/>"/> | ||
public class ChatGptClientFactoryBuilder : IChatGptClientFactoryBuilder | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="IServiceCollection"/> where ChatGPT services are registered. | ||
/// </summary> | ||
public IServiceCollection Services { get; } | ||
|
||
/// <inheritdoc/> | ||
/// <summary> | ||
/// Gets the <see cref="IHttpClientBuilder"/> used to configure the <see cref="HttpClient"/> used by ChatGPT. | ||
/// </summary> | ||
public IHttpClientBuilder HttpClientBuilder { get; } | ||
|
||
internal ChatGptClientFactoryBuilder(IServiceCollection services, IHttpClientBuilder httpClientBuilder) | ||
{ | ||
Services = services; | ||
HttpClientBuilder = httpClientBuilder; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Represents a builder for configuring ChatGPT client factory. | ||
/// </summary> | ||
public interface IChatGptClientFactoryBuilder | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="IServiceCollection"/> where ChatGPT services are registered. | ||
/// </summary> | ||
IServiceCollection Services { get; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IHttpClientBuilder"/> used to configure the <see cref="HttpClient"/> used by ChatGPT. | ||
/// </summary> | ||
IHttpClientBuilder HttpClientBuilder { get; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace ChatGptNet; | ||
|
||
/// <summary> | ||
/// Provides methods to create new instances of <see cref="IChatGptClient"/> at runtime | ||
/// </summary> | ||
public interface IChatGptClientFactory | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of a ChatGptClient configured with the supplied action. | ||
/// </summary> | ||
/// <param name="setupAction">The <see cref="Action{ChatGptOptions}"/> to configure the provided <see cref="ChatGptOptions"/>.</param> | ||
/// <returns>A new <see cref="IChatGptClient"/></returns> | ||
IChatGptClient CreateClient(Action<IServiceProvider, ChatGptOptionsBuilder>? setupAction); | ||
|
||
/// <summary> | ||
/// Creates a new instance of a ChatGptClient configured with the supplied action. | ||
/// </summary> | ||
/// <param name="setupAction">The <see cref="Action{ChatGptOptions}"/> to configure the provided <see cref="ChatGptOptions"/>.</param> | ||
/// <returns>A new <see cref="IChatGptClient"/></returns> | ||
IChatGptClient CreateClient(Action<ChatGptOptionsBuilder>? setupAction); | ||
|
||
/// <summary> | ||
/// Creates a new instance of a ChatGptClient. | ||
/// </summary> | ||
/// <returns>A new <see cref="IChatGptClient"/></returns> | ||
IChatGptClient CreateClient(); | ||
|
||
} |