Skip to content

Commit

Permalink
把分析结果的文本框改为用HTML渲染Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ssshinchan committed Dec 22, 2024
1 parent 7988382 commit e789d3e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 19 deletions.
27 changes: 27 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "JA Learner Development",
"image": "mcr.microsoft.com/dotnet/sdk:6.0",
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "lts"
},
"ghcr.io/devcontainers/features/git:1": {}
},
"postCreateCommand": "dotnet restore && npm install -g @vue/cli",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csharp",
"ms-dotnettools.vscode-dotnet-runtime",
"Vue.volar",
"dbaeumer.vscode-eslint",
"ms-vscode.powershell"
]
}
},
"forwardPorts": [
5000,
5001,
8080
]
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,6 @@ FodyWeavers.xsd
*.sln.iml

config.txt
extra_prompts/
extra_prompts/
dist_vue
.vscode
50 changes: 37 additions & 13 deletions GUI/TranslationPanel.Designer.cs

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

9 changes: 6 additions & 3 deletions GUI/TranslationPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Markdig;
using System.Web;

namespace ja_learner.GUI
{
Expand All @@ -27,13 +29,14 @@ public void UpdateText(string text)

private void buttonInterpret_Click(object sender, EventArgs e)
{
textBoxResult.Text = "";
var markdownContent = "";
buttonInterpret.Enabled = false;
var chat = GptCaller.CreateInterpretConversation(textBoxSentence.Text);

GptCaller.StreamResponse(chat, res =>
{
textBoxResult.Text += res.Replace("\n", "\r\n");
textBoxResult.ScrollToCaret();
markdownContent += res;
webBrowserResult.Document.InvokeScript("setMarkdown", new object[] { Markdown.ToHtml(markdownContent) });
});

buttonInterpret.Enabled = true;
Expand Down
2 changes: 2 additions & 0 deletions GptCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static void SetProxy(bool useProxy)
public static Conversation CreateTranslateConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
conversation.Model = Program.APP_SETTING.GPT.Model;
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.TranslatePrompt);
if (UserConfig.useExtraPrompt)
{
Expand All @@ -45,6 +46,7 @@ public static Conversation CreateTranslateConversation(string text)
public static Conversation CreateInterpretConversation(string text)
{
Conversation conversation = api.Chat.CreateConversation();
conversation.Model = Program.APP_SETTING.GPT.Model;
conversation.AppendSystemMessage(Program.APP_SETTING.GPT.ExplainPrompt);
if (UserConfig.useExtraPrompt)
{
Expand Down
3 changes: 3 additions & 0 deletions Model/AppSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class GPTOptions
{
public string ApiKey { get; set; } = string.Empty;
public string ApiUrl { get; set; } = string.Empty;

public string Model { get; set; } = string.Empty;

public string ExtraPromptDir { get; set; } = string.Empty;
public string TranslatePrompt { get; set; } = string.Empty;
public string ExplainPrompt { get; set; } = string.Empty;
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"GPT": {
"ApiKey": "sk-xxx",
"ApiUrl": "https://api.openai.com/{0}/{1}", // 实际调用为https://api.openai.com/v1/chat/completions
"Model": "gpt-4o-mini", // 设置模型
"ExtraPromptDir": "extra_prompts", // 额外的Prompt,比如指定某些角色名字怎么翻译
"TranslatePrompt": "...", // 翻译Prompt
"ExplainPrompt": "..." // 分析Prompt
Expand Down
1 change: 1 addition & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"GPT": {
"ApiKey": "sk-xxx",
"ApiUrl": "https://api.openai.com/{0}/{1}",
"Model": "deepseek-chat",
"ExtraPromptDir": "extra_prompts",
"TranslatePrompt": "Translate the text to Simplified Chinese. Don't output anything other than translation results.",
"ExplainPrompt": "List and explain the vocabulary (except prepositions) and grammar of the given Japanese text in Simplified Chinese. Your output consists of three parts: translation, vocabulary, grammar. Don't use English and romaji."
Expand Down
6 changes: 4 additions & 2 deletions ja-learner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<MeCabUseDefaultDictionary>True</MeCabUseDefaultDictionary>
<MeCabUseDefaultDictionary>True</MeCabUseDefaultDictionary>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MeCab.DotNet" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1774.30" />
<PackageReference Include="OpenAI" Version="1.7.2" />
<PackageReference Include="OpenAI" Version="1.11.0" />
<PackageReference Include="Markdig" Version="0.30.4" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e789d3e

Please sign in to comment.