-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IInternalClock abstractions; Add ServiceCollectionExtensions
- Loading branch information
Showing
20 changed files
with
338 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
OpenAI.ChatGpt.EntityFrameworkCore/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenAI.ChatGpt.Interfaces; | ||
using static OpenAI.ChatGpt.Extensions.ServiceCollectionExtensions; | ||
|
||
namespace OpenAI.ChatGpt.EntityFrameworkCore.Extensions; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the <see cref="IChatHistoryStorage"/> implementation using Entity Framework Core. | ||
/// </summary> | ||
public static IServiceCollection AddChatGptEntityFrameworkIntegration( | ||
this IServiceCollection services, | ||
Action<DbContextOptionsBuilder> optionsAction, | ||
string credentialsConfigSectionPath = CredentialsConfigSectionPathDefault, | ||
string completionsConfigSectionPath = CompletionsConfigSectionPathDefault) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
ArgumentNullException.ThrowIfNull(optionsAction); | ||
if (string.IsNullOrWhiteSpace(credentialsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(credentialsConfigSectionPath)); | ||
} | ||
if (string.IsNullOrWhiteSpace(completionsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(completionsConfigSectionPath)); | ||
} | ||
|
||
services.AddChatGptIntegrationCore(credentialsConfigSectionPath, completionsConfigSectionPath); | ||
services.AddDbContext<ChatGptDbContext>(optionsAction); | ||
services.AddScoped<IChatHistoryStorage, EfChatHistoryStorage>(); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenAI.ChatGpt.Interfaces; | ||
using OpenAI.ChatGpt.Internal; | ||
using OpenAI.ChatGpt.Models; | ||
|
||
namespace OpenAI.ChatGpt.Extensions; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public const string CredentialsConfigSectionPathDefault = "ChatGptCredentials"; | ||
public const string CompletionsConfigSectionPathDefault = "ChatCompletionsConfig"; | ||
|
||
public static IServiceCollection AddChatGptInMemoryIntegration( | ||
this IServiceCollection services, | ||
string credentialsConfigSectionPath = CredentialsConfigSectionPathDefault, | ||
string completionsConfigSectionPath = CompletionsConfigSectionPathDefault) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
if (string.IsNullOrWhiteSpace(credentialsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(credentialsConfigSectionPath)); | ||
} | ||
if (string.IsNullOrWhiteSpace(completionsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(completionsConfigSectionPath)); | ||
} | ||
services.AddChatGptIntegrationCore(credentialsConfigSectionPath, completionsConfigSectionPath); | ||
services.AddSingleton<IChatHistoryStorage, InMemoryChatHistoryStorage>(); | ||
return services; | ||
} | ||
public static IServiceCollection AddChatGptIntegrationCore( | ||
this IServiceCollection services, | ||
string credentialsConfigSectionPath = CredentialsConfigSectionPathDefault, | ||
string completionsConfigSectionPath = CompletionsConfigSectionPathDefault) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
if (string.IsNullOrWhiteSpace(credentialsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(credentialsConfigSectionPath)); | ||
} | ||
if (string.IsNullOrWhiteSpace(completionsConfigSectionPath)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", | ||
nameof(completionsConfigSectionPath)); | ||
} | ||
|
||
services.AddOptions<ChatGptCredentials>() | ||
.BindConfiguration(credentialsConfigSectionPath) | ||
.ValidateDataAnnotations(); | ||
services.AddOptions<ChatCompletionsConfig>() | ||
.BindConfiguration(completionsConfigSectionPath) | ||
.Configure(_ => { }) | ||
.ValidateDataAnnotations(); | ||
|
||
services.AddHttpClient(); | ||
|
||
services.AddSingleton<IInternalClock, InternalClockUtc>(); | ||
services.AddSingleton<ChatGPTFactory>(); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.