Skip to content

Commit

Permalink
feat: add preset prompts for chatglm and gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Mar 13, 2024
1 parent 5da724b commit dfacad2
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace STranslate.ViewModels.Preference.Services
{
public partial class TranslatorChatglm : ObservableObject, ITranslatorAI
{
#region Constructor

public TranslatorChatglm() : this(Guid.NewGuid(), "https://open.bigmodel.cn/api/paas/v4/chat/completions", "智谱AI")
{
}
Expand All @@ -29,8 +31,14 @@ public TranslatorChatglm(Guid guid, string url, string name = "", IconType icon
AppKey = appKey;
IsEnabled = isEnabled;
Type = type;

PromptCounter = Prompts.Count;
}

#endregion Constructor

#region Properties

[ObservableProperty]
private Guid _identify = Guid.Empty;

Expand Down Expand Up @@ -98,9 +106,44 @@ public TranslatorChatglm(Guid guid, string url, string name = "", IconType icon

#endregion Show/Hide Encrypt Info

#region Prompt

[JsonIgnore]
[ObservableProperty]
private BindingList<UserDefinePrompt> userDefinePrompts = [];
private BindingList<UserDefinePrompt> _userDefinePrompts =
[
new UserDefinePrompt("翻译", [new Prompt("user", "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it."),new Prompt("assistant", "Ok, I will only translate the text content, never interpret it."),new Prompt("user", "Translate the following text from en to zh: hello world"),new Prompt("assistant", "你好,世界"), new Prompt("user", "Translate the following text from $source to $target: $content")]),
new UserDefinePrompt("润色", [new Prompt("user", "You are a text embellisher, you can only embellish the text, never interpret it."), new Prompt("assistant", "Ok, I will only embellish the text, never interpret it."), new Prompt("user", "Embellish the following text in $source: $content")]),
new UserDefinePrompt("总结", [new Prompt("user", "You are a text summarizer, you can only summarize the text, never interpret it."), new Prompt("assistant", "Ok, I will only summarize the text, never interpret it."), new Prompt("user", "Summarize the following text in $source: $content")]),
];

[RelayCommand]
[property: JsonIgnore]
private void PresetPrompt(BindingList<Prompt> prompts)
{
Prompts = prompts.Clone();
PromptCounter = Prompts.Count;
}

[RelayCommand]
[property: JsonIgnore]
private void EditPresetPrompt(UserDefinePrompt userDefinePrompt)
{
var dialog = new Views.Preference.Service.PromptDialog(ServiceType.OpenAIService, (UserDefinePrompt)userDefinePrompt.Clone());
if (dialog.ShowDialog() ?? false)
{
var tmp = ((PromptViewModel)dialog.DataContext).UserDefinePrompt;
userDefinePrompt.Name = tmp.Name;
userDefinePrompt.Prompts = tmp.Prompts;
}
}

[RelayCommand]
[property: JsonIgnore]
private void DelPresetPrompt(UserDefinePrompt userDefinePrompt)
{
UserDefinePrompts.Remove(userDefinePrompt);
}

[JsonIgnore]
[ObservableProperty]
Expand All @@ -118,6 +161,7 @@ public TranslatorChatglm(Guid guid, string url, string name = "", IconType icon
private void DeletePrompt(Prompt msg)
{
Prompts.Remove(msg);
PromptCounter--;
}

[RelayCommand]
Expand All @@ -131,8 +175,30 @@ private void AddPrompt()
_ => new Prompt("user")
};
Prompts.Add(newOne);
PromptCounter++;
}

[property: JsonIgnore]
[RelayCommand(CanExecute = nameof(CanSavePrompt))]
private void SavePrompt(BindingList<Prompt> prompts)
{
UserDefinePrompts.Add(new UserDefinePrompt("UnDefined", prompts));
}

private bool CanSavePrompt => PromptCounter > 0;

[JsonIgnore]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SavePromptCommand))]
[property: JsonIgnore]
private int promptCounter;

#endregion Prompt

#endregion Properties

#region Interface Implementation

public async Task TranslateAsync(object request, Action<string> OnDataReceived, CancellationToken token)
{
if (string.IsNullOrEmpty(Url) || string.IsNullOrEmpty(AppKey))
Expand Down Expand Up @@ -231,7 +297,10 @@ public ITranslator Clone()
Icons = this.Icons,
KeyHide = this.KeyHide,
Prompts = this.Prompts,
UserDefinePrompts = this.UserDefinePrompts,
};
}

#endregion Interface Implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace STranslate.ViewModels.Preference.Services
{
public partial class TranslatorGemini : ObservableObject, ITranslatorAI
{
#region Constructor

public TranslatorGemini()
: this(Guid.NewGuid(), "https://generativelanguage.googleapis.com", "Gemini") { }

Expand All @@ -37,8 +39,14 @@ public TranslatorGemini(
AppKey = appKey;
IsEnabled = isEnabled;
Type = type;

PromptCounter = Prompts.Count;
}

#endregion Constructor

#region Properties

[ObservableProperty]
private Guid _identify = Guid.Empty;

Expand Down Expand Up @@ -104,12 +112,48 @@ public TranslatorGemini(

#endregion Show/Hide Encrypt Info

#region Prompt

[JsonIgnore]
[ObservableProperty]
private BindingList<UserDefinePrompt> userDefinePrompts = [];
private BindingList<UserDefinePrompt> _userDefinePrompts =
[
new UserDefinePrompt("翻译", [new Prompt("user", "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it."),new Prompt("model", "Ok, I will only translate the text content, never interpret it"),new Prompt("user", "Translate the following text from en to zh: hello world"),new Prompt("model", "你好,世界"), new Prompt("user", "Translate the following text from $source to $target: $content")]),
new UserDefinePrompt("润色", [new Prompt("user", "You are a text embellisher, you can only embellish the text, never interpret it."), new Prompt("model", "Ok, I will only embellish the text, never interpret it."), new Prompt("user", "Embellish the following text in $source: $content")]),
new UserDefinePrompt("总结", [new Prompt("user", "You are a text summarizer, you can only summarize the text, never interpret it."), new Prompt("model", "Ok, I will only summarize the text, never interpret it."), new Prompt("user", "Summarize the following text in $source: $content")]),
];

[RelayCommand]
[property: JsonIgnore]
private void PresetPrompt(BindingList<Prompt> prompts)
{
Prompts = prompts.Clone();
PromptCounter = Prompts.Count;
}

[RelayCommand]
[property: JsonIgnore]
private void EditPresetPrompt(UserDefinePrompt userDefinePrompt)
{
var dialog = new Views.Preference.Service.PromptDialog(ServiceType.OpenAIService, (UserDefinePrompt)userDefinePrompt.Clone());
if (dialog.ShowDialog() ?? false)
{
var tmp = ((PromptViewModel)dialog.DataContext).UserDefinePrompt;
userDefinePrompt.Name = tmp.Name;
userDefinePrompt.Prompts = tmp.Prompts;
}
}

[RelayCommand]
[property: JsonIgnore]
private void DelPresetPrompt(UserDefinePrompt userDefinePrompt)
{
UserDefinePrompts.Remove(userDefinePrompt);
}

[JsonIgnore]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SavePromptCommand))]
private BindingList<Prompt> prompts =
[
new Prompt("user", "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it."),
Expand All @@ -124,6 +168,7 @@ public TranslatorGemini(
private void DeletePrompt(Prompt msg)
{
Prompts.Remove(msg);
PromptCounter--;
}

[RelayCommand]
Expand All @@ -137,8 +182,30 @@ private void AddPrompt()
_ => new Prompt("user")
};
Prompts.Add(newOne);
PromptCounter++;
}

[property: JsonIgnore]
[RelayCommand(CanExecute = nameof(CanSavePrompt))]
private void SavePrompt(BindingList<Prompt> prompts)
{
UserDefinePrompts.Add(new UserDefinePrompt("UnDefined", prompts));
}

private bool CanSavePrompt => PromptCounter > 0;

[JsonIgnore]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SavePromptCommand))]
[property: JsonIgnore]
private int promptCounter;

#endregion Prompt

#endregion Properties

#region Interface Implementation

public async Task TranslateAsync(object request, Action<string> OnDataReceived, CancellationToken token)
{
if (string.IsNullOrEmpty(Url) || string.IsNullOrEmpty(AppKey))
Expand Down Expand Up @@ -226,7 +293,10 @@ public ITranslator Clone()
Icons = this.Icons,
KeyHide = this.KeyHide,
Prompts = this.Prompts,
UserDefinePrompts = this.UserDefinePrompts,
};
}

#endregion Interface Implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ private void DelPresetPrompt(UserDefinePrompt userDefinePrompt)
new Prompt("user", "Translate the following text from $source to $target: $content")
];

[JsonIgnore]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SavePromptCommand))]
[property: JsonIgnore]
private int promptCounter;

[RelayCommand]
[property: JsonIgnore]
private void DelPrompt(Prompt msg)
Expand Down Expand Up @@ -192,6 +186,12 @@ private void SavePrompt(BindingList<Prompt> prompts)

private bool CanSavePrompt => PromptCounter > 0;

[JsonIgnore]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SavePromptCommand))]
[property: JsonIgnore]
private int promptCounter;

#endregion Prompt

#endregion Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,54 @@
</Grid>

<StackPanel Margin="0,0,0,20" VerticalAlignment="Top">
<!-- // Preset Prompts // -->
<Border Style="{DynamicResource BorderMainStyle}">
<ListBox Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding UserDefinePrompts}"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="{DynamicResource BtnBackground}"
BorderBrush="{DynamicResource BorderBrushColor}"
BorderThickness="1"
CornerRadius="5">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.PresetPromptCommand}"
CommandParameter="{Binding Prompts}"
Content="{Binding Name}"
Style="{DynamicResource lbtn}" />
<Separator Style="{DynamicResource verSep}" />
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.EditPresetPromptCommand}"
CommandParameter="{Binding Path=.}"
Content="&#xe61b;"
FontFamily="{DynamicResource IconFont}"
FontSize="{DynamicResource FontSize_Identify}"
Style="{DynamicResource btn}" />
<Separator Style="{DynamicResource verSep}" />
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.DelPresetPromptCommand}"
CommandParameter="{Binding Path=.}"
Content="&#xe74b;"
FontFamily="{DynamicResource IconFont}"
FontSize="{DynamicResource FontSize_Prompt}"
Style="{DynamicResource rbtn}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>

<!-- // Current Pormpts // -->
<ListBox Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Prompts}"
Expand Down Expand Up @@ -152,7 +200,18 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Command="{Binding AddPromptCommand}" Content="添加Prompt" FontWeight="Bold" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Command="{Binding AddPromptCommand}" Content="添加Prompt" FontWeight="Bold" />
<Button Grid.Column="1"
Command="{Binding SavePromptCommand}"
CommandParameter="{Binding Prompts}"
Content="保存Prompt"
FontWeight="Bold" />
</Grid>
</StackPanel>

<Grid Margin="0,20">
Expand All @@ -164,7 +223,6 @@
<TextBlock Grid.Column="1" Margin="10,0" ToolTip="在浏览器中打开"><Hyperlink Click="Hyperlink_Click">进入官网</Hyperlink></TextBlock>
</Grid>
</StackPanel>

</ScrollViewer>
</Border>
</UserControl>
Loading

0 comments on commit dfacad2

Please sign in to comment.