ChatGTPAIHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using PDF_Master.Properties;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. namespace PDF_Master.Helper
  13. {
  14. public static class ChatGTPAIHelper
  15. {
  16. private static string host = "http://api-us-east-1.compdf.com:8082";
  17. private static string translate = "http://101.132.103.13:8030";
  18. /// <summary>
  19. /// 翻译文档Key接口
  20. /// </summary>
  21. private static string Uri_fileKeytranslate = translate + "/v1/translate/fileUpload";
  22. /// <summary>
  23. /// 翻译文档接口
  24. /// </summary>
  25. private static string Uri_filetranslate = translate + "/v1/translate/fileTranslateHandle";
  26. /// <summary>
  27. /// 翻译文本接口
  28. /// </summary>
  29. private static string Uri_texttranslate = translate + "/v1/translate/textTrans";
  30. ///// <summary>
  31. ///// FAQ接口
  32. ///// </summary>
  33. //private static string Uri_FAQ = host + "/find-faq";
  34. ///// <summary>
  35. ///// 跳转接口
  36. ///// </summary>
  37. //private static string Uri_GoToView = host + "/recognition";
  38. ///// <summary>
  39. ///// 摘要接口
  40. ///// </summary>
  41. //private static string Uri_Summary = host + "/summary";
  42. /// <summary>
  43. /// 错别字纠正
  44. /// </summary>
  45. private static string Uri_Correction = host + "/api/correct-typos";
  46. /// <summary>
  47. /// 错别字纠正
  48. /// </summary>
  49. private static string Uri_Rewrite = host + "/api/rewrite";
  50. /// <summary>
  51. /// 翻译指定内容,可以指定语言
  52. /// </summary>
  53. /// <param name="content"></param>
  54. /// <param name="language">1=中文 2=英文 3=法语 4=汉语</param>
  55. /// <returns></returns>
  56. public async static Task<string> fileKeyTranslate(string content, int language)
  57. {
  58. System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
  59. //需要翻译的内容
  60. namevalue["file"] = content;
  61. //1=中文 2=英文 3=法语 4=汉语
  62. namevalue["projectId"] = "2";
  63. namevalue["version"] = "1.0.1";
  64. if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
  65. {
  66. namevalue["userId"] = Settings.Default.UserDate.id;
  67. }
  68. return await PostString(Uri_fileKeytranslate, namevalue);
  69. }
  70. /// <summary>
  71. /// 翻译指定内容,可以指定语言
  72. /// </summary>
  73. /// <param name="fileKey">文本Key</param>
  74. /// <param name="fromlanguage">文本语言</param>
  75. /// <param name="tolanguage">需要翻译的语言</param>
  76. /// <returns></returns>
  77. public static String fileTranslate(string fileKey, string fromlanguage, string tolanguage)
  78. {
  79. HttpWebResponse response = null;
  80. ServicePointManager.DefaultConnectionLimit = 200;
  81. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
  82. request.Method = "Post";
  83. request.ContentType = "application/json";
  84. //request.Accept = "application/vnd.api+json;version=1";
  85. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  86. request.Timeout = 20000;
  87. request.ServicePoint.Expect100Continue = false;
  88. StringWriter sw = new StringWriter();
  89. using (JsonWriter writer = new JsonTextWriter(sw))
  90. {
  91. writer.WriteStartObject();
  92. writer.WritePropertyName("fileKey");
  93. writer.WriteValue(fileKey);
  94. writer.WritePropertyName("from");
  95. writer.WriteValue(fromlanguage);
  96. writer.WritePropertyName("to");
  97. writer.WriteValue(tolanguage);
  98. writer.WritePropertyName("projectId");
  99. writer.WriteValue("2");
  100. writer.WritePropertyName("version");
  101. writer.WriteValue("1.0.1");
  102. writer.WritePropertyName("userId");
  103. writer.WriteValue(Settings.Default.UserDate.id);
  104. writer.WriteEndObject();
  105. }
  106. try
  107. {
  108. string postBody = sw.ToString();
  109. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  110. {
  111. writer.Write(postBody);
  112. writer.Close();
  113. }
  114. response = (HttpWebResponse)request.GetResponse();
  115. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  116. {
  117. string responseData = reader.ReadToEnd();
  118. Console.WriteLine(responseData);
  119. reader.Close();
  120. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  121. if (response != null)
  122. {
  123. response.Close();
  124. }
  125. if (request != null)
  126. {
  127. request.Abort();
  128. }
  129. if (jobject["code"].ToObject<string>().ToLower() == "200")
  130. {
  131. }
  132. //return jobject["code"].ToObject<string>().ToLower();
  133. return "200";
  134. }
  135. }
  136. catch
  137. {
  138. return "300";
  139. }
  140. }
  141. /// <summary>
  142. /// 翻译指定内容,可以指定语言
  143. /// </summary>
  144. /// <param name="content">翻译内容</param>
  145. /// <param name="fromlanguage">文本语言</param>
  146. /// <param name="tolanguage">需要翻译的语言</param>
  147. /// <returns></returns>
  148. public static String textTranslate(string content, string fromlanguage, string tolanguage)
  149. {
  150. HttpWebResponse response = null;
  151. ServicePointManager.DefaultConnectionLimit = 200;
  152. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
  153. request.Method = "Post";
  154. request.ContentType = "application/json";
  155. //request.Accept = "application/vnd.api+json;version=1";
  156. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  157. request.Timeout = 20000;
  158. request.ServicePoint.Expect100Continue = false;
  159. StringWriter sw = new StringWriter();
  160. using (JsonWriter writer = new JsonTextWriter(sw))
  161. {
  162. writer.WriteStartObject();
  163. writer.WritePropertyName("q");
  164. writer.WriteValue(content);
  165. writer.WritePropertyName("from");
  166. writer.WriteValue(fromlanguage);
  167. writer.WritePropertyName("to");
  168. writer.WriteValue(tolanguage);
  169. writer.WritePropertyName("projectId");
  170. writer.WriteValue("2");
  171. writer.WritePropertyName("version");
  172. writer.WriteValue("1.0.1");
  173. writer.WritePropertyName("userId");
  174. writer.WriteValue(Settings.Default.UserDate.id);
  175. writer.WriteEndObject();
  176. }
  177. try
  178. {
  179. string postBody = sw.ToString();
  180. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  181. {
  182. writer.Write(postBody);
  183. writer.Close();
  184. }
  185. response = (HttpWebResponse)request.GetResponse();
  186. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  187. {
  188. string responseData = reader.ReadToEnd();
  189. Console.WriteLine(responseData);
  190. reader.Close();
  191. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  192. if (response != null)
  193. {
  194. response.Close();
  195. }
  196. if (request != null)
  197. {
  198. request.Abort();
  199. }
  200. if (jobject["code"].ToObject<string>().ToLower() == "200")
  201. {
  202. }
  203. //return jobject["code"].ToObject<string>().ToLower();
  204. return "200";
  205. }
  206. }
  207. catch
  208. {
  209. return "300";
  210. }
  211. }
  212. public async static Task<string> textTranslate(string content, int language)
  213. {
  214. System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
  215. //需要翻译的内容
  216. namevalue["q"] = content;
  217. //1=中文 2=英文 3=法语 4=汉语
  218. namevalue["from"] = "auto";
  219. namevalue["to"] = "en";
  220. namevalue["projectId"] = "2";
  221. namevalue["version"] = "1.0.1";
  222. if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
  223. {
  224. namevalue["userId"] = Settings.Default.UserDate.id;
  225. }
  226. return await PostString(Uri_filetranslate, namevalue);
  227. }
  228. /// <summary>
  229. /// 纠错
  230. /// </summary>
  231. /// <param name="content"></param>
  232. /// <returns></returns>
  233. public async static Task<string> Correction(string content)
  234. {
  235. System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
  236. //需要纠错的内容
  237. namevalue["project_id"] = "3";
  238. namevalue["version"] = "1.0.1";
  239. if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
  240. {
  241. namevalue["user_id"] = Settings.Default.UserDate.id;
  242. }
  243. namevalue["content"] = content;
  244. return await PostString(Uri_Correction, namevalue);
  245. }
  246. /// <summary>
  247. /// 重写
  248. /// </summary>
  249. /// <param name="content"></param>
  250. /// <returns></returns>
  251. public async static Task<string> Rewrite(string content)
  252. {
  253. System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
  254. //需要纠错的内容
  255. namevalue["project_id"] = "3";
  256. namevalue["version"] = "1.0.1";
  257. if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
  258. {
  259. namevalue["user_id"] = Settings.Default.UserDate.id;
  260. }
  261. namevalue["content"] = content;
  262. return await PostString(Uri_Rewrite, namevalue);
  263. }
  264. ///// <summary>
  265. ///// 页面跳转
  266. ///// </summary>
  267. ///// <param name="content"></param>
  268. ///// <returns></returns>
  269. //public async static Task<string> GoToView(string content)
  270. //{
  271. // System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
  272. // //需要翻译的内容
  273. // namevalue["str"] = content;
  274. // return await PostString(Uri_GoToView, namevalue, "flag");
  275. //}
  276. /// <summary>
  277. /// 从链接中返回存在的链接(仅返回第一个找到的链接)
  278. /// </summary>
  279. /// <param name="content"></param>
  280. /// <returns></returns>
  281. public static List<string> GetLinkFromString(string content)
  282. {
  283. if (string.IsNullOrEmpty(content))
  284. {
  285. return null;
  286. }
  287. // 创建正则表达式模式
  288. string pattern = @"(https?://[^\s]+)";
  289. // 获取所有匹配项
  290. MatchCollection matches = Regex.Matches(content, pattern);
  291. List<string> list = new List<string>();
  292. // 遍历并打印所有匹配项
  293. foreach (Match match in matches)
  294. {
  295. list.Add(match.Value);
  296. }
  297. return list;
  298. }
  299. /// <summary>
  300. /// 从接口获取信息,获取message对应value值
  301. /// </summary>
  302. /// <param name="url"></param>
  303. /// <param name="namevalue"></param>
  304. /// <returns></returns>
  305. private async static Task<string> PostString(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message")
  306. {
  307. string repsonseData = "";
  308. try
  309. {
  310. using (var client = new WebClient())
  311. {
  312. byte[] bytes = await client.UploadValuesTaskAsync(url, namevalue);
  313. //转换成字符串类型
  314. var json = Encoding.Default.GetString(bytes);
  315. //将Json格式字符串转换成键值对
  316. var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  317. string unicode = "";
  318. // 遍历字典对象输出键值对
  319. foreach (KeyValuePair<string, string> item in values)
  320. {
  321. if (item.Key == key)
  322. {
  323. unicode = item.Value;
  324. }
  325. }
  326. //将Unicode格式转换成String
  327. repsonseData = Regex.Unescape(unicode);
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. return null;
  333. }
  334. return repsonseData;
  335. }
  336. }
  337. }