Added a grammar and spell checker assistant

This commit is contained in:
Thorsten Sommer 2024-08-11 21:43:50 +02:00
parent 77e7e044e3
commit aaf577269e
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
7 changed files with 97 additions and 1 deletions

View File

@ -17,7 +17,7 @@
</MudForm> </MudForm>
<Issues IssuesData="@this.inputIssues"/> <Issues IssuesData="@this.inputIssues"/>
@if (this.resultingContentBlock is not null) @if (this.ShowResult && this.resultingContentBlock is not null)
{ {
<ContentBlockComponent Role="@this.resultingContentBlock.Role" Type="@this.resultingContentBlock.ContentType" Time="@this.resultingContentBlock.Time" Content="@this.resultingContentBlock.Content" Class="mr-2"/> <ContentBlockComponent Role="@this.resultingContentBlock.Role" Type="@this.resultingContentBlock.ContentType" Time="@this.resultingContentBlock.Time" Content="@this.resultingContentBlock.Content" Class="mr-2"/>
} }

View File

@ -26,6 +26,8 @@ public abstract partial class AssistantBase : ComponentBase
private protected virtual RenderFragment? Body => null; private protected virtual RenderFragment? Body => null;
protected virtual bool ShowResult => true;
protected static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new(); protected static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
protected AIStudio.Settings.Provider providerSettings; protected AIStudio.Settings.Provider providerSettings;

View File

@ -12,6 +12,7 @@
<MudStack Row="@true" Wrap="@Wrap.Wrap" Class="mb-3"> <MudStack Row="@true" Wrap="@Wrap.Wrap" Class="mb-3">
<AssistantBlock Name="Text Summarizer" Description="Using a LLM to summarize a given text." Icon="@Icons.Material.Filled.TextSnippet" Link="/assistant/summarizer"/> <AssistantBlock Name="Text Summarizer" Description="Using a LLM to summarize a given text." Icon="@Icons.Material.Filled.TextSnippet" Link="/assistant/summarizer"/>
<AssistantBlock Name="Translation" Description="Translate text into another language." Icon="@Icons.Material.Filled.Translate" Link="/assistant/translation"/> <AssistantBlock Name="Translation" Description="Translate text into another language." Icon="@Icons.Material.Filled.Translate" Link="/assistant/translation"/>
<AssistantBlock Name="Grammar & Spelling" Description="Check grammar and spelling of a given text." Icon="@Icons.Material.Filled.Edit" Link="/assistant/grammar-spelling"/>
</MudStack> </MudStack>
<MudText Typo="Typo.h4" Class="mb-2 mr-3 mt-6"> <MudText Typo="Typo.h4" Class="mb-2 mr-3 mt-6">

View File

@ -0,0 +1,11 @@
@using AIStudio.Tools
@page "/assistant/grammar-spelling"
@inherits AssistantBaseCore
<MudTextField T="string" @bind-Text="@this.inputText" Validation="@this.ValidateText" AdornmentIcon="@Icons.Material.Filled.DocumentScanner" Adornment="Adornment.Start" Label="Your input to check" Variant="Variant.Outlined" Lines="6" AutoGrow="@true" MaxLines="12" Class="mb-3" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
<EnumSelection T="CommonLanguages" NameFunc="@(language => language.NameSelectingOptional())" @bind-Value="@this.selectedTargetLanguage" Icon="@Icons.Material.Filled.Translate" Label="Language" AllowOther="@true" OtherValue="CommonLanguages.OTHER" @bind-OtherInput="@this.customTargetLanguage" ValidateOther="@this.ValidateCustomLanguage" LabelOther="Custom language" />
<ProviderSelection @bind-ProviderSettings="@this.providerSettings" ValidateProvider="@this.ValidatingProvider"/>
<MudButton Variant="Variant.Filled" Class="mb-3" OnClick="() => this.ProofreadText()">
Proofread
</MudButton>

View File

@ -0,0 +1,73 @@
using AIStudio.Tools;
namespace AIStudio.Components.Pages.GrammarSpelling;
public partial class AssistantGrammarSpelling : AssistantBaseCore
{
protected override string Title => "Grammar and Spelling Checker";
protected override string Description =>
"""
Check the grammar and spelling of a text.
""";
protected override string SystemPrompt =>
$"""
You get a text as input. The user wants you to check the grammar and spelling of the text.
Correct any spelling or grammar mistakes. Do not add any information. Do not ask for additional information.
Do not improve the text. Do not mirror the user's language. Do not mirror your task.{this.SystemPromptLanguage()}
""";
protected override bool ShowResult => true;
private string originalText = string.Empty;
private string inputText = string.Empty;
private CommonLanguages selectedTargetLanguage;
private string customTargetLanguage = string.Empty;
private string? ValidateText(string text)
{
if(string.IsNullOrWhiteSpace(text))
return "Please provide a text as input. You might copy the desired text from a document or a website.";
return null;
}
private string? ValidateCustomLanguage(string language)
{
if(this.selectedTargetLanguage == CommonLanguages.OTHER && string.IsNullOrWhiteSpace(language))
return "Please provide a custom language.";
return null;
}
private string SystemPromptLanguage()
{
var lang = this.selectedTargetLanguage switch
{
CommonLanguages.AS_IS => string.Empty,
CommonLanguages.OTHER => this.customTargetLanguage,
_ => this.selectedTargetLanguage.Name(),
};
if (string.IsNullOrWhiteSpace(lang))
return string.Empty;
return
$"""
The text is written in {lang}.
""";
}
private async Task ProofreadText()
{
if (!this.inputIsValid)
return;
this.CreateChatThread();
var time = this.AddUserRequest(this.inputText);
await this.AddAIResponseAsync(time);
}
}

View File

@ -42,4 +42,12 @@ public static class CommonLanguageExtensions
return language.Name(); return language.Name();
} }
public static string NameSelectingOptional(this CommonLanguages language)
{
if(language is CommonLanguages.AS_IS)
return "Do not specify the language";
return language.Name();
}
} }

View File

@ -1,2 +1,3 @@
# v0.8.8, build 170 # v0.8.8, build 170
- Added a grammar and spell checker assistant
- Upgraded MudBlazor to v7.6.0 - Upgraded MudBlazor to v7.6.0