fasdfsa commited on
Commit
670b81d
·
1 Parent(s): 263e434

deepseek post

Browse files
src/WpfEditor/DeepSeek.cs ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Newtonsoft.Json;
2
+ using Newtonsoft.Json.Linq;
3
+ using System;
4
+ using System.Collections;
5
+ using System.Collections.Generic;
6
+ using System.Collections.Generic;
7
+ using System.IO;
8
+ using System.Linq;
9
+ using System.Net.Http;
10
+ using System.Reflection.Emit;
11
+ using System.Runtime.InteropServices;
12
+ using System.Text;
13
+ using System.Threading.Tasks;
14
+ using System.Windows;
15
+ using System.Windows.Controls;
16
+ using Tea;
17
+ using Tea.Utils;
18
+ using static System.Runtime.InteropServices.JavaScript.JSType;
19
+ using Formatting = Newtonsoft.Json.Formatting;
20
+
21
+ namespace WpfEditor
22
+ {
23
+ /*
24
+
25
+ string res = await DeepSeek.translate("担当の先輩は、先生にこっぴどく怒られてたけどね");
26
+ if (res != "")
27
+ {
28
+ JObject jsn = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(res);
29
+ string zh = jsn["choices"][0]["message"]["content"].Value<string>();
30
+ int a = 1;
31
+ }
32
+
33
+ */
34
+
35
+ public static class DeepSeek
36
+ {
37
+ private static readonly HttpClient httpClient = new HttpClient();
38
+
39
+
40
+ public static async Task<(JObject? result, object error)> SendRequestAsync(string api, string key, string ja)
41
+ {
42
+ try
43
+ {
44
+ // 设置超时时间为1小时
45
+ httpClient.Timeout = TimeSpan.FromHours(1);
46
+
47
+ JArray arr = new JArray();
48
+ arr.Add(new JObject { ["role"] = "system", ["content"] = "You are a helpful assistant." });
49
+ arr.Add(new JObject { ["role"] = "user", ["content"] = $"翻译成中文:{ja}" });
50
+
51
+ // 构建请求体
52
+ var requestBody = new
53
+ {
54
+ model = "deepseek-chat",
55
+ messages = arr,
56
+ stream = false
57
+ };
58
+
59
+ var jsonContent = JsonConvert.SerializeObject(requestBody);
60
+ var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
61
+
62
+ // 设置请求头
63
+ var request = new HttpRequestMessage(HttpMethod.Post, api);
64
+ request.Headers.Add("Authorization", $"Bearer {key}");
65
+ request.Content = content;
66
+
67
+ // 发送请求
68
+ var response = await httpClient.SendAsync(request);
69
+
70
+ // 检查状态码
71
+ if (response.StatusCode != System.Net.HttpStatusCode.OK)
72
+ {
73
+ var errorBody = await response.Content.ReadAsStringAsync();
74
+ return (null, new { error_code = (int)response.StatusCode, error_msg = errorBody });
75
+ }
76
+
77
+ // 解析响应
78
+ var responseBody = await response.Content.ReadAsStringAsync();
79
+
80
+ try
81
+ {
82
+ var result = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(responseBody);
83
+ return (result, null);
84
+ }
85
+ catch (Exception ex)
86
+ {
87
+ Console.WriteLine(ex.Message);
88
+ Console.WriteLine((int)response.StatusCode);
89
+ Console.WriteLine(responseBody);
90
+ return (null, ex);
91
+ }
92
+ }
93
+ catch (Exception error)
94
+ {
95
+ Console.WriteLine("#####ERROR: aliyun ocr fail.");
96
+ Console.WriteLine(error.Message);
97
+ return (null, error);
98
+ }
99
+ }
100
+
101
+
102
+ public static async Task<string> translate(string ja)
103
+ {
104
+ string pth_config = Path.Combine(Constant.ExecutablePath, "config.json");
105
+ if (!System.IO.File.Exists(pth_config))
106
+ {
107
+ throw new Exception("config not found.");
108
+ }
109
+
110
+ string cf = File.ReadAllText(pth_config);
111
+ var cfg = JObject.Parse(cf);
112
+ string api = cfg["deepSeek"]["api"].Value<string>();
113
+ string key = cfg["deepSeek"]["key"].Value<string>();
114
+
115
+ var ( result, error) = await SendRequestAsync(api, key, ja);
116
+
117
+ if (result != null)
118
+ {
119
+ return result.ToString(Formatting.Indented);
120
+ }
121
+
122
+ return "";
123
+ }
124
+
125
+ }
126
+
127
+ }
src/WpfEditor/TreeMenuPage.xaml CHANGED
@@ -4,7 +4,7 @@
4
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
  mc:Ignorable="d"
7
- Title="树形菜单页面" Height="600" Width="800">
8
  <Grid>
9
  <!-- 主容器 -->
10
  <Grid.ColumnDefinitions>
 
4
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
  mc:Ignorable="d"
7
+ Title="EchoDict" Height="600" Width="800">
8
  <Grid>
9
  <!-- 主容器 -->
10
  <Grid.ColumnDefinitions>
src/WpfEditor/TreeMenuPage.xaml.cs CHANGED
@@ -1,12 +1,13 @@
 
1
  using ScreenGrab;
 
2
  using System.IO;
3
  using System.Windows;
4
  using System.Windows.Controls;
 
5
  using System.Windows.Input;
6
  using System.Windows.Media;
7
  using System.Windows.Media.Animation;
8
- using System.Windows.Forms;
9
- using System.Drawing;
10
  using Bitmap = System.Drawing.Bitmap;
11
  using MessageBox = System.Windows.MessageBox;
12
  using Point = System.Windows.Point;
 
1
+ using Newtonsoft.Json.Linq;
2
  using ScreenGrab;
3
+ using System.Drawing;
4
  using System.IO;
5
  using System.Windows;
6
  using System.Windows.Controls;
7
+ using System.Windows.Forms;
8
  using System.Windows.Input;
9
  using System.Windows.Media;
10
  using System.Windows.Media.Animation;
 
 
11
  using Bitmap = System.Drawing.Bitmap;
12
  using MessageBox = System.Windows.MessageBox;
13
  using Point = System.Windows.Point;
src/WpfEditor/WpfEditor.csproj CHANGED
@@ -53,6 +53,9 @@
53
  </ItemGroup>
54
 
55
  <ItemGroup>
 
 
 
56
  <None Update="ico.ico">
57
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58
  </None>
 
53
  </ItemGroup>
54
 
55
  <ItemGroup>
56
+ <None Update="config.json">
57
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58
+ </None>
59
  <None Update="ico.ico">
60
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
61
  </None>
src/WpfEditor/config_default.json CHANGED
@@ -9,7 +9,14 @@
9
  "appKey": "",
10
  "appCode": "",
11
  "appSecret": ""
12
- }
 
 
 
 
 
 
 
13
  }
14
 
15
 
 
9
  "appKey": "",
10
  "appCode": "",
11
  "appSecret": ""
12
+ },
13
+ "deepSeek": {
14
+ "api": "https://api.deepseek.com/chat/completions",
15
+ "key": ""
16
+ },
17
+ "comicOCR": {
18
+ "api": "http://127.0.0.1:2393/comicocr"
19
+ }
20
  }
21
 
22