From 67038e085247843a0cc6f6c4b4ffb152f72e1fbc Mon Sep 17 00:00:00 2001 From: rodion-m Date: Mon, 24 Apr 2023 16:05:43 +0600 Subject: [PATCH] Rename Chat to ChatService; Release 2.4.0 --- .../Extensions/ServiceCollectionExtensions.cs | 8 ++++---- .../OpenAI.ChatGpt.AspNetCore.csproj | 2 +- .../OpenAI.ChatGpt.EntityFrameworkCore.csproj | 6 +++--- OpenAI.ChatGpt/ChatGPT.cs | 16 ++++++++-------- OpenAI.ChatGpt/{Chat.cs => ChatService.cs} | 4 ++-- OpenAI.ChatGpt/OpenAI.ChatGpt.csproj | 2 +- samples/ChatGpt.BlazorExample/Pages/Index.razor | 12 ++++++------ samples/ChatGpt.ConsoleExample/Program.cs | 4 ++-- samples/ChatGpt.SpectreConsoleExample/Program.cs | 10 +++++----- .../ChatGptTests.cs | 14 +++++++------- .../ChatGptServicesIntegrationTests.cs | 4 ++-- 11 files changed, 41 insertions(+), 41 deletions(-) rename OpenAI.ChatGpt/{Chat.cs => ChatService.cs} (98%) diff --git a/OpenAI.ChatGpt.AspNetCore/Extensions/ServiceCollectionExtensions.cs b/OpenAI.ChatGpt.AspNetCore/Extensions/ServiceCollectionExtensions.cs index 179f605..9c98368 100644 --- a/OpenAI.ChatGpt.AspNetCore/Extensions/ServiceCollectionExtensions.cs +++ b/OpenAI.ChatGpt.AspNetCore/Extensions/ServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddChatGptInMemoryIntegration( this IServiceCollection services, - bool injectInMemoryChat = true, + bool injectInMemoryChatService = true, string credentialsConfigSectionPath = CredentialsConfigSectionPathDefault, string completionsConfigSectionPath = CchatGPTConfigSectionPathDefault) { @@ -29,14 +29,14 @@ public static IServiceCollection AddChatGptInMemoryIntegration( } services.AddChatGptIntegrationCore(credentialsConfigSectionPath, completionsConfigSectionPath); services.AddSingleton(); - if(injectInMemoryChat) + if(injectInMemoryChatService) { - services.AddScoped(CreateChatGptChat); + services.AddScoped(CreateChatService); } return services; } - private static Chat CreateChatGptChat(IServiceProvider provider) + private static ChatService CreateChatService(IServiceProvider provider) { ArgumentNullException.ThrowIfNull(provider); var userId = Guid.Empty.ToString(); diff --git a/OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj b/OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj index bad232c..4e32596 100644 --- a/OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj +++ b/OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj @@ -8,7 +8,7 @@ OpenAI.ChatGPT.AspNetCore https://github.com/rodion-m/ChatGPT_API_dotnet OpenAI ChatGPT integration for .NET with DI - 2.3.0 + 2.4.0 OpenAI Chat Completions API (ChatGPT) integration with easy DI supporting (Microsoft.Extensions.DependencyInjection). It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams. https://github.com/rodion-m/ChatGPT_API_dotnet net6.0;net7.0 diff --git a/OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj b/OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj index 2490eac..9533a97 100644 --- a/OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj +++ b/OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj @@ -9,7 +9,7 @@ OpenAI.ChatGPT.EntityFrameworkCore https://github.com/rodion-m/ChatGPT_API_dotnet OpenAI ChatGPT integration for .NET with EF Core storage - 2.3.0 + 2.4.0 OpenAI Chat Completions API (ChatGPT) integration with DI and EF Core supporting. It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams. https://github.com/rodion-m/ChatGPT_API_dotnet net6.0;net7.0 @@ -25,8 +25,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/OpenAI.ChatGpt/ChatGPT.cs b/OpenAI.ChatGpt/ChatGPT.cs index fc714a3..72d0c9e 100644 --- a/OpenAI.ChatGpt/ChatGPT.cs +++ b/OpenAI.ChatGpt/ChatGPT.cs @@ -15,7 +15,7 @@ public class ChatGPT : IDisposable private readonly ITimeProvider _clock; private readonly ChatGPTConfig? _config; private readonly OpenAiClient _client; - private Chat? _currentChat; + private ChatService? _currentChat; /// /// Use this constructor to create chat conversation provider for the specific user. @@ -53,7 +53,7 @@ public ChatGPT( /// /// If you don't have users and don't want to save messages into database use this method. /// - public static Task CreateInMemoryChat( + public static Task CreateInMemoryChat( string apiKey, ChatGPTConfig? config = null, UserOrSystemMessage? initialDialog = null, @@ -71,7 +71,7 @@ public void Dispose() } /// Continues the last topic or starts a new one. - public async Task ContinueOrStartNewTopic(CancellationToken cancellationToken = default) + public async Task ContinueOrStartNewTopic(CancellationToken cancellationToken = default) { if (_currentChat is not null) return _currentChat; var topic = await _storage.GetMostRecentTopicOrNull(_userId, cancellationToken); @@ -81,7 +81,7 @@ public async Task ContinueOrStartNewTopic(CancellationToken cancellationTo } /// Starts a new topic. - public async Task StartNewTopic( + public async Task StartNewTopic( string? name = null, ChatGPTConfig? config = null, UserOrSystemMessage? initialDialog = null, @@ -110,7 +110,7 @@ private IEnumerable ConvertToPersistentMessages(ChatCompl ); } - public async Task SetTopic(Guid topicId, CancellationToken cancellationToken = default) + public async Task SetTopic(Guid topicId, CancellationToken cancellationToken = default) { var topic = await _storage.GetTopic(_userId, topicId, cancellationToken); if (topic is null) @@ -120,17 +120,17 @@ public async Task SetTopic(Guid topicId, CancellationToken cancellationTok return await SetTopic(topic, cancellationToken); } - private Task SetTopic(Topic topic, CancellationToken cancellationToken = default) + private Task SetTopic(Topic topic, CancellationToken cancellationToken = default) { if (topic == null) throw new ArgumentNullException(nameof(topic)); _currentChat = CreateChat(topic, false, false); return Task.FromResult(_currentChat); } - private Chat CreateChat(Topic topic, bool isNew, bool clearOnDisposal) + private ChatService CreateChat(Topic topic, bool isNew, bool clearOnDisposal) { if (topic == null) throw new ArgumentNullException(nameof(topic)); - return new Chat(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal); + return new ChatService(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal); } public async Task> GetTopics(CancellationToken cancellationToken = default) diff --git a/OpenAI.ChatGpt/Chat.cs b/OpenAI.ChatGpt/ChatService.cs similarity index 98% rename from OpenAI.ChatGpt/Chat.cs rename to OpenAI.ChatGpt/ChatService.cs index 5bebe28..605d59e 100644 --- a/OpenAI.ChatGpt/Chat.cs +++ b/OpenAI.ChatGpt/ChatService.cs @@ -14,7 +14,7 @@ namespace OpenAI.ChatGpt; /// /// Not thread-safe. Use one instance per user. [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")] -public class Chat : IDisposable, IAsyncDisposable +public class ChatService : IDisposable, IAsyncDisposable { public Topic Topic { get; } public string UserId { get; } @@ -31,7 +31,7 @@ public class Chat : IDisposable, IAsyncDisposable private readonly bool _clearOnDisposal; private CancellationTokenSource? _cts; - internal Chat( + internal ChatService( IChatHistoryStorage chatHistoryStorage, ITimeProvider clock, OpenAiClient client, diff --git a/OpenAI.ChatGpt/OpenAI.ChatGpt.csproj b/OpenAI.ChatGpt/OpenAI.ChatGpt.csproj index 75b2024..1498d3c 100644 --- a/OpenAI.ChatGpt/OpenAI.ChatGpt.csproj +++ b/OpenAI.ChatGpt/OpenAI.ChatGpt.csproj @@ -10,7 +10,7 @@ OpenAI.ChatGPT https://github.com/rodion-m/ChatGPT_API_dotnet OpenAI ChatGPT integration for .NET - 2.3.0 + 2.4.0 .NET integration for ChatGPT with streaming responses supporting (like ChatGPT) via async streams. https://github.com/rodion-m/ChatGPT_API_dotnet MIT diff --git a/samples/ChatGpt.BlazorExample/Pages/Index.razor b/samples/ChatGpt.BlazorExample/Pages/Index.razor index 1bd27de..ee859f9 100644 --- a/samples/ChatGpt.BlazorExample/Pages/Index.razor +++ b/samples/ChatGpt.BlazorExample/Pages/Index.razor @@ -135,7 +135,7 @@ else string _errorMessage = ""; bool _processing = false; int _totalTokens = 0; - private Chat _chat; + private Chat _chatService; private string _userId = "test-user-id"; protected override async Task OnInitializedAsync() @@ -146,10 +146,10 @@ else private async Task CreateNewChat() { - if(_chat != null) await _chat.DisposeAsync(); + if(_chatService != null) await _chatService.DisposeAsync(); var chatGpt = await ChatGPTFactory.Create(_userId); - _chat = await chatGpt.ContinueOrStartNewTopic(); - _messages = (await _chat.GetMessages()).Select(m => (m.Role, m.Content)) + _chatService = await chatGpt.ContinueOrStartNewTopic(); + _messages = (await _chatService.GetMessages()).Select(m => (m.Role, m.Content)) .ToList(); } @@ -177,14 +177,14 @@ else // Clear any previous error messages _errorMessage = ""; - var response = await _chat.GetNextMessageResponse(_prompt); + var response = await _chatService.GetNextMessageResponse(_prompt); // Create a new MessageSave object with the user's prompt and other // details and add it to the messages list _messages.Add((ChatCompletionRoles.User, _prompt)); _messages.Add((ChatCompletionRoles.Assistant, response)); - _totalTokens = (int) (_chat.LastResponse?.Usage.TotalTokens ?? 0); + _totalTokens = (int) (_chatService.LastResponse?.Usage.TotalTokens ?? 0); } catch (Exception ex) { diff --git a/samples/ChatGpt.ConsoleExample/Program.cs b/samples/ChatGpt.ConsoleExample/Program.cs index 076e9db..b965452 100644 --- a/samples/ChatGpt.ConsoleExample/Program.cs +++ b/samples/ChatGpt.ConsoleExample/Program.cs @@ -8,12 +8,12 @@ var apiKey = LoadApiKey(); var config = new ChatGPTConfig() { MaxTokens = 300 }; -await using Chat chat = await ChatGPT.CreateInMemoryChat(apiKey, config); +await using ChatService chatService = await ChatGPT.CreateInMemoryChat(apiKey, config); Console.Write("User: "); while (Console.ReadLine() is { } userMessage) { - var response = await chat.GetNextMessageResponse(userMessage); + var response = await chatService.GetNextMessageResponse(userMessage); Console.WriteLine($"ChatGPT: {response.Trim()}"); Console.Write("User: "); } diff --git a/samples/ChatGpt.SpectreConsoleExample/Program.cs b/samples/ChatGpt.SpectreConsoleExample/Program.cs index fd8d384..6b61ddb 100644 --- a/samples/ChatGpt.SpectreConsoleExample/Program.cs +++ b/samples/ChatGpt.SpectreConsoleExample/Program.cs @@ -10,21 +10,21 @@ var name = Console.Ask("What's your [green]name[/]?") ?? "Me"; var apiKey = LoadApiKey(); -await using Chat chat = await ChatGPT.CreateInMemoryChat( +await using ChatService chatService = await ChatGPT.CreateInMemoryChat( apiKey, config: new ChatGPTConfig() { MaxTokens = 200 }, initialDialog: Dialog.StartAsSystem($"You are helpful assistant for a person named {name}.") ); -SetupCancellation(chat); +SetupCancellation(chatService); Console.MarkupLine("[underline yellow]Start chat. Now ask something ChatGPT...[/]"); while (Console.Ask($"[underline green]{name}[/]: ") is { } userMessage) { Console.Markup("[underline red]ChatGPT[/]: "); - var stream = chat.StreamNextMessageResponse(userMessage, throwOnCancellation: false); + var stream = chatService.StreamNextMessageResponse(userMessage, throwOnCancellation: false); await foreach (string chunk in stream.SkipWhile(string.IsNullOrWhiteSpace)) { - if (!chat.IsCancelled) Console.Write(chunk); + if (!chatService.IsCancelled) Console.Write(chunk); } Console.WriteLine(); @@ -39,7 +39,7 @@ string LoadApiKey() return key; } -void SetupCancellation(Chat chat) +void SetupCancellation(ChatService chat) { System.Console.CancelKeyPress += (_, args) => { diff --git a/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTests.cs b/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTests.cs index 7aa2528..c514557 100644 --- a/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTests.cs +++ b/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTests.cs @@ -5,14 +5,14 @@ public class ChatGptTests [Fact] public async void Stream_chatgpt_response_cancellation_throws_exception() { - Chat chat = await CreateInMemoryChat(); + ChatService chatService = await CreateInMemoryChat(); const string text = "Write numbers from 1 to 50"; await FluentActions.Invoking( async () => { - await foreach (var _ in chat.StreamNextMessageResponse(text)) + await foreach (var _ in chatService.StreamNextMessageResponse(text)) { - chat.Stop(); + chatService.Stop(); } }) .Should().ThrowAsync(); @@ -21,20 +21,20 @@ await FluentActions.Invoking( [Fact] public async void Stream_chatgpt_response_cancellation_with_throwOnCancellation_false_stopped_silently() { - Chat chat = await CreateInMemoryChat(); + ChatService chatService = await CreateInMemoryChat(); const string text = "Write numbers from 1 to 50"; await FluentActions.Invoking( async () => { - await foreach (var _ in chat.StreamNextMessageResponse(text, throwOnCancellation: false)) + await foreach (var _ in chatService.StreamNextMessageResponse(text, throwOnCancellation: false)) { - chat.Stop(); + chatService.Stop(); } }) .Should().NotThrowAsync(); } - private static async Task CreateInMemoryChat() + private static async Task CreateInMemoryChat() { return await ChatGPT.CreateInMemoryChat(Helpers.GetKeyFromEnvironment("OPENAI_API_KEY"), new ChatGPTConfig() diff --git a/tests/OpenAI.ChatGpt.UnitTests/DependencyInjectionTests/ChatGptServicesIntegrationTests.cs b/tests/OpenAI.ChatGpt.UnitTests/DependencyInjectionTests/ChatGptServicesIntegrationTests.cs index 27106a3..2f3a1ef 100644 --- a/tests/OpenAI.ChatGpt.UnitTests/DependencyInjectionTests/ChatGptServicesIntegrationTests.cs +++ b/tests/OpenAI.ChatGpt.UnitTests/DependencyInjectionTests/ChatGptServicesIntegrationTests.cs @@ -57,7 +57,7 @@ public async void AddChatGptInMemoryIntegration_with_Chat_injection_works() var services = CreateServiceCollection(); // Act - services.AddChatGptInMemoryIntegration(injectInMemoryChat: true); + services.AddChatGptInMemoryIntegration(injectInMemoryChatService: true); // Assert await using var provider = services.BuildServiceProvider(); @@ -65,7 +65,7 @@ public async void AddChatGptInMemoryIntegration_with_Chat_injection_works() var storage = provider.GetRequiredService(); storage.Should().BeOfType(); - _ = provider.GetRequiredService(); + _ = provider.GetRequiredService(); } [Fact]