| using Newtonsoft.Json; |
| using Newtonsoft.Json.Linq; |
| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Linq; |
| using System.Net.Http; |
| using System.Reflection.Emit; |
| using System.Runtime.InteropServices; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Windows; |
| using System.Windows.Controls; |
| using Tea; |
| using Tea.Utils; |
| using static System.Runtime.InteropServices.JavaScript.JSType; |
| using Formatting = Newtonsoft.Json.Formatting; |
|
|
| namespace WpfEditor |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| public static class DeepSeek |
| { |
| private static readonly HttpClient httpClient = new HttpClient(); |
|
|
|
|
| public static async Task<(JObject? result, object error)> SendRequestAsync(string api, string key, string ja) |
| { |
| try |
| { |
| |
| httpClient.Timeout = TimeSpan.FromHours(1); |
|
|
| JArray arr = new JArray(); |
| arr.Add(new JObject { ["role"] = "system", ["content"] = "You are a helpful assistant." }); |
| arr.Add(new JObject { ["role"] = "user", ["content"] = $"翻译成中文:{ja}" }); |
|
|
| |
| var requestBody = new |
| { |
| model = "deepseek-chat", |
| messages = arr, |
| stream = false |
| }; |
|
|
| var jsonContent = JsonConvert.SerializeObject(requestBody); |
| var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); |
|
|
| |
| var request = new HttpRequestMessage(HttpMethod.Post, api); |
| request.Headers.Add("Authorization", $"Bearer {key}"); |
| request.Content = content; |
|
|
| |
| var response = await httpClient.SendAsync(request); |
|
|
| |
| if (response.StatusCode != System.Net.HttpStatusCode.OK) |
| { |
| var errorBody = await response.Content.ReadAsStringAsync(); |
| return (null, new { error_code = (int)response.StatusCode, error_msg = errorBody }); |
| } |
|
|
| |
| var responseBody = await response.Content.ReadAsStringAsync(); |
|
|
| try |
| { |
| var result = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(responseBody); |
| return (result, null); |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine(ex.Message); |
| Console.WriteLine((int)response.StatusCode); |
| Console.WriteLine(responseBody); |
| return (null, ex); |
| } |
| } |
| catch (Exception error) |
| { |
| Console.WriteLine("#####ERROR: aliyun ocr fail."); |
| Console.WriteLine(error.Message); |
| return (null, error); |
| } |
| } |
|
|
|
|
| public static async Task<string> translate(string ja) |
| { |
| string pth_config = Path.Combine(Constant.ExecutablePath, "config.json"); |
| if (!System.IO.File.Exists(pth_config)) |
| { |
| throw new Exception("config not found."); |
| } |
|
|
| string cf = File.ReadAllText(pth_config); |
| var cfg = JObject.Parse(cf); |
| string api = cfg["deepSeek"]["api"].Value<string>(); |
| string key = cfg["deepSeek"]["key"].Value<string>(); |
|
|
| var ( result, error) = await SendRequestAsync(api, key, ja); |
|
|
| if (result != null) |
| { |
| return result.ToString(Formatting.Indented); |
| } |
|
|
| return ""; |
| } |
|
|
| } |
|
|
| } |
|
|