准备工作
第一步:注册OpenAI账号,如果搭建了科学还是提示不对你的国家提供服务的话,尝试清空浏览器缓存或者打开浏览器的无痕窗口。Chrome默认在右上角三个点打开就能找到“打开新的无痕式窗口”。
ChatGPT聊天库代码示例
我们需要三个东西:UserAgent,CfClearance,Session_token
我们需要先打开ChatGPT官方网站:https://chat.openai.com/chat 然后按下F12打开浏览器的开发者模式
UserAgent在网络里(只需要复制UserAgent:后面的值):
CfClearance和Session_token在应用程序->cookie里面
2.创建一个session用来表示一个会话
public OpenAISession(string session_token,string cfClearance,string userAgent) { Session_token = session_token; CfClearance = cfClearance; UserAgent = userAgent; Headers = new Dictionary<string, string>(); Cookies = new Dictionary<string, string>(); Proxies = new Dictionary<string, string>(); } Session可以刷新自己的AccessToken和Session_token public async Task RefreshSessionAsync() { if (string.IsNullOrEmpty(Session_token)) { throw new Exception("No tokens provided"); } // Set cookies Cookies.Put("__Secure-next-auth.session-token", Session_token); Cookies.Put("cf_clearance", CfClearance); string cookiesString = GetCookiesString(); Dictionary<string, string> map = new Dictionary<string, string>(); Headers.Put("User-Agent", UserAgent); Headers.Put("cookie", cookiesString); Headers.Put("Cookie", cookiesString); var response = await GetAsync("https://chat.openai.com/api/auth/session"); if (!response.IsSuccessStatusCode) { Console.WriteLine("err code: " + response.StatusCode); Console.WriteLine("cf_clearance: " + CfClearance); Console.WriteLine("token: " + Session_token); Console.WriteLine("userAgent: " + UserAgent); Console.WriteLine("请检查以上参数是否正确,是否过期。"); throw new Exception("无法获取token!请重试"); } try { string name = "__Secure-next-auth.session-token="; var cookies = response.Headers.GetValues("Set-Cookie"); var stoken = cookies.FirstOrDefault(x => x.StartsWith(name)); Session_token = stoken == null ? Session_token : stoken.Substring(name.Length, stoken.IndexOf(";") - name.Length); Cookies.Put("__Secure-next-auth.session-token", Session_token); var result = await response.Content.ReadAsStringAsync(); AccessToken = JsonSerializer.Deserialize<Profile>(result, _jsonSerializerOptions)?.AccessToken; RefreshHeaders(); } catch (Exception ex) { Console.WriteLine($"Error {nameof(RefreshSessionAsync)}:{ex}"); throw new Exception($"Error {nameof(RefreshSessionAsync)}", ex); } }
获取到的最新的AccessToken更新到header里,Session_token更新到cookie里
private void RefreshHeaders() { Headers.Put("Host", "chat.openai.com"); Headers.Put("Accept", "text/event-stream"); Headers.Put("Authorization", $"Bearer {AccessToken}"); Headers.Put("User-Agent", UserAgent); Headers.Put("X-Openai-Assistant-App-Id", string.Empty); Headers.Put("Connection", "close"); Headers.Put("Accept-Language", "en-US,en;q=0.9"); Headers.Put("Referer", "https://chat.openai.com/chat"); } string name = "__Secure-next-auth.session-token="; var cookies = response.Headers.GetValues("Set-Cookie"); var stoken = cookies.FirstOrDefault(x => x.StartsWith(name)); Session_token = stoken == null ? Session_token : stoken.Substring(name.Length, stoken.IndexOf(";") - name.Length); Cookies.Put("__Secure-next-auth.session-token", Session_token);
3.创建机器人绑定一个会话
public Chatbot(OpenAISession openAISession) { OpenAISession = openAISession; ResetConversation(); } /// <summary> /// 重置Conversation,开启一个新的会话 /// </summary> public void ResetConversation() { _conversationId = null; _parentMessageId = Guid.NewGuid().ToString(); }
如何保持上下文聊天以及多用户隔离?
上下文聊天已经内置,会自动与机器人的回复进行关联,当需要重新开启一个对话的时候,可以调用ResetConversation,这样对应的机器人对象发过去就是开启了一个新的对话。
如何多用户隔离?比如联合微信机器人使用,可以将一个人或者一个群对应一个Chatbot对象,并且利用id与机器人做关联。
当然这个_clientID需要使用者做唯一性管理
//可以作为隔离不同客户端聊天上下文的凭据,即一个机器人绑定一个客户端 //客户端编号可以是,微信id,qq id,飞书id,亦或者自己开发的软件的用户id private readonly string _clientID; public Chatbot(OpenAISession openAISession, string clientID) { OpenAISession = openAISession; _clientID = clientID == null ? Guid.NewGuid().ToString() : clientID; ResetConversation(); }
最后构造对话对象,发送到对应api
public async Task<Reply> GetChatReplyAsync(string prompt) { var conversation = new Conversation(); conversation.Conversation_id = _conversationId; conversation.Parent_message_id = _parentMessageId; conversation.Messages = new Message[] { new Message() { Content = new Content { Parts = new string []{ prompt } } } }; return await GetChatResponseAsync(conversation); } /// <summary> /// 获取响应 /// </summary> /// <param name="conversation"></param> /// <returns></returns> /// <exception cref="Exception">服务器返回非200</exception> private async Task<Reply> GetChatResponseAsync(Conversation conversation) { using (var client = new HttpClient()) { var response = await OpenAISession.PostAsync(_conversation, JsonSerializer.Serialize(conversation, _jsonSerializerOptions)); if (!response.IsSuccessStatusCode) { throw new Exception($"Faild to request.StatusCode:{response.StatusCode}"); } var msg = await response.Content.ReadAsStringAsync(); var data = msg.Split("\n")?.ToList().Where(x => !string.IsNullOrEmpty(x) && !x.Contains("data: [DONE]")).LastOrDefault()?.Substring(5); var reply = JsonSerializer.Deserialize<Reply>(data, _jsonSerializerOptions); _conversationId = reply.Conversation_id; return reply; } }
ChatGPT
2023-02-13 219ChatGPT是一款最近非常火的人工智能系统,我的站长站为了体验一下ChatGPT的强大,在网络上收集了许多关于ChatGPT的信息,分享给大家.包含大家最关系的ChatGPT注册方...
- 免费ChatGPT4帐号分享网站 [2024-07-04]
- chatgpt连续对话本地搭建源码 [2024-02-07]
- ChatGPT+AI实战智能虚拟数字人项目教程 [2024-01-26]
- nodejs框架Nine AI ChatGPT系统网站源码2.4.2 [2024-01-16]
- GPT4All Chat一建本地部署Chatgpt [2023-12-31]