Skip to content

Commit d66f690

Browse files
committed
扩展openai api支持
1 parent 2e82f23 commit d66f690

File tree

6 files changed

+116
-42
lines changed

6 files changed

+116
-42
lines changed

Mikoto/IAppSettings.cs

+7
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ string ChatGPTapiUrl
150150
set;
151151
}
152152

153+
[Option(Alias = "ChatGPTTranslator.ApiModel", DefaultValue = "gpt-3.5-turbo")]
154+
string ChatGPTapiModel
155+
{
156+
get;
157+
set;
158+
}
159+
153160
[Option(Alias = "XiaoniuTranslator.XiaoniuApiKey", DefaultValue = "")]
154161
string XiaoniuApiKey
155162
{
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,120 @@
11
<Page x:Class="Mikoto.SettingsPages.TranslatorPages.ChatGPTTransSettingsPage"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:hc="https://handyorg.github.io/handycontrol"
6-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:local="clr-namespace:Mikoto.SettingsPages.TranslatorPages"
8-
mc:Ignorable="d"
9-
d:DesignHeight="450" d:DesignWidth="594"
8+
mc:Ignorable="d"
9+
d:DesignHeight="450"
10+
d:DesignWidth="594"
1011
Title="ChatGPTTransSettingsPage">
1112

1213
<Grid>
1314
<hc:ScrollViewer HorizontalScrollBarVisibility="Disabled">
14-
<StackPanel >
15-
<TextBlock Foreground="{DynamicResource PrimaryForeground}" Text="{DynamicResource ChatGPTTransSettingsPage_Introduce}" FontSize="16" Margin="10" TextWrapping="WrapWithOverflow"/>
15+
<StackPanel>
16+
<TextBlock Foreground="{DynamicResource PrimaryForeground}"
17+
Text="{DynamicResource ChatGPTTransSettingsPage_Introduce}"
18+
FontSize="16"
19+
Margin="10"
20+
TextWrapping="WrapWithOverflow" />
1621
<StackPanel Margin="20,20,20,0">
1722
<Grid Margin="0,10,0,0">
1823
<Grid.ColumnDefinitions>
19-
<ColumnDefinition Width="*"/>
20-
<ColumnDefinition Width="Auto"/>
24+
<ColumnDefinition Width="*" />
25+
<ColumnDefinition Width="Auto" />
2126
</Grid.ColumnDefinitions>
22-
<Grid Grid.ColumnSpan="2">
23-
<hc:TextBox hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="{DynamicResource ChatGPTTransSettingsPage_secretKey}" Name="ChatGPTTransSecretKeyBox" hc:InfoElement.ShowClearButton="True" Margin="0,0,132,0"/>
24-
<!--Button Content="{DynamicResource API_authTestBtn}" Margin="432,0,0,0" Width="122" Click="AuthTestBtn_Click" HorizontalAlignment="Left" /-->
25-
</Grid>
27+
<hc:TextBox hc:TitleElement.TitlePlacement="Left"
28+
hc:TitleElement.Title="{DynamicResource ChatGPTTransSettingsPage_secretKey}"
29+
Name="ChatGPTTransSecretKeyBox"
30+
hc:InfoElement.ShowClearButton="True" />
2631
</Grid>
2732
<Grid Margin="0,10,0,0">
2833
<Grid.ColumnDefinitions>
29-
<ColumnDefinition Width="*"/>
30-
<ColumnDefinition Width="Auto"/>
34+
<ColumnDefinition Width="*" />
35+
<ColumnDefinition Width="Auto" />
3136
</Grid.ColumnDefinitions>
32-
<Grid Grid.ColumnSpan="2">
33-
<hc:TextBox hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="{DynamicResource ChatGPTTransSettingsPage_apiUrl}" Name="ChatGPTTransUrlBox" hc:InfoElement.ShowClearButton="True"
34-
Text="https://api.openai.com/v1/chat/completions" Margin="0,0,132,0"/>
35-
<Button Content="{DynamicResource API_authTestBtn}" Margin="432,0,0,0" Width="122" Click="AuthTestBtn_Click" HorizontalAlignment="Left" />
36-
</Grid>
37+
<hc:TextBox Grid.Column="0"
38+
hc:TitleElement.TitlePlacement="Left"
39+
hc:TitleElement.Title="{DynamicResource ChatGPTTransSettingsPage_apiUrl}"
40+
Name="ChatGPTTransUrlBox"
41+
hc:InfoElement.ShowClearButton="True"
42+
Text="https://api.openai.com/v1/chat/completions" />
3743
</Grid>
38-
<hc:Divider/>
39-
<hc:TextBox hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{DynamicResource API_testSrcText}" Name="TestSrcText" hc:InfoElement.ShowClearButton="True" Text="{DynamicResource API_testCase}"/>
4044
<Grid Margin="0,10,0,0">
4145
<Grid.ColumnDefinitions>
42-
<ColumnDefinition Width="*"/>
43-
<ColumnDefinition Width="50"/>
44-
<ColumnDefinition Width="*"/>
45-
<ColumnDefinition Width="Auto"/>
46+
<ColumnDefinition Width="*" />
47+
<ColumnDefinition Width="Auto" />
4648
</Grid.ColumnDefinitions>
47-
<hc:TextBox hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{DynamicResource API_testSrcLang}" Name="TestSrcLang" hc:InfoElement.ShowClearButton="True" Text="ja" TextAlignment="Center"/>
48-
<hc:TextBox Grid.Column="2" hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{DynamicResource API_testDstLang}" Name="TestDstLang" hc:InfoElement.ShowClearButton="True" Text="zh" TextAlignment="Center"/>
49-
<Button Grid.Column="3" Content="{DynamicResource API_testTransBtn}" Margin="10,0,0,0" Width="122" Click="TransTestBtn_Click" ></Button>
49+
<hc:TextBox Grid.Column="0"
50+
hc:TitleElement.TitlePlacement="Left"
51+
hc:TitleElement.Title="{DynamicResource ChatGPTTransSettingsPage_apiModel}"
52+
Name="ChatGPTModelBox"
53+
hc:InfoElement.ShowClearButton="True"
54+
Text="gpt-3.5-turbo" />
55+
<Button Grid.Column="1"
56+
Content="{DynamicResource API_authTestBtn}"
57+
Margin="10,0,0,0"
58+
Width="122"
59+
Click="AuthTestBtn_Click" />
5060
</Grid>
51-
<StackPanel Margin="0,10,0,0" Orientation="Horizontal" HorizontalAlignment="Right">
52-
<Button Content="{DynamicResource API_applyBtn}" Width="122" Click="ApplyBtn_Click" HorizontalAlignment="Left" />
53-
<Button Content="{DynamicResource API_docBtn}" Margin="10,0,0,0" Width="225" Click="DocBtn_Click" HorizontalAlignment="Left" ></Button>
54-
<Button Content="{DynamicResource API_billBtn}" Margin="10,0,0,0" Width="122" Click="BillBtn_Click" HorizontalAlignment="Right" ></Button>
61+
62+
<hc:Divider />
63+
<hc:TextBox hc:InfoElement.TitlePlacement="Left"
64+
hc:InfoElement.Title="{DynamicResource API_testSrcText}"
65+
Name="TestSrcText"
66+
hc:InfoElement.ShowClearButton="True"
67+
Text="{DynamicResource API_testCase}" />
68+
<Grid Margin="0,10,0,0">
69+
<Grid.ColumnDefinitions>
70+
<ColumnDefinition Width="*" />
71+
<ColumnDefinition Width="50" />
72+
<ColumnDefinition Width="*" />
73+
<ColumnDefinition Width="Auto" />
74+
</Grid.ColumnDefinitions>
75+
<hc:TextBox hc:InfoElement.TitlePlacement="Left"
76+
hc:InfoElement.Title="{DynamicResource API_testSrcLang}"
77+
Name="TestSrcLang"
78+
hc:InfoElement.ShowClearButton="True"
79+
Text="ja"
80+
TextAlignment="Center" />
81+
<hc:TextBox Grid.Column="2"
82+
hc:InfoElement.TitlePlacement="Left"
83+
hc:InfoElement.Title="{DynamicResource API_testDstLang}"
84+
Name="TestDstLang"
85+
hc:InfoElement.ShowClearButton="True"
86+
Text="zh"
87+
TextAlignment="Center" />
88+
<Button Grid.Column="3"
89+
Content="{DynamicResource API_testTransBtn}"
90+
Margin="10,0,0,0"
91+
Width="122"
92+
Click="TransTestBtn_Click"></Button>
93+
</Grid>
94+
<StackPanel Margin="0,10,0,0"
95+
Orientation="Horizontal"
96+
HorizontalAlignment="Right">
97+
<Button Content="{DynamicResource API_applyBtn}"
98+
Width="122"
99+
Click="ApplyBtn_Click"
100+
HorizontalAlignment="Left" />
101+
<Button Content="{DynamicResource API_docBtn}"
102+
Margin="10,0,0,0"
103+
Width="225"
104+
Click="DocBtn_Click"
105+
HorizontalAlignment="Left"></Button>
106+
<Button Content="{DynamicResource API_billBtn}"
107+
Margin="10,0,0,0"
108+
Width="122"
109+
Click="BillBtn_Click"
110+
HorizontalAlignment="Right"></Button>
55111
</StackPanel>
56112
</StackPanel>
57113
</StackPanel>
58114
</hc:ScrollViewer>
59-
<StackPanel hc:Growl.GrowlParent="True" HorizontalAlignment="Right" Margin="15"/>
115+
<StackPanel hc:Growl.GrowlParent="True"
116+
HorizontalAlignment="Right"
117+
Margin="15" />
60118

61119
</Grid>
62120
</Page>

Mikoto/SettingsPages/TranslatorPages/ChatGPTTransSettingsPage.xaml.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ public ChatGPTTransSettingsPage()
1616
InitializeComponent();
1717
ChatGPTTransSecretKeyBox.Text = Common.AppSettings.ChatGPTapiKey;
1818
ChatGPTTransUrlBox.Text = Common.AppSettings.ChatGPTapiUrl;
19+
ChatGPTModelBox.Text = Common.AppSettings.ChatGPTapiModel;
1920
}
2021

2122
private async void AuthTestBtn_Click(object sender, RoutedEventArgs e)
2223
{
2324
Common.AppSettings.ChatGPTapiKey = ChatGPTTransSecretKeyBox.Text;
2425
Common.AppSettings.ChatGPTapiUrl = ChatGPTTransUrlBox.Text;
26+
Common.AppSettings.ChatGPTapiModel = ChatGPTModelBox.Text;
2527

26-
ITranslator chatGPTTrans = ChatGPTTranslator.TranslatorInit(ChatGPTTransSecretKeyBox.Text, ChatGPTTransUrlBox.Text);
28+
ITranslator chatGPTTrans = ChatGPTTranslator.TranslatorInit(ChatGPTTransSecretKeyBox.Text, ChatGPTTransUrlBox.Text, ChatGPTModelBox.Text);
2729

2830
if (await chatGPTTrans.TranslateAsync("apple", "zh", "en") != null)
2931
{
3032
HandyControl.Controls.Growl.Success($"ChatGPT {Application.Current.Resources["APITest_Success_Hint"]}");
3133
}
3234
else
3335
{
34-
HandyControl.Controls.Growl.Error($"ChatGPT {Application.Current.Resources["APITest_Error_Hint"]}\n{chatGPTTrans.GetLastError()}");
36+
HandyControl.Controls.Growl.Error($"ChatGPT {Application.Current.Resources["APITest_Error_Hint"]}{Environment.NewLine}{chatGPTTrans.GetLastError()}");
3537
}
3638
}
3739

@@ -52,7 +54,7 @@ private void BillBtn_Click(object sender, RoutedEventArgs e)
5254

5355
private async void TransTestBtn_Click(object sender, RoutedEventArgs e)
5456
{
55-
ITranslator chatGPTTrans = ChatGPTTranslator.TranslatorInit(ChatGPTTransSecretKeyBox.Text, ChatGPTTransUrlBox.Text);
57+
ITranslator chatGPTTrans = ChatGPTTranslator.TranslatorInit(ChatGPTTransSecretKeyBox.Text, ChatGPTTransUrlBox.Text, ChatGPTModelBox.Text);
5658
string? res = await chatGPTTrans.TranslateAsync(TestSrcText.Text, TestDstLang.Text, TestSrcLang.Text);
5759

5860
if (res != null)

Mikoto/Translators/Implementations/ChatGPTTranslator.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ private ChatGPTTranslator() { }
1717
public static readonly string SIGN_UP_URL = "https://platform.openai.com";
1818
public static readonly string BILL_URL = "https://platform.openai.com/account/usage";
1919
public static readonly string DOCUMENT_URL = "https://platform.openai.com/docs/introduction/overview";
20-
private string openai_model = "gpt-3.5-turbo";
21-
20+
21+
private string? openai_model = "gpt-3.5-turbo";
2222
private string? apiKey; //ChatGPT翻译API的密钥
2323
private string? apiUrl; //ChatGPT翻译API的URL
2424
private string errorInfo = string.Empty; //错误信息
@@ -46,7 +46,8 @@ public string GetLastError()
4646
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
4747
try
4848
{
49-
retString = await (await hc.PostAsync(apiUrl, req)).Content.ReadAsStringAsync();
49+
HttpResponseMessage? httpResponseMessage = await hc.PostAsync(apiUrl, req);
50+
retString = await httpResponseMessage.Content.ReadAsStringAsync();
5051
}
5152
catch (HttpRequestException ex)
5253
{
@@ -97,10 +98,15 @@ public string GetLastError()
9798

9899
public static ITranslator TranslatorInit(params string[] param)
99100
{
101+
if (param.Length < 3)
102+
throw new ArgumentException("Expected 3 parameters: API Key, API URL, Model");
103+
104+
100105
ChatGPTTranslator chatGPTTranslator = new()
101106
{
102-
apiKey = param.First(),
103-
apiUrl = param.Last(),
107+
apiKey = param[0],
108+
apiUrl = param[1],
109+
openai_model = param[2],
104110
};
105111
return chatGPTTranslator;
106112
}

Mikoto/Translators/TranslatorCommon.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class TranslatorCommon
2626
nameof(KingsoftFastAITTranslator) => KingsoftFastAITTranslator.TranslatorInit(Common.AppSettings.KingsoftFastAITPath),
2727
nameof(DreyeTranslator) => DreyeTranslator.TranslatorInit(Common.AppSettings.DreyePath),
2828
nameof(DeepLTranslator) => DeepLTranslator.TranslatorInit(Common.AppSettings.DeepLsecretKey, Common.AppSettings.DeepLsecretKey),
29-
nameof(ChatGPTTranslator) => ChatGPTTranslator.TranslatorInit(Common.AppSettings.ChatGPTapiKey, Common.AppSettings.ChatGPTapiUrl),
29+
nameof(ChatGPTTranslator) => ChatGPTTranslator.TranslatorInit(Common.AppSettings.ChatGPTapiKey, Common.AppSettings.ChatGPTapiUrl, Common.AppSettings.ChatGPTapiModel),
3030
nameof(AzureTranslator) => AzureTranslator.TranslatorInit(Common.AppSettings.AzureSecretKey, Common.AppSettings.AzureLocation),
3131
nameof(ArtificialTranslator) => ArtificialTranslator.TranslatorInit(Common.AppSettings.ArtificialPatchPath),
3232
nameof(VolcanoTranslator) => VolcanoTranslator.TranslatorInit(Common.AppSettings.VolcanoId, Common.AppSettings.VolcanoKey),

Mikoto/lang/zh-CN.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<sys:String x:Key="ChatGPTTransSettingsPage_Introduce">ChatGPT人工智能翻译,可能拒绝翻译某些内容,可能并非完整翻译原文而略有增减;你需要先申请OpenAI的Api Key,获取密钥后填入下方相应位置并进行测试认证;如果认证出现错误,请根据错误码至OpenAI官方获取解决办法;ChatGPT API 的部分版本可能会产生某些费用,你可以使用额度查询按钮进入控制台确认。</sys:String>
9393
<sys:String x:Key="ChatGPTTransSettingsPage_secretKey">ChatGPT API 密钥</sys:String>
9494
<sys:String x:Key="ChatGPTTransSettingsPage_apiUrl">ChatGPT API URL</sys:String>
95+
<sys:String x:Key="ChatGPTTransSettingsPage_apiModel">AI模型</sys:String>
9596

9697
<sys:String x:Key="TencentOldTransSettingsPage_Introduce">腾讯翻译(私人)API支持多种语言互译,对日语、汉语的Galgame互译支持较好,需要联网使用;你需要先申请该API并创建一个密钥,获取SecretId和SecretKey后填入下方相应位置并进行测试认证;如果认证出现错误,请根据错误码至腾讯翻译API官方文档获取解决办法;腾讯旧版翻译API部分版本可能会产生某些费用,你可以使用额度查询按钮进入控制台确认。</sys:String>
9798
<sys:String x:Key="TencentOldTransSettingsPage_SecretId">腾讯云API SecretId</sys:String>

0 commit comments

Comments
 (0)