Skip to content

Commit bf93d0e

Browse files
committed
Add blazor example; Release 2.3.0
1 parent 6d7d243 commit bf93d0e

File tree

9 files changed

+27
-51
lines changed

9 files changed

+27
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,4 @@ FodyWeavers.xsd
477477
# JetBrains Rider
478478
*.sln.iml
479479

480+
*.db

OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
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.2.2</Version>
11+
<Version>2.3.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>

OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
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.2.2</Version>
12+
<Version>2.3.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>

OpenAI.ChatGpt/ChatGPT.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace OpenAI.ChatGpt;
1111
public class ChatGPT : IDisposable
1212
{
1313
private readonly string _userId;
14-
private readonly IChatHistoryStorage _chatHistoryStorage;
14+
private readonly IChatHistoryStorage _storage;
1515
private readonly ITimeProvider _clock;
1616
private readonly ChatGPTConfig? _config;
1717
private readonly OpenAiClient _client;
@@ -29,7 +29,7 @@ public ChatGPT(
2929
{
3030
_client = client ?? throw new ArgumentNullException(nameof(client));
3131
_userId = userId ?? throw new ArgumentNullException(nameof(userId));
32-
_chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
32+
_storage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
3333
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
3434
_config = config;
3535
}
@@ -44,7 +44,7 @@ public ChatGPT(
4444
ChatGPTConfig? config)
4545
{
4646
_client = client ?? throw new ArgumentNullException(nameof(client));
47-
_chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
47+
_storage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
4848
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
4949
_userId = Guid.Empty.ToString();
5050
_config = config;
@@ -71,12 +71,10 @@ public void Dispose()
7171
}
7272

7373
/// <summary> Continues the last topic or starts a new one.</summary>
74-
public async Task<Chat> ContinueOrStartNewTopic(
75-
DateTimeOffset? createdAt = null,
76-
CancellationToken cancellationToken = default)
74+
public async Task<Chat> ContinueOrStartNewTopic(CancellationToken cancellationToken = default)
7775
{
7876
if (_currentChat is not null) return _currentChat;
79-
var topic = await _chatHistoryStorage.GetMostRecentTopicOrNull(_userId, cancellationToken);
77+
var topic = await _storage.GetMostRecentTopicOrNull(_userId, cancellationToken);
8078
return topic is null
8179
? await StartNewTopic(cancellationToken: cancellationToken)
8280
: await SetTopic(topic, cancellationToken);
@@ -91,13 +89,13 @@ public async Task<Chat> StartNewTopic(
9189
CancellationToken cancellationToken = default)
9290
{
9391
config = ChatGPTConfig.CombineOrDefault(_config, config);
94-
var topic = new Topic(_chatHistoryStorage.NewTopicId(), _userId, name, _clock.GetCurrentTime(), config);
95-
await _chatHistoryStorage.AddTopic(topic, cancellationToken);
92+
var topic = new Topic(_storage.NewTopicId(), _userId, name, _clock.GetCurrentTime(), config);
93+
await _storage.AddTopic(topic, cancellationToken);
9694
initialDialog ??= config.GetInitialDialogOrNull();
9795
if (initialDialog is not null)
9896
{
9997
var messages = ConvertToPersistentMessages(initialDialog, topic);
100-
await _chatHistoryStorage.SaveMessages(_userId, topic.Id, messages, cancellationToken);
98+
await _storage.SaveMessages(_userId, topic.Id, messages, cancellationToken);
10199
}
102100

103101
_currentChat = CreateChat(topic, initialDialog is null, clearOnDisposal: clearOnDisposal);
@@ -108,13 +106,13 @@ private IEnumerable<PersistentChatMessage> ConvertToPersistentMessages(ChatCompl
108106
{
109107
return dialog.GetMessages()
110108
.Select(m => new PersistentChatMessage(
111-
_chatHistoryStorage.NewMessageId(), _userId, topic.Id, _clock.GetCurrentTime(), m)
109+
_storage.NewMessageId(), _userId, topic.Id, _clock.GetCurrentTime(), m)
112110
);
113111
}
114112

115113
public async Task<Chat> SetTopic(Guid topicId, CancellationToken cancellationToken = default)
116114
{
117-
var topic = await _chatHistoryStorage.GetTopic(_userId, topicId, cancellationToken);
115+
var topic = await _storage.GetTopic(_userId, topicId, cancellationToken);
118116
if (topic is null)
119117
{
120118
throw new ArgumentException($"Chat with id {topicId} not found for user {_userId}");
@@ -132,12 +130,12 @@ private Task<Chat> SetTopic(Topic topic, CancellationToken cancellationToken = d
132130
private Chat CreateChat(Topic topic, bool isNew, bool clearOnDisposal)
133131
{
134132
if (topic == null) throw new ArgumentNullException(nameof(topic));
135-
return new Chat(_chatHistoryStorage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
133+
return new Chat(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
136134
}
137135

138136
public async Task<IReadOnlyList<Topic>> GetTopics(CancellationToken cancellationToken = default)
139137
{
140-
var chats = await _chatHistoryStorage.GetTopics(_userId, cancellationToken);
138+
var chats = await _storage.GetTopics(_userId, cancellationToken);
141139
return chats.ToList();
142140
}
143141

OpenAI.ChatGpt/OpenAI.ChatGpt.csproj

Lines changed: 1 addition & 1 deletion
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.2.2</Version>
13+
<Version>2.3.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>

samples/ChatGpt.BlazorExample/ChatGpt.BlazorExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
12-
<PackageReference Include="OpenAI.ChatGPT.EntityFrameworkCore" Version="2.2.0" />
12+
<PackageReference Include="OpenAI.ChatGPT.EntityFrameworkCore" Version="2.3.0" />
1313
</ItemGroup>
1414

1515
</Project>

samples/ChatGpt.BlazorExample/Pages/Counter.razor

Lines changed: 0 additions & 18 deletions
This file was deleted.

samples/ChatGpt.BlazorExample/Pages/Index.razor

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
@page "/"
22
@using OpenAI.ChatGpt.AspNetCore
33
@using OpenAI.ChatGpt
4+
@using OpenAI.ChatGpt.Interfaces
45
@using OpenAI.ChatGpt.Models
56
@using OpenAI.ChatGpt.Models.ChatCompletion
67
@inject IJSRuntime JsRuntime
78
@inject ChatGPTFactory ChatGPTFactory
9+
@inject IChatHistoryStorage ChatHistoryStorage
810

911
<PageTitle>Index</PageTitle>
1012
@if (_messages is null)
@@ -77,7 +79,7 @@
7779
@foreach (var item in _messages)
7880
{
7981
<div>
80-
@if (item.role == item.content)
82+
@if (item.role == ChatCompletionRoles.User)
8183
{
8284
<div style="float: right; margin-right: 20px; margin-top: 10px">
8385
<b>Human</b>
@@ -134,6 +136,7 @@ else
134136
bool _processing = false;
135137
int _totalTokens = 0;
136138
private Chat _chat;
139+
private string _userId = "test-user-id";
137140

138141
protected override async Task OnInitializedAsync()
139142
{
@@ -144,9 +147,10 @@ else
144147
private async Task CreateNewChat()
145148
{
146149
if(_chat != null) await _chat.DisposeAsync();
147-
var chatGpt = await ChatGPTFactory.Create("test-user-id");
148-
_chat = await chatGpt.StartNewTopic("Test Topic", clearOnDisposal: true);
149-
150+
var chatGpt = await ChatGPTFactory.Create(_userId);
151+
_chat = await chatGpt.ContinueOrStartNewTopic();
152+
_messages = (await _chat.GetMessages()).Select(m => (m.Role, m.Content))
153+
.ToList();
150154
}
151155

152156
protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -180,7 +184,7 @@ else
180184
_messages.Add((ChatCompletionRoles.User, _prompt));
181185
_messages.Add((ChatCompletionRoles.Assistant, response));
182186

183-
//TotalTokens = _chat.LastResponse.Usage.TotalTokens;
187+
_totalTokens = (int) (_chat.LastResponse?.Usage.TotalTokens ?? 0);
184188
}
185189
catch (Exception ex)
186190
{
@@ -204,6 +208,7 @@ else
204208
async Task RestartChatGpt()
205209
{
206210
_prompt = "Write a 10 word description of OpenAI ChatGPT";
211+
await ChatHistoryStorage.ClearTopics(_userId, default);
207212
await CreateNewChat();
208213
_messages = new();
209214
_totalTokens = 0;

samples/ChatGpt.BlazorExample/Shared/NavMenu.razor

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
<span class="oi oi-home" aria-hidden="true"></span> Home
1515
</NavLink>
1616
</div>
17-
<div class="nav-item px-3">
18-
<NavLink class="nav-link" href="counter">
19-
<span class="oi oi-plus" aria-hidden="true"></span> Counter
20-
</NavLink>
21-
</div>
22-
<div class="nav-item px-3">
23-
<NavLink class="nav-link" href="fetchdata">
24-
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
25-
</NavLink>
26-
</div>
2717
</nav>
2818
</div>
2919

0 commit comments

Comments
 (0)