Skip to content

Commit

Permalink
Add blazor example; Release 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rodion-m committed Apr 20, 2023
1 parent 6d7d243 commit bf93d0e
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,4 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

*.db
2 changes: 1 addition & 1 deletion OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageId>OpenAI.ChatGPT.AspNetCore</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT integration for .NET with DI</Product>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<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>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageId>OpenAI.ChatGPT.EntityFrameworkCore</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT integration for .NET with EF Core storage</Product>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<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>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
Expand Down
26 changes: 12 additions & 14 deletions OpenAI.ChatGpt/ChatGPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OpenAI.ChatGpt;
public class ChatGPT : IDisposable
{
private readonly string _userId;
private readonly IChatHistoryStorage _chatHistoryStorage;
private readonly IChatHistoryStorage _storage;
private readonly ITimeProvider _clock;
private readonly ChatGPTConfig? _config;
private readonly OpenAiClient _client;
Expand All @@ -29,7 +29,7 @@ public ChatGPT(
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_userId = userId ?? throw new ArgumentNullException(nameof(userId));
_chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
_storage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
_config = config;
}
Expand All @@ -44,7 +44,7 @@ public ChatGPT(
ChatGPTConfig? config)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
_storage = chatHistoryStorage ?? throw new ArgumentNullException(nameof(chatHistoryStorage));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
_userId = Guid.Empty.ToString();
_config = config;
Expand All @@ -71,12 +71,10 @@ public void Dispose()
}

/// <summary> Continues the last topic or starts a new one.</summary>
public async Task<Chat> ContinueOrStartNewTopic(
DateTimeOffset? createdAt = null,
CancellationToken cancellationToken = default)
public async Task<Chat> ContinueOrStartNewTopic(CancellationToken cancellationToken = default)
{
if (_currentChat is not null) return _currentChat;
var topic = await _chatHistoryStorage.GetMostRecentTopicOrNull(_userId, cancellationToken);
var topic = await _storage.GetMostRecentTopicOrNull(_userId, cancellationToken);
return topic is null
? await StartNewTopic(cancellationToken: cancellationToken)
: await SetTopic(topic, cancellationToken);
Expand All @@ -91,13 +89,13 @@ public async Task<Chat> StartNewTopic(
CancellationToken cancellationToken = default)
{
config = ChatGPTConfig.CombineOrDefault(_config, config);
var topic = new Topic(_chatHistoryStorage.NewTopicId(), _userId, name, _clock.GetCurrentTime(), config);
await _chatHistoryStorage.AddTopic(topic, cancellationToken);
var topic = new Topic(_storage.NewTopicId(), _userId, name, _clock.GetCurrentTime(), config);
await _storage.AddTopic(topic, cancellationToken);
initialDialog ??= config.GetInitialDialogOrNull();
if (initialDialog is not null)
{
var messages = ConvertToPersistentMessages(initialDialog, topic);
await _chatHistoryStorage.SaveMessages(_userId, topic.Id, messages, cancellationToken);
await _storage.SaveMessages(_userId, topic.Id, messages, cancellationToken);
}

_currentChat = CreateChat(topic, initialDialog is null, clearOnDisposal: clearOnDisposal);
Expand All @@ -108,13 +106,13 @@ private IEnumerable<PersistentChatMessage> ConvertToPersistentMessages(ChatCompl
{
return dialog.GetMessages()
.Select(m => new PersistentChatMessage(
_chatHistoryStorage.NewMessageId(), _userId, topic.Id, _clock.GetCurrentTime(), m)
_storage.NewMessageId(), _userId, topic.Id, _clock.GetCurrentTime(), m)
);
}

public async Task<Chat> SetTopic(Guid topicId, CancellationToken cancellationToken = default)
{
var topic = await _chatHistoryStorage.GetTopic(_userId, topicId, cancellationToken);
var topic = await _storage.GetTopic(_userId, topicId, cancellationToken);
if (topic is null)
{
throw new ArgumentException($"Chat with id {topicId} not found for user {_userId}");
Expand All @@ -132,12 +130,12 @@ private Task<Chat> SetTopic(Topic topic, CancellationToken cancellationToken = d
private Chat CreateChat(Topic topic, bool isNew, bool clearOnDisposal)
{
if (topic == null) throw new ArgumentNullException(nameof(topic));
return new Chat(_chatHistoryStorage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
return new Chat(_storage, _clock, _client, _userId, topic, isNew, clearOnDisposal);
}

public async Task<IReadOnlyList<Topic>> GetTopics(CancellationToken cancellationToken = default)
{
var chats = await _chatHistoryStorage.GetTopics(_userId, cancellationToken);
var chats = await _storage.GetTopics(_userId, cancellationToken);
return chats.ToList();
}

Expand Down
2 changes: 1 addition & 1 deletion OpenAI.ChatGpt/OpenAI.ChatGpt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageId>OpenAI.ChatGPT</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT integration for .NET</Product>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<Description>.NET integration for ChatGPT with streaming responses supporting (like ChatGPT) via async streams.</Description>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
2 changes: 1 addition & 1 deletion samples/ChatGpt.BlazorExample/ChatGpt.BlazorExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
<PackageReference Include="OpenAI.ChatGPT.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="OpenAI.ChatGPT.EntityFrameworkCore" Version="2.3.0" />
</ItemGroup>

</Project>
18 changes: 0 additions & 18 deletions samples/ChatGpt.BlazorExample/Pages/Counter.razor

This file was deleted.

15 changes: 10 additions & 5 deletions samples/ChatGpt.BlazorExample/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
@page "/"
@using OpenAI.ChatGpt.AspNetCore
@using OpenAI.ChatGpt
@using OpenAI.ChatGpt.Interfaces
@using OpenAI.ChatGpt.Models
@using OpenAI.ChatGpt.Models.ChatCompletion
@inject IJSRuntime JsRuntime
@inject ChatGPTFactory ChatGPTFactory
@inject IChatHistoryStorage ChatHistoryStorage

<PageTitle>Index</PageTitle>
@if (_messages is null)
Expand Down Expand Up @@ -77,7 +79,7 @@
@foreach (var item in _messages)
{
<div>
@if (item.role == item.content)
@if (item.role == ChatCompletionRoles.User)
{
<div style="float: right; margin-right: 20px; margin-top: 10px">
<b>Human</b>
Expand Down Expand Up @@ -134,6 +136,7 @@ else
bool _processing = false;
int _totalTokens = 0;
private Chat _chat;
private string _userId = "test-user-id";

protected override async Task OnInitializedAsync()
{
Expand All @@ -144,9 +147,10 @@ else
private async Task CreateNewChat()
{
if(_chat != null) await _chat.DisposeAsync();
var chatGpt = await ChatGPTFactory.Create("test-user-id");
_chat = await chatGpt.StartNewTopic("Test Topic", clearOnDisposal: true);

var chatGpt = await ChatGPTFactory.Create(_userId);
_chat = await chatGpt.ContinueOrStartNewTopic();
_messages = (await _chat.GetMessages()).Select(m => (m.Role, m.Content))
.ToList();
}

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand Down Expand Up @@ -180,7 +184,7 @@ else
_messages.Add((ChatCompletionRoles.User, _prompt));
_messages.Add((ChatCompletionRoles.Assistant, response));

//TotalTokens = _chat.LastResponse.Usage.TotalTokens;
_totalTokens = (int) (_chat.LastResponse?.Usage.TotalTokens ?? 0);
}
catch (Exception ex)
{
Expand All @@ -204,6 +208,7 @@ else
async Task RestartChatGpt()
{
_prompt = "Write a 10 word description of OpenAI ChatGPT";
await ChatHistoryStorage.ClearTopics(_userId, default);
await CreateNewChat();
_messages = new();
_totalTokens = 0;
Expand Down
10 changes: 0 additions & 10 deletions samples/ChatGpt.BlazorExample/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="oi oi-plus" aria-hidden="true"></span> Counter
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="fetchdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</div>
</nav>
</div>

Expand Down

0 comments on commit bf93d0e

Please sign in to comment.