mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-17 16:58:00 +00:00
Add default configuration for assistants (#52)
This commit is contained in:
1 parent
a1c4618796
commit
b3b8bf23dc
42 files changed
+729
-138
No files matched your search
@@ -0,0 +1,50 @@
|
||||
@page "/assistant/translation"
|
||||
@using AIStudio.Settings
|
||||
@using AIStudio.Tools
|
||||
@inherits AssistantBaseCore
|
||||
|
||||
<MudField Label="Live translation" Variant="Variant.Outlined" Class="mb-3">
|
||||
<MudSwitch T="bool" @bind-Value="@this.liveTranslation" Color="Color.Primary">
|
||||
@(this.liveTranslation ? "Live translation" : "No live translation")
|
||||
</MudSwitch>
|
||||
</MudField>
|
||||
|
||||
@if (this.liveTranslation)
|
||||
{
|
||||
<MudTextField T="string" @bind-Text="@this.inputText" Validation="@this.ValidatingText" AdornmentIcon="@Icons.Material.Filled.DocumentScanner" Adornment="Adornment.Start" Label="Your input" Variant="Variant.Outlined" Lines="6" AutoGrow="@true" MaxLines="12" Class="mb-3" Immediate="@true" DebounceInterval="1_000" OnDebounceIntervalElapsed="() => this.TranslateText(force: false)" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudTextField T="string" @bind-Text="@this.inputText" Validation="@this.ValidatingText" AdornmentIcon="@Icons.Material.Filled.DocumentScanner" Adornment="Adornment.Start" Label="Your input" Variant="Variant.Outlined" Lines="6" AutoGrow="@true" MaxLines="12" Class="mb-3" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
|
||||
}
|
||||
|
||||
<MudStack Row="@true" AlignItems="AlignItems.Center" Class="mb-3">
|
||||
<MudSelect T="CommonLanguages" @bind-Value="@this.selectedTargetLanguage" Validation="@this.ValidatingTargetLanguage" AdornmentIcon="@Icons.Material.Filled.Translate" Adornment="Adornment.Start" Label="Target language" Variant="Variant.Outlined" Margin="Margin.Dense">
|
||||
@foreach (var targetLanguage in Enum.GetValues<CommonLanguages>())
|
||||
{
|
||||
if (targetLanguage is CommonLanguages.AS_IS)
|
||||
{
|
||||
<MudSelectItem Value="@targetLanguage">Please select the target language</MudSelectItem>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudSelectItem Value="@targetLanguage">@targetLanguage.Name()</MudSelectItem>
|
||||
}
|
||||
}
|
||||
</MudSelect>
|
||||
@if (this.selectedTargetLanguage is CommonLanguages.OTHER)
|
||||
{
|
||||
<MudTextField T="string" @bind-Text="@this.customTargetLanguage" Validation="@this.ValidateCustomLanguage" Label="Custom target language" Variant="Variant.Outlined" Margin="Margin.Dense" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
|
||||
}
|
||||
</MudStack>
|
||||
|
||||
<MudSelect T="Provider" @bind-Value="@this.providerSettings" Validation="@this.ValidatingProvider" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Apps" Margin="Margin.Dense" Label="Provider" Class="mb-3 rounded-lg" Variant="Variant.Outlined">
|
||||
@foreach (var provider in this.SettingsManager.ConfigurationData.Providers)
|
||||
{
|
||||
<MudSelectItem Value="@provider"/>
|
||||
}
|
||||
</MudSelect>
|
||||
|
||||
<MudButton Variant="Variant.Filled" Class="mb-3" OnClick="() => this.TranslateText(force: true)">
|
||||
Translate
|
||||
</MudButton>
|
||||
@@ -0,0 +1,91 @@
|
||||
using AIStudio.Tools;
|
||||
|
||||
namespace AIStudio.Components.Pages.Translation;
|
||||
|
||||
public partial class AssistantTranslation : AssistantBaseCore
|
||||
{
|
||||
protected override string Title => "Translation";
|
||||
|
||||
protected override string Description =>
|
||||
"""
|
||||
Translate text from one language to another.
|
||||
""";
|
||||
|
||||
protected override string SystemPrompt =>
|
||||
"""
|
||||
You get text in a source language as input. The user wants to get the text translated into a target language.
|
||||
Provide the translation in the requested language. Do not add any information. Correct any spelling or grammar mistakes.
|
||||
Do not ask for additional information. Do not mirror the user's language. Do not mirror the task. When the target
|
||||
language requires, e.g., shorter sentences, you should split the text into shorter sentences.
|
||||
""";
|
||||
|
||||
private bool liveTranslation;
|
||||
private string inputText = string.Empty;
|
||||
private string inputTextLastTranslation = string.Empty;
|
||||
private CommonLanguages selectedTargetLanguage;
|
||||
private string customTargetLanguage = string.Empty;
|
||||
|
||||
#region Overrides of ComponentBase
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (this.SettingsManager.ConfigurationData.PreselectTranslationOptions)
|
||||
{
|
||||
this.liveTranslation = this.SettingsManager.ConfigurationData.PreselectLiveTranslation;
|
||||
this.selectedTargetLanguage = this.SettingsManager.ConfigurationData.PreselectedTranslationTargetLanguage;
|
||||
this.customTargetLanguage = this.SettingsManager.ConfigurationData.PreselectTranslationOtherLanguage;
|
||||
this.providerSettings = this.SettingsManager.ConfigurationData.Providers.FirstOrDefault(x => x.Id == this.SettingsManager.ConfigurationData.PreselectedTranslationProvider);
|
||||
}
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
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? ValidatingTargetLanguage(CommonLanguages language)
|
||||
{
|
||||
if(language == CommonLanguages.AS_IS)
|
||||
return "Please select a target language.";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string? ValidateCustomLanguage(string language)
|
||||
{
|
||||
if(this.selectedTargetLanguage == CommonLanguages.OTHER && string.IsNullOrWhiteSpace(language))
|
||||
return "Please provide a custom language.";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task TranslateText(bool force)
|
||||
{
|
||||
if (!this.inputIsValid)
|
||||
return;
|
||||
|
||||
if(!force && this.inputText == this.inputTextLastTranslation)
|
||||
return;
|
||||
|
||||
this.inputTextLastTranslation = this.inputText;
|
||||
this.CreateChatThread();
|
||||
var time = this.AddUserRequest(
|
||||
$"""
|
||||
{this.selectedTargetLanguage.Prompt(this.customTargetLanguage)}
|
||||
|
||||
The given text is:
|
||||
|
||||
---
|
||||
{this.inputText}
|
||||
""");
|
||||
|
||||
await this.AddAIResponseAsync(time);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using AIStudio.Tools;
|
||||
|
||||
namespace AIStudio.Components.Pages.Translation;
|
||||
|
||||
public static class CommonLanguageExtension
|
||||
{
|
||||
public static string Prompt(this CommonLanguages language, string customLanguage) => language switch
|
||||
{
|
||||
CommonLanguages.OTHER => $"Translate the text in {customLanguage}.",
|
||||
|
||||
_ => $"Translate the given text in {language.Name()} ({language}).",
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user