From 4554cde08577c0929f22a3defb1336ddd74f85b3 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 14 Jul 2024 20:34:16 +0200 Subject: [PATCH] Added text summarization assistant --- .../AssistantTextSummarizer.razor | 43 ++++++++++ .../AssistantTextSummarizer.razor.cs | 86 +++++++++++++++++++ .../TextSummarizer/CommonLanguagePrompts.cs | 14 +++ .../Pages/TextSummarizer/Complexity.cs | 13 +++ .../TextSummarizer/ComplexityExtensions.cs | 32 +++++++ .../Tools/CommonLanguageExtensions.cs | 22 +++++ .../Tools/CommonLanguages.cs | 19 ++++ 7 files changed, 229 insertions(+) create mode 100644 app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor create mode 100644 app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor.cs create mode 100644 app/MindWork AI Studio/Components/Pages/TextSummarizer/CommonLanguagePrompts.cs create mode 100644 app/MindWork AI Studio/Components/Pages/TextSummarizer/Complexity.cs create mode 100644 app/MindWork AI Studio/Components/Pages/TextSummarizer/ComplexityExtensions.cs create mode 100644 app/MindWork AI Studio/Tools/CommonLanguageExtensions.cs create mode 100644 app/MindWork AI Studio/Tools/CommonLanguages.cs diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor new file mode 100644 index 00000000..56da6e60 --- /dev/null +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor @@ -0,0 +1,43 @@ +@page "/assistant/summarizer" +@using AIStudio.Settings +@using AIStudio.Tools +@inherits AssistantBaseCore + + + + + + @foreach (var targetLanguage in Enum.GetValues()) + { + @targetLanguage.Name() + } + + @if (this.selectedTargetLanguage is CommonLanguages.OTHER) + { + + } + + + + + @foreach (var targetComplexity in Enum.GetValues()) + { + @targetComplexity.Name() + } + + @if (this.selectedComplexity is Complexity.SCIENTIFIC_LANGUAGE_OTHER_EXPERTS) + { + + } + + + + @foreach (var provider in this.SettingsManager.ConfigurationData.Providers) + { + + } + + + + Summarize + \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor.cs b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor.cs new file mode 100644 index 00000000..2bf92e44 --- /dev/null +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor.cs @@ -0,0 +1,86 @@ +using AIStudio.Provider; +using AIStudio.Tools; + +namespace AIStudio.Components.Pages.TextSummarizer; + +public partial class AssistantTextSummarizer : AssistantBaseCore +{ + protected override string Title => "Text Summarizer"; + + protected override string Description => + """ + Summarize long text into a shorter version while retaining the main points. + You might want to change the language of the summary to make it more readable. + It is also possible to change the complexity of the summary to make it + easy to understand. + """; + + protected override string SystemPrompt => + """ + You get a long text as input. The user wants to get a summary of the text. + The user might want to change the language of the summary. In this case, + you should provide a summary in the requested language. Eventually, the user + want to change the complexity of the text. In this case, you should provide + a summary with the requested complexity. In any case, do not add any information. + """; + + private string inputText = string.Empty; + private CommonLanguages selectedTargetLanguage; + private string customTargetLanguage = string.Empty; + private Complexity selectedComplexity; + private string expertInField = string.Empty; + + private string? ValidatingText(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? ValidatingProvider(AIStudio.Settings.Provider provider) + { + if(provider.UsedProvider == Providers.NONE) + return "Please select a provider."; + + 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? ValidateExpertInField(string field) + { + if(this.selectedComplexity == Complexity.SCIENTIFIC_LANGUAGE_OTHER_EXPERTS && string.IsNullOrWhiteSpace(field)) + return "Please provide your field of expertise."; + + return null; + } + + private async Task SummarizeText() + { + await this.form!.Validate(); + if (!this.inputIsValid) + return; + + this.CreateChatThread(); + var time = this.AddUserRequest( + $""" + {this.selectedTargetLanguage.Prompt(this.customTargetLanguage)} + {this.selectedComplexity.Prompt(this.expertInField)} + + Please summarize the following text: + + ``` + {this.inputText} + ``` + """); + + await this.AddAIResponseAsync(time); + } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/CommonLanguagePrompts.cs b/app/MindWork AI Studio/Components/Pages/TextSummarizer/CommonLanguagePrompts.cs new file mode 100644 index 00000000..73bd72cb --- /dev/null +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/CommonLanguagePrompts.cs @@ -0,0 +1,14 @@ +using AIStudio.Tools; + +namespace AIStudio.Components.Pages.TextSummarizer; + +public static class CommonLanguagePrompts +{ + public static string Prompt(this CommonLanguages language, string customLanguage) => language switch + { + CommonLanguages.AS_IS => "Do not change the language of the text.", + CommonLanguages.OTHER => $"Output you summary in {customLanguage}.", + + _ => $"Output your summary in {language.Name()} ({language}).", + }; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/Complexity.cs b/app/MindWork AI Studio/Components/Pages/TextSummarizer/Complexity.cs new file mode 100644 index 00000000..0c9125e8 --- /dev/null +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/Complexity.cs @@ -0,0 +1,13 @@ +namespace AIStudio.Components.Pages.TextSummarizer; + +public enum Complexity +{ + NO_CHANGE, + + SIMPLE_LANGUAGE, + TEEN_LANGUAGE, + EVERYDAY_LANGUAGE, + POPULAR_SCIENCE_LANGUAGE, + SCIENTIFIC_LANGUAGE_FIELD_EXPERTS, + SCIENTIFIC_LANGUAGE_OTHER_EXPERTS, +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/ComplexityExtensions.cs b/app/MindWork AI Studio/Components/Pages/TextSummarizer/ComplexityExtensions.cs new file mode 100644 index 00000000..19a335c7 --- /dev/null +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/ComplexityExtensions.cs @@ -0,0 +1,32 @@ +namespace AIStudio.Components.Pages.TextSummarizer; + +public static class ComplexityExtensions +{ + public static string Name(this Complexity complexity) => complexity switch + { + Complexity.NO_CHANGE => "No change in complexity", + + Complexity.SIMPLE_LANGUAGE => "Simple language, e.g., for children", + Complexity.TEEN_LANGUAGE => "Teen language, e.g., for teenagers", + Complexity.EVERYDAY_LANGUAGE => "Everyday language, e.g., for adults", + Complexity.POPULAR_SCIENCE_LANGUAGE => "Popular science language, e.g., for people interested in science", + Complexity.SCIENTIFIC_LANGUAGE_FIELD_EXPERTS => "Scientific language for experts in this field", + Complexity.SCIENTIFIC_LANGUAGE_OTHER_EXPERTS => "Scientific language for experts from other fields (interdisciplinary)", + + _ => "No change in complexity", + }; + + public static string Prompt(this Complexity complexity, string expertInField) => complexity switch + { + Complexity.NO_CHANGE => "Do not change the complexity of the text.", + + Complexity.SIMPLE_LANGUAGE => "Simplify the language, e.g., for 8 to 12-year-old children. Might use short sentences and simple words. You could use analogies to explain complex terms.", + Complexity.TEEN_LANGUAGE => "Use a language suitable for teenagers, e.g., 16 to 19 years old. Might use teenage slang and analogies to explain complex terms.", + Complexity.EVERYDAY_LANGUAGE => "Use everyday language suitable for adults. Avoid specific scientific terms. Use everday analogies to explain complex terms.", + Complexity.POPULAR_SCIENCE_LANGUAGE => "Use popular science language, e.g., for people interested in science. The text should be easy to understand, though. Use analogies to explain complex terms.", + Complexity.SCIENTIFIC_LANGUAGE_FIELD_EXPERTS => "Use scientific language for experts in the field of the texts subject. Use specific terms for this field.", + Complexity.SCIENTIFIC_LANGUAGE_OTHER_EXPERTS => $"The reader is an expert in {expertInField}. Change the language so that it is suitable. Explain specific terms, so that the reader within his field can understand the text. You might use analogies to explain complex terms.", + + _ => "Do not change the complexity of the text.", + }; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/CommonLanguageExtensions.cs b/app/MindWork AI Studio/Tools/CommonLanguageExtensions.cs new file mode 100644 index 00000000..5d932a2a --- /dev/null +++ b/app/MindWork AI Studio/Tools/CommonLanguageExtensions.cs @@ -0,0 +1,22 @@ +namespace AIStudio.Tools; + +public static class CommonLanguageExtensions +{ + public static string Name(this CommonLanguages language) => language switch + { + CommonLanguages.AS_IS => "Do not change the language", + + CommonLanguages.EN_US => "English (US)", + CommonLanguages.EN_GB => "English (UK)", + CommonLanguages.ZH_CN => "Chinese (Simplified)", + CommonLanguages.HI_IN => "Hindi (India)", + CommonLanguages.ES_ES => "Spanish (Spain)", + CommonLanguages.FR_FR => "French (France)", + CommonLanguages.DE_DE => "German (Germany)", + CommonLanguages.DE_AT => "German (Austria)", + CommonLanguages.DE_CH => "German (Switzerland)", + CommonLanguages.JA_JP => "Japanese (Japan)", + + _ => "Other", + }; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/CommonLanguages.cs b/app/MindWork AI Studio/Tools/CommonLanguages.cs new file mode 100644 index 00000000..afa83f6a --- /dev/null +++ b/app/MindWork AI Studio/Tools/CommonLanguages.cs @@ -0,0 +1,19 @@ +namespace AIStudio.Tools; + +public enum CommonLanguages +{ + AS_IS, + + EN_US, + EN_GB, + ZH_CN, + HI_IN, + ES_ES, + FR_FR, + DE_DE, + DE_CH, + DE_AT, + JA_JP, + + OTHER, +} \ No newline at end of file