Skip to content

Commit

Permalink
perf: OpenAI supports custom models.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jan 18, 2024
1 parent 9ac9420 commit a9355c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions STranslate/ViewModels/Preference/Services/TranslatorOpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public TranslatorOpenAI(
[ObservableProperty]
public string _appKey = string.Empty;

[JsonIgnore]
[ObservableProperty]
private string _model = "gpt-3.5-turbo";

[JsonIgnore]
public object _data = string.Empty;

Expand Down
9 changes: 6 additions & 3 deletions STranslate/ViewModels/ServiceHandler.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using STranslate.Model;
using STranslate.Util;
using STranslate.ViewModels.Preference.Services;

namespace STranslate.ViewModels
{
Expand Down Expand Up @@ -121,10 +121,13 @@ public static async Task OpenAIHandlerAsync(ITranslator service, string content,
uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/v1/completions";
}

var a_model = (service as TranslatorOpenAI)?.Model;
a_model = string.IsNullOrEmpty(a_model) ? "gpt-3.5-turbo" : a_model;

// 构建请求数据
var reqData = new
{
model = "gpt-3.5-turbo",
model = a_model,
messages = new[] { new { role = "user", content = $"Translate the following text to {target}: {content}" } },
temperature = 1.0,
stream = true
Expand Down Expand Up @@ -232,7 +235,7 @@ public static async Task GeminiHandlerAsync(ITranslator service, string content,
Content = new StringContent(jsonData, Encoding.UTF8, "application/json")
};

// 发送请求
// 发送请求
using var response = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, token);
// 获取响应流
using var responseStream = await response.Content.ReadAsStreamAsync(token);
Expand Down
16 changes: 15 additions & 1 deletion STranslate/Views/Preference/Service/TextOpenAIServicesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
</Grid>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="模型: " />

<common:PlaceholderTextBox Grid.Column="1"
MinWidth="206"
HorizontalAlignment="Left"
Placeholder="gpt-3.5-turbo"
Text="{Binding Model, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</Grid>

<Grid Margin="0,20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
Expand Down Expand Up @@ -88,7 +102,7 @@
SelectedValue="{Binding Icon}" />
</Grid>

<Grid Margin="0,20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
Expand Down

0 comments on commit a9355c1

Please sign in to comment.