Skip to content

Commit

Permalink
修bug,添加自定义额外prompt
Browse files Browse the repository at this point in the history
ks233 committed Oct 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d1f322a commit c57a261
Showing 8 changed files with 88 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -397,4 +397,5 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

config.txt
config.txt
extra_prompt.txt
27 changes: 27 additions & 0 deletions GUI/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
using System.Text;
using System.Reflection.Metadata;
using OpenAI_API.Chat;
using Microsoft.VisualBasic;

namespace ja_learner
{
@@ -61,7 +62,8 @@ private async void MainForm_Load(object sender, EventArgs e)
#endif

dictForm = new DictForm(this);

dictForm.Show();
dictForm.Hide();
}

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
@@ -102,7 +104,10 @@ private void MainForm_SizeChanged(object sender, EventArgs e)
if (timerWindowAttach.Enabled)
{
heightAfter = this.Height;
dictForm.Width = this.Right - WindowAttacher.TargetWindowRect.Right;
if (dictForm.Visible)
{
dictForm.Width = this.Right - WindowAttacher.TargetWindowRect.Right;
}
}
}

@@ -270,5 +275,15 @@ private void checkBoxTopmost_CheckedChanged(object sender, EventArgs e)
TopMost = checkBoxTopmost.Checked;
}

private void buttonUpdateExtraPrompt_Click(object sender, EventArgs e)
{
UserConfig.UpdateExtraPrompt();
MessageBox.Show("当前额外Prompt:\n" + UserConfig.extraPrompt);
}

private void checkBoxUseExtraPrompt_CheckedChanged(object sender, EventArgs e)
{
UserConfig.useExtraPrompt = checkBoxUseExtraPrompt.Checked;
}
}
}
1 change: 1 addition & 0 deletions GUI/TranslationPanel.cs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ public void UpdateText(string text)

async private void buttonInterpret_Click(object sender, EventArgs e)
{
textBoxResult.Text = "";
buttonInterpret.Enabled = false;
var chat = GptCaller.CreateInterpretConversation(textBoxSentence.Text);
GptCaller.StreamResponse(chat, res =>
18 changes: 17 additions & 1 deletion GptCaller.cs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ internal class GptCaller
private static OpenAIAPI api;

public static void Initialize() {
SetConfig(UserConfig.api_key, UserConfig.api_url);
SetConfig(UserConfig.apiKey, UserConfig.apiUrl);
}

public static void SetConfig(string api_key, string api_url)
@@ -27,6 +27,10 @@ public static Conversation CreateTranslateConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
conversation.AppendSystemMessage("You are a translation engine, translate the text to Simplified Chinese. Don't output anything other than translation results.");
if (UserConfig.useExtraPrompt)
{
AddExtraSystemPrompt(conversation);
}
conversation.AppendUserInput($"{text}");
return conversation;
}
@@ -35,10 +39,22 @@ public static Conversation CreateInterpretConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
conversation.AppendSystemMessage("You are a Japanese teacher, List and explain the vocabulary (except prepositions) and grammar of the given text in Simplified Chinese. Your output consists of three parts: translation, vocabulary, grammar. Don't use English and romaji.");
if(UserConfig.useExtraPrompt)
{
AddExtraSystemPrompt(conversation);
}
conversation.AppendUserInput($"{text}");
return conversation;
}

private static void AddExtraSystemPrompt(Conversation conversation)
{
if (UserConfig.extraPrompt.Length > 0)
{
conversation.AppendSystemMessage(UserConfig.extraPrompt);
}
}

async public static void StreamResponse(Conversation conversation, Action<string> callback)
{
try
22 changes: 17 additions & 5 deletions UserConfig.cs
Original file line number Diff line number Diff line change
@@ -8,23 +8,35 @@ namespace ja_learner
{
internal class UserConfig
{
public static string api_key = "";
public static string api_url = "";
public static string apiKey = "";
public static string apiUrl = "";
public static string extraPrompt = "";
public static bool useExtraPrompt = false;

public static void ReadConfigFile()
{
string filePath = "config.txt";

// 逐行读取config.txt,第一行key第二行url
using (StreamReader reader = new StreamReader(filePath))
{
try
{
api_key = reader.ReadLine();
api_url = reader.ReadLine();
apiKey = reader.ReadLine();
apiUrl = reader.ReadLine();
}
catch { }
}
UpdateExtraPrompt();
}

public static void UpdateExtraPrompt()
{
string filePath = "extra_prompt.txt";
try
{
extraPrompt = File.ReadAllText(filePath);
}
catch { }
}
}
}
7 changes: 4 additions & 3 deletions WindowAttacher.cs
Original file line number Diff line number Diff line change
@@ -110,16 +110,17 @@ public static void AttachWindows(Form form, Form dictForm)
// 对齐宽度
if (dictForm.Visible)
{
dictForm.Top = rect.Top;
dictForm.Left = rect.Right;
dictForm.Height = rect.Bottom - rect.Top;
form.Width = rect.Right - rect.Left + dictForm.Width;
}
else
{
form.Width = rect.Right - rect.Left;
}

dictForm.Top = rect.Top;
dictForm.Left = rect.Right;
dictForm.Height = rect.Bottom - rect.Top;

}
}
}
3 changes: 3 additions & 0 deletions ja-learner.csproj
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@
<None Update="config.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="extra_prompt.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit c57a261

Please sign in to comment.