Skip to content

Commit 67038e0

Browse files
committed
Rename Chat to ChatService; Release 2.4.0
1 parent 2538e0c commit 67038e0

File tree

11 files changed

+41
-41
lines changed

11 files changed

+41
-41
lines changed

Diff for: OpenAI.ChatGpt.AspNetCore/Extensions/ServiceCollectionExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions
1212

1313
public static IServiceCollection AddChatGptInMemoryIntegration(
1414
this IServiceCollection services,
15-
bool injectInMemoryChat = true,
15+
bool injectInMemoryChatService = true,
1616
string credentialsConfigSectionPath = CredentialsConfigSectionPathDefault,
1717
string completionsConfigSectionPath = CchatGPTConfigSectionPathDefault)
1818
{
@@ -29,14 +29,14 @@ public static IServiceCollection AddChatGptInMemoryIntegration(
2929
}
3030
services.AddChatGptIntegrationCore(credentialsConfigSectionPath, completionsConfigSectionPath);
3131
services.AddSingleton<IChatHistoryStorage, InMemoryChatHistoryStorage>();
32-
if(injectInMemoryChat)
32+
if(injectInMemoryChatService)
3333
{
34-
services.AddScoped<Chat>(CreateChatGptChat);
34+
services.AddScoped<ChatService>(CreateChatService);
3535
}
3636
return services;
3737
}
3838

39-
private static Chat CreateChatGptChat(IServiceProvider provider)
39+
private static ChatService CreateChatService(IServiceProvider provider)
4040
{
4141
ArgumentNullException.ThrowIfNull(provider);
4242
var userId = Guid.Empty.ToString();

Diff for: OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageId>OpenAI.ChatGPT.AspNetCore</PackageId>
99
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
1010
<Product>OpenAI ChatGPT integration for .NET with DI</Product>
11-
<Version>2.3.0</Version>
11+
<Version>2.4.0</Version>
1212
<Description>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.</Description>
1313
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
1414
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>

Diff for: OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageId>OpenAI.ChatGPT.EntityFrameworkCore</PackageId>
1010
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
1111
<Product>OpenAI ChatGPT integration for .NET with EF Core storage</Product>
12-
<Version>2.3.0</Version>
12+
<Version>2.4.0</Version>
1313
<Description>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.</Description>
1414
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
1515
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
@@ -25,8 +25,8 @@
2525
<PrivateAssets>all</PrivateAssets>
2626
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2727
</PackageReference>
28-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
29-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
28+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
29+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
3030
<PrivateAssets>all</PrivateAssets>
3131
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3232
</PackageReference>

Diff for: OpenAI.ChatGpt/ChatGPT.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ChatGPT : IDisposable
1515
private readonly ITimeProvider _clock;
1616
private readonly ChatGPTConfig? _config;
1717
private readonly OpenAiClient _client;
18-
private Chat? _currentChat;
18+
private ChatService? _currentChat;
1919

2020
/// <summary>
2121
/// Use this constructor to create chat conversation provider for the specific user.
@@ -53,7 +53,7 @@ public ChatGPT(
5353
/// <summary>
5454
/// If you don't have users and don't want to save messages into database use this method.
5555
/// </summary>
56-
public static Task<Chat> CreateInMemoryChat(
56+
public static Task<ChatService> CreateInMemoryChat(
5757
string apiKey,
5858
ChatGPTConfig? config = null,
5959
UserOrSystemMessage? initialDialog = null,
@@ -71,7 +71,7 @@ public void Dispose()
7171
}
7272

7373
/// <summary> Continues the last topic or starts a new one.</summary>
74-
public async Task<Chat> ContinueOrStartNewTopic(CancellationToken cancellationToken = default)
74+
public async Task<ChatService> ContinueOrStartNewTopic(CancellationToken cancellationToken = default)
7575
{
7676
if (_currentChat is not null) return _currentChat;
7777
var topic = await _storage.GetMostRecentTopicOrNull(_userId, cancellationToken);
@@ -81,7 +81,7 @@ public async Task<Chat> ContinueOrStartNewTopic(CancellationToken cancellationTo
8181
}
8282

8383
/// <summary> Starts a new topic. </summary>
84-
public async Task<Chat> StartNewTopic(
84+
public async Task<ChatService> StartNewTopic(
8585
string? name = null,
8686
ChatGPTConfig? config = null,
8787
UserOrSystemMessage? initialDialog = null,
@@ -110,7 +110,7 @@ private IEnumerable<PersistentChatMessage> ConvertToPersistentMessages(ChatCompl
110110
);
111111
}
112112

113-
public async Task<Chat> SetTopic(Guid topicId, CancellationToken cancellationToken = default)
113+
public async Task<ChatService> SetTopic(Guid topicId, CancellationToken cancellationToken = default)
114114
{
115115
var topic = await _storage.GetTopic(_userId, topicId, cancellationToken);
116116
if (topic is null)
@@ -120,17 +120,17 @@ public async Task<Chat> SetTopic(Guid topicId, CancellationToken cancellationTok
120120
return await SetTopic(topic, cancellationToken);
121121
}
122122

123-
private Task<Chat> SetTopic(Topic topic, CancellationToken cancellationToken = default)
123+
private Task<ChatService> SetTopic(Topic topic, CancellationToken cancellationToken = default)
124124
{
125125
if (topic == null) throw new ArgumentNullException(nameof(topic));
126126
_currentChat = CreateChat(topic, false, false);
127127
return Task.FromResult(_currentChat);
128128
}
129129

130-
private Chat CreateChat(Topic topic, bool isNew, bool clearOnDisposal)
130+
private ChatService CreateChat(Topic topic, bool isNew, bool clearOnDisposal)
131131
{
132132
if (topic == null) throw new ArgumentNullException(nameof(topic));
133-
return new Chat(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
133+
return new ChatService(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
134134
}
135135

136136
public async Task<IReadOnlyList<Topic>> GetTopics(CancellationToken cancellationToken = default)

Diff for: OpenAI.ChatGpt/Chat.cs renamed to OpenAI.ChatGpt/ChatService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace OpenAI.ChatGpt;
1414
/// </summary>
1515
/// <remarks>Not thread-safe. Use one instance per user.</remarks>
1616
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
17-
public class Chat : IDisposable, IAsyncDisposable
17+
public class ChatService : IDisposable, IAsyncDisposable
1818
{
1919
public Topic Topic { get; }
2020
public string UserId { get; }
@@ -31,7 +31,7 @@ public class Chat : IDisposable, IAsyncDisposable
3131
private readonly bool _clearOnDisposal;
3232
private CancellationTokenSource? _cts;
3333

34-
internal Chat(
34+
internal ChatService(
3535
IChatHistoryStorage chatHistoryStorage,
3636
ITimeProvider clock,
3737
OpenAiClient client,

Diff for: OpenAI.ChatGpt/OpenAI.ChatGpt.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageId>OpenAI.ChatGPT</PackageId>
1111
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
1212
<Product>OpenAI ChatGPT integration for .NET</Product>
13-
<Version>2.3.0</Version>
13+
<Version>2.4.0</Version>
1414
<Description>.NET integration for ChatGPT with streaming responses supporting (like ChatGPT) via async streams.</Description>
1515
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
1616
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Diff for: samples/ChatGpt.BlazorExample/Pages/Index.razor

+6-6
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ else
135135
string _errorMessage = "";
136136
bool _processing = false;
137137
int _totalTokens = 0;
138-
private Chat _chat;
138+
private Chat _chatService;
139139
private string _userId = "test-user-id";
140140

141141
protected override async Task OnInitializedAsync()
@@ -146,10 +146,10 @@ else
146146

147147
private async Task CreateNewChat()
148148
{
149-
if(_chat != null) await _chat.DisposeAsync();
149+
if(_chatService != null) await _chatService.DisposeAsync();
150150
var chatGpt = await ChatGPTFactory.Create(_userId);
151-
_chat = await chatGpt.ContinueOrStartNewTopic();
152-
_messages = (await _chat.GetMessages()).Select(m => (m.Role, m.Content))
151+
_chatService = await chatGpt.ContinueOrStartNewTopic();
152+
_messages = (await _chatService.GetMessages()).Select(m => (m.Role, m.Content))
153153
.ToList();
154154
}
155155

@@ -177,14 +177,14 @@ else
177177

178178
// Clear any previous error messages
179179
_errorMessage = "";
180-
var response = await _chat.GetNextMessageResponse(_prompt);
180+
var response = await _chatService.GetNextMessageResponse(_prompt);
181181

182182
// Create a new MessageSave object with the user's prompt and other
183183
// details and add it to the messages list
184184
_messages.Add((ChatCompletionRoles.User, _prompt));
185185
_messages.Add((ChatCompletionRoles.Assistant, response));
186186

187-
_totalTokens = (int) (_chat.LastResponse?.Usage.TotalTokens ?? 0);
187+
_totalTokens = (int) (_chatService.LastResponse?.Usage.TotalTokens ?? 0);
188188
}
189189
catch (Exception ex)
190190
{

Diff for: samples/ChatGpt.ConsoleExample/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
var apiKey = LoadApiKey();
1010
var config = new ChatGPTConfig() { MaxTokens = 300 };
11-
await using Chat chat = await ChatGPT.CreateInMemoryChat(apiKey, config);
11+
await using ChatService chatService = await ChatGPT.CreateInMemoryChat(apiKey, config);
1212

1313
Console.Write("User: ");
1414
while (Console.ReadLine() is { } userMessage)
1515
{
16-
var response = await chat.GetNextMessageResponse(userMessage);
16+
var response = await chatService.GetNextMessageResponse(userMessage);
1717
Console.WriteLine($"ChatGPT: {response.Trim()}");
1818
Console.Write("User: ");
1919
}

Diff for: samples/ChatGpt.SpectreConsoleExample/Program.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111
var name = Console.Ask<string?>("What's your [green]name[/]?") ?? "Me";
1212
var apiKey = LoadApiKey();
13-
await using Chat chat = await ChatGPT.CreateInMemoryChat(
13+
await using ChatService chatService = await ChatGPT.CreateInMemoryChat(
1414
apiKey,
1515
config: new ChatGPTConfig() { MaxTokens = 200 },
1616
initialDialog: Dialog.StartAsSystem($"You are helpful assistant for a person named {name}.")
1717
);
18-
SetupCancellation(chat);
18+
SetupCancellation(chatService);
1919

2020
Console.MarkupLine("[underline yellow]Start chat. Now ask something ChatGPT...[/]");
2121
while (Console.Ask<string>($"[underline green]{name}[/]: ") is { } userMessage)
2222
{
2323
Console.Markup("[underline red]ChatGPT[/]: ");
24-
var stream = chat.StreamNextMessageResponse(userMessage, throwOnCancellation: false);
24+
var stream = chatService.StreamNextMessageResponse(userMessage, throwOnCancellation: false);
2525
await foreach (string chunk in stream.SkipWhile(string.IsNullOrWhiteSpace))
2626
{
27-
if (!chat.IsCancelled) Console.Write(chunk);
27+
if (!chatService.IsCancelled) Console.Write(chunk);
2828
}
2929

3030
Console.WriteLine();
@@ -39,7 +39,7 @@ string LoadApiKey()
3939
return key;
4040
}
4141

42-
void SetupCancellation(Chat chat)
42+
void SetupCancellation(ChatService chat)
4343
{
4444
System.Console.CancelKeyPress += (_, args) =>
4545
{

Diff for: tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ public class ChatGptTests
55
[Fact]
66
public async void Stream_chatgpt_response_cancellation_throws_exception()
77
{
8-
Chat chat = await CreateInMemoryChat();
8+
ChatService chatService = await CreateInMemoryChat();
99
const string text = "Write numbers from 1 to 50";
1010
await FluentActions.Invoking(
1111
async () =>
1212
{
13-
await foreach (var _ in chat.StreamNextMessageResponse(text))
13+
await foreach (var _ in chatService.StreamNextMessageResponse(text))
1414
{
15-
chat.Stop();
15+
chatService.Stop();
1616
}
1717
})
1818
.Should().ThrowAsync<OperationCanceledException>();
@@ -21,20 +21,20 @@ await FluentActions.Invoking(
2121
[Fact]
2222
public async void Stream_chatgpt_response_cancellation_with_throwOnCancellation_false_stopped_silently()
2323
{
24-
Chat chat = await CreateInMemoryChat();
24+
ChatService chatService = await CreateInMemoryChat();
2525
const string text = "Write numbers from 1 to 50";
2626
await FluentActions.Invoking(
2727
async () =>
2828
{
29-
await foreach (var _ in chat.StreamNextMessageResponse(text, throwOnCancellation: false))
29+
await foreach (var _ in chatService.StreamNextMessageResponse(text, throwOnCancellation: false))
3030
{
31-
chat.Stop();
31+
chatService.Stop();
3232
}
3333
})
3434
.Should().NotThrowAsync();
3535
}
3636

37-
private static async Task<Chat> CreateInMemoryChat()
37+
private static async Task<ChatService> CreateInMemoryChat()
3838
{
3939
return await ChatGPT.CreateInMemoryChat(Helpers.GetKeyFromEnvironment("OPENAI_API_KEY"),
4040
new ChatGPTConfig()

Diff for: tests/OpenAI.ChatGpt.UnitTests/DependencyInjectionTests/ChatGptServicesIntegrationTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public async void AddChatGptInMemoryIntegration_with_Chat_injection_works()
5757
var services = CreateServiceCollection();
5858

5959
// Act
60-
services.AddChatGptInMemoryIntegration(injectInMemoryChat: true);
60+
services.AddChatGptInMemoryIntegration(injectInMemoryChatService: true);
6161

6262
// Assert
6363
await using var provider = services.BuildServiceProvider();
6464

6565
var storage = provider.GetRequiredService<IChatHistoryStorage>();
6666
storage.Should().BeOfType<InMemoryChatHistoryStorage>();
6767

68-
_ = provider.GetRequiredService<Chat>();
68+
_ = provider.GetRequiredService<ChatService>();
6969
}
7070

7171
[Fact]

0 commit comments

Comments
 (0)