imradv3 / src /WpfEditor /DeepSeek.cs
fasdfsa's picture
deepseek post
670b81d
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
{
/*
string res = await DeepSeek.translate("担当の先輩は、先生にこっぴどく怒られてたけどね");
if (res != "")
{
JObject jsn = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(res);
string zh = jsn["choices"][0]["message"]["content"].Value<string>();
int a = 1;
}
*/
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
{
// 设置超时时间为1小时
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 "";
}
}
}