Skip to content

Commit

Permalink
perf: Optimize AI translation effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jan 22, 2024
1 parent 5c76030 commit 0488326
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions STranslate/ViewModels/ServiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,23 @@ public static async Task OpenAIHandlerAsync(ITranslator service, string content,
// 兼容旧版API: https://platform.openai.com/docs/guides/text-generation
if (!uriBuilder.Path.EndsWith("/v1/chat/completions") && !uriBuilder.Path.EndsWith("/v1/completions"))
{
uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/v1/chat/completions";
uriBuilder.Path = "/v1/chat/completions";
}

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

// 组织语言
var a_content = source.Equals("auto", StringComparison.CurrentCultureIgnoreCase)
? $"Translate the following text to {target}: {content}"
: $"Translate the following text from {source} to {target}: {content}";

// 构建请求数据
var reqData = new
{
model = a_model,
messages = new[] { new { role = "user", content = $"Translate the following text to {target}: {content}" } },
messages = new[] { new { role = "user", content = a_content } },
temperature = 1.0,
stream = true
};
Expand All @@ -138,7 +144,7 @@ public static async Task OpenAIHandlerAsync(ITranslator service, string content,
var jsonData = JsonConvert.SerializeObject(reqData);

// 构建请求
var client = new HttpClient(new SocketsHttpHandler());
var client = new HttpClient(new SocketsHttpHandler()) { Timeout = TimeSpan.FromSeconds(10) };
var req = new HttpRequestMessage
{
Method = HttpMethod.Post,
Expand Down Expand Up @@ -216,19 +222,24 @@ public static async Task GeminiHandlerAsync(ITranslator service, string content,

if (!uriBuilder.Path.EndsWith("/v1beta/models/gemini-pro:streamGenerateContent"))
{
uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/v1beta/models/gemini-pro:streamGenerateContent";
uriBuilder.Path = "/v1beta/models/gemini-pro:streamGenerateContent";
}

uriBuilder.Query = $"key={service.AppKey}";

// 组织语言
var a_content = source.Equals("auto", StringComparison.CurrentCultureIgnoreCase)
? $"Translate the following text to {target}: {content}"
: $"Translate the following text from {source} to {target}: {content}";

// 构建请求数据
var reqData = new { contents = new[] { new { parts = new[] { new { text = $"Translate the following text to {target}: {content}" } } } } };
var reqData = new { contents = new[] { new { parts = new[] { new { text = a_content } } } } };

// 为了流式输出与MVVM还是放这里吧
var jsonData = JsonConvert.SerializeObject(reqData);

// 构建请求
var client = new HttpClient(new SocketsHttpHandler());
var client = new HttpClient(new SocketsHttpHandler()) { Timeout = TimeSpan.FromSeconds(10) };
var req = new HttpRequestMessage
{
Method = HttpMethod.Post,
Expand Down

0 comments on commit 0488326

Please sign in to comment.