using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using System.Web.Services; using Newtonsoft.Json; namespace TradeManageNew.chatgpt { /// /// ChatGptService 的摘要说明 /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService] public class ChatGptService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } #region 多条件普通查询 [WebMethod(EnableSession = true)] public string GetGptMess(string prompt) { try { string js = "{\"prompt\":\"" + prompt + "\",\"max_tokens\":1028, \"temperature\": 1,\"model\":\"text-davinci-003\"}"; byte[] data = System.Text.Encoding.UTF8.GetBytes(js); string mess = ""; List head = new List(); head.Add("Authorization:Bearer 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"); string content = HttpRequest("https://api.openai.com/v1/completions", "Post", "application/json", head, null, data, out mess); if (mess != "") return mess; GptRoot model = JsonConvert.DeserializeObject(content); string remess = ""; if (model != null && model.choices != null) { foreach (var md in model.choices) { remess += md.text;//.Replace("\n", "
"); } } return remess; } catch (Exception ex) { return ex.Message; } } #endregion public static string HttpRequest(string url, string Method, string ContentType, List ListHeader, Version ver, byte[] bytes, out string ErrorMessage) { try { ErrorMessage = ""; //System.Net.ServicePointManager.DefaultConnectionLimit =200; //设置支持的ssl协议版本,这里我们都勾选上常用的几个 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; if (string.IsNullOrEmpty(ContentType) == true) ContentType = "application/x-www-form-urlencoded"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = Method; //GET,POST myRequest.ContentType = ContentType; myRequest.KeepAlive = true; //myRequest.Accept = "text/html"; //myRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)"; myRequest.Headers.Add("Accept-Language: zh-CN"); myRequest.Headers["Pragma"] = "no-cache"; //禁用缓存 myRequest.Headers["Cache-Control"] = "no-cache"; //禁用缓存 //myRequest.Timeout = 3 * 60 * 1000; myRequest.Timeout = 300000; myRequest.ReadWriteTimeout = 3 * 60 * 1000; if (ver != null) myRequest.ProtocolVersion = ver; if (ListHeader != null) { foreach (var item in ListHeader) { myRequest.Headers.Add(item); } } if (Method.ToUpper() == "POST" && (bytes == null || bytes.Length == 0)) { bytes = new byte[1]; } if (bytes != null && bytes.Length > 0) { //myRequest.GetRequestStream().Write(bytes, 0, bytes.Length); Stream stream = myRequest.GetRequestStream(); stream.Write(bytes, 0, bytes.Length); stream.Close(); } //获得接口返回值 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); Stream myResponseStream = myResponse.GetResponseStream(); StreamReader reader = new StreamReader(myResponseStream, Encoding.UTF8); string content = reader.ReadToEnd(); reader.Close(); myResponseStream.Close(); myRequest.Abort(); myResponse.Close(); reader = null; myResponseStream = null; myRequest = null; myResponse = null; return content; } catch (WebException e) { if (e.Response == null) { ErrorMessage = e.Message; } else { Stream myResponseStream = e.Response.GetResponseStream(); StreamReader reader = new StreamReader(myResponseStream, Encoding.UTF8); ErrorMessage = reader.ReadToEnd(); if (ErrorMessage == "") ErrorMessage = e.Message; } } return ""; } } //如果好用,请收藏地址,帮忙分享。 public class ChoicesItem { /// /// /// public string text { get; set; } /// /// /// public int index { get; set; } /// /// /// public string logprobs { get; set; } /// /// /// public string finish_reason { get; set; } } public class Usage { /// /// /// public int prompt_tokens { get; set; } /// /// /// public int completion_tokens { get; set; } /// /// /// public int total_tokens { get; set; } } public class GptRoot { /// /// /// public string id { get; set; } /// /// /// public int created { get; set; } /// /// /// public string model { get; set; } /// /// /// public List choices { get; set; } /// /// /// public Usage usage { get; set; } } }