-
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.
Fix AddChatGptEntityFrameworkIntegration extension; Release 2.2.2
- Loading branch information
Showing
38 changed files
with
1,570 additions
and
5 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
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
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,12 @@ | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> |
15 changes: 15 additions & 0 deletions
15
samples/ChatGpt.BlazorExample/ChatGpt.BlazorExample.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>e883db38-8c66-433b-a1cf-301b5b3b2171</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" /> | ||
<PackageReference Include="OpenAI.ChatGPT.EntityFrameworkCore" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,18 @@ | ||
@page "/counter" | ||
|
||
<PageTitle>Counter</PageTitle> | ||
|
||
<h1>Counter</h1> | ||
|
||
<p role="status">Current count: @currentCount</p> | ||
|
||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
||
@code { | ||
private int currentCount = 0; | ||
|
||
private void IncrementCount() | ||
{ | ||
currentCount++; | ||
} | ||
} |
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,42 @@ | ||
@page | ||
@model ChatGpt.BlazorExample.Pages.ErrorModel | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | ||
<title>Error</title> | ||
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" /> | ||
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" /> | ||
</head> | ||
|
||
<body> | ||
<div class="main"> | ||
<div class="content px-4"> | ||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
</div> | ||
</div> | ||
</body> | ||
|
||
</html> |
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,27 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using System.Diagnostics; | ||
|
||
namespace ChatGpt.BlazorExample.Pages | ||
{ | ||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
[IgnoreAntiforgeryToken] | ||
public class ErrorModel : PageModel | ||
{ | ||
public string? RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
private readonly ILogger<ErrorModel> _logger; | ||
|
||
public ErrorModel(ILogger<ErrorModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
} | ||
} | ||
} |
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,213 @@ | ||
@page "/" | ||
@using OpenAI.ChatGpt.AspNetCore | ||
@using OpenAI.ChatGpt | ||
@using OpenAI.ChatGpt.Models | ||
@using OpenAI.ChatGpt.Models.ChatCompletion | ||
@inject IJSRuntime JsRuntime | ||
@inject ChatGPTFactory ChatGPTFactory | ||
|
||
<PageTitle>Index</PageTitle> | ||
@if (_messages is null) | ||
{ | ||
<h3>Loading messages...</h3> | ||
return; | ||
} | ||
<style> | ||
textarea { | ||
border: 1px dashed #888; | ||
border-radius: 5px; | ||
width: 80%; | ||
overflow: auto; | ||
background: #f7f7f7 | ||
} | ||
/* improved CSS for speech bubbles */ | ||
.assistant, .user { | ||
position: relative; | ||
font-family: arial; | ||
font-size: 1.1em; | ||
border-radius: 10px; | ||
padding: 20px; | ||
margin-bottom: 20px; | ||
} | ||
.assistant:after, .user:after { | ||
content: ''; | ||
border: 20px solid transparent; | ||
position: absolute; | ||
margin-top: -30px; | ||
} | ||
.user { | ||
background: #03a9f4; | ||
color: #fff; | ||
margin-left: 20%; | ||
margin-right: 100px; | ||
top: 30%; | ||
text-align: right; | ||
} | ||
.assistant { | ||
background: #4CAF50; | ||
color: #fff; | ||
margin-left: 100px; | ||
margin-right: 20%; | ||
} | ||
.user:after { | ||
border-left-color: #03a9f4; | ||
border-right: 0; | ||
right: -20px; | ||
} | ||
.assistant:after { | ||
border-right-color: #4CAF50; | ||
border-left: 0; | ||
left: -20px; | ||
} | ||
.msg { | ||
font-size: medium; | ||
} | ||
</style> | ||
<h1>ChatGPT</h1> | ||
<p style="font-size:small"><b>Total Tokens:</b> @_totalTokens</p> | ||
<div id="chatcontainer" style="height:550px; width:80%; overflow: scroll;"> | ||
@foreach (var item in _messages) | ||
{ | ||
<div> | ||
@if (item.role == item.content) | ||
{ | ||
<div style="float: right; margin-right: 20px; margin-top: 10px"> | ||
<b>Human</b> | ||
</div> | ||
<div class="@item.role.ToLower()"> | ||
<div class="msg"> | ||
@item.content | ||
<br /><br /> | ||
</div> | ||
</div> | ||
} | ||
else | ||
{ | ||
<div style="float: left; margin-left: 20px; margin-top: 10px"> | ||
<b>ChatGPT </b> | ||
</div> | ||
<div class="@item.role.ToLower()"> | ||
<div class="msg"> | ||
@if (item.content != null) | ||
{ | ||
@((MarkupString)item.content) | ||
} | ||
<br /><br /> | ||
</div> | ||
</div> | ||
} | ||
</div> | ||
} | ||
</div> | ||
@if (!_processing) | ||
{ | ||
<textarea rows="3" cols="60" @bind="_prompt"></textarea> | ||
<br /> | ||
<button class="btn btn-primary" | ||
@onclick="CallChatGpt"> | ||
Call ChatGPT | ||
</button> | ||
<span> </span> | ||
<button class="btn btn-info" | ||
@onclick="RestartChatGpt"> | ||
Restart | ||
</button> | ||
} | ||
else | ||
{ | ||
<br> | ||
<h4>Processing...</h4> | ||
} | ||
<br /><p style="color:red">@_errorMessage</p> | ||
@code { | ||
List<(string role, string content)> _messages = new(); | ||
string _prompt = "Write a 10 word description of OpenAI ChatGPT"; | ||
string _errorMessage = ""; | ||
bool _processing = false; | ||
int _totalTokens = 0; | ||
private Chat _chat; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
await CreateNewChat(); | ||
} | ||
|
||
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); | ||
|
||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
try | ||
{ | ||
await JsRuntime.InvokeAsync<string>("ScrollToBottom", "chatcontainer"); | ||
} | ||
catch | ||
{ | ||
// do nothing if this fails | ||
} | ||
} | ||
|
||
async Task CallChatGpt() | ||
{ | ||
try | ||
{ | ||
// Set Processing to true to indicate that the method is processing | ||
_processing = true; | ||
|
||
// Call StateHasChanged to refresh the UI | ||
StateHasChanged(); | ||
|
||
// Clear any previous error messages | ||
_errorMessage = ""; | ||
var response = await _chat.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 = _chat.LastResponse.Usage.TotalTokens; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Set ErrorMessage to the exception message if an error occurs | ||
_errorMessage = ex.Message; | ||
} | ||
finally | ||
{ | ||
// Clear the prompt variable | ||
_prompt = ""; | ||
|
||
// Set Processing to false to indicate | ||
// that the method is done processing | ||
_processing = false; | ||
|
||
// Call StateHasChanged to refresh the UI | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
async Task RestartChatGpt() | ||
{ | ||
_prompt = "Write a 10 word description of OpenAI ChatGPT"; | ||
await CreateNewChat(); | ||
_messages = new(); | ||
_totalTokens = 0; | ||
_errorMessage = ""; | ||
StateHasChanged(); | ||
} | ||
} |
Oops, something went wrong.