如何調(diào)用chatGPT實(shí)現(xiàn)代碼機(jī)器人
最近c(diǎn)hatGPT也是非常的火爆,相信大家都看到了,現(xiàn)在提供一種Java調(diào)用chatGPT的方法,我們主要通過(guò)兩個(gè)工具來(lái)實(shí)現(xiàn),一就是httpclient,二就是hutool,你覺得那種好理解你就用那種即可!
獲取chatGPT登錄Token信息
1.需要擁有chatGPT賬號(hào),進(jìn)入官網(wǎng)需要科學(xué)上網(wǎng)自行解決!
官網(wǎng):ChatGPT
注冊(cè)需使用國(guó)外手機(jī)號(hào)!
視頻教程:https://www.bilibili.com/video/BV1HP4y1Q7QP/
2.在登錄時(shí)打開f12,抓取關(guān)鍵包
3.需要注意的是此token只能存活一天,所以說(shuō)你要對(duì)接到QQ機(jī)器人或者是微信機(jī)器人的情況下,需要每天手動(dòng)獲取一次token,需要對(duì)接QQ機(jī)器人可以看我往期的博客,只需要將以下兩種方式的任意一種封裝成方法進(jìn)行調(diào)用即可!
基于SpringBoot實(shí)現(xiàn)QQ機(jī)器人_木芒果呀的博客-CSDN博客_android_phone、android_pad、android_watch
一、通過(guò)Httpclient實(shí)現(xiàn)調(diào)用chatGPT
1.在項(xiàng)目中引入Httpclient依賴
<!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.7</version> </dependency>
2.測(cè)試類
/** * 使用httpclient調(diào)用chatGPT */ public class App { public static void main(String[] args) throws Exception { //通過(guò)登錄chatgpt截取頁(yè)面的token信息 String au = "token"; String sendMsg = "發(fā)送的消息"; String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42"; String json = "{\"action\":\"next\",\"messages\":[{\"id\":\"16f7cd15-5e7a-461b-8992-81a80ae770fa\",\"role\":\"user\",\"content\":{\"content_type\":\"text\",\"parts\":[\"" + sendMsg + "\"]}}],\"conversation_id\":\"add24670-9871-421a-bf23-e210b5021099\",\"parent_message_id\":\"c743b493-c2dc-454e-aacd-9ce90cb1ee97\",\"model\":\"text-davinci-002-render\"}"; System.out.println(json); HttpPost httpPost = new HttpPost("https://chat.openai.com/backend-api/conversation"); httpPost.addHeader("authorization", au); httpPost.addHeader("user-agent", user_agent); httpPost.setHeader("Content-type", "application/json; charset=utf-8"); httpPost.setEntity(new StringEntity(json, StandardCharsets.UTF_8)); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); String[] splitR = body.split("\n\n"); String replaceFirst = splitR[splitR.length - 2].replaceFirst("data: ", ""); String res = JSON.parseObject(replaceFirst).getJSONObject("message").getJSONObject("content").getJSONArray("parts").getString(0); System.out.println(res); } }
二、通過(guò)hutool實(shí)現(xiàn)調(diào)用chatGPT
1.在項(xiàng)目中引入依賴
<!-- hutool --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.10</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.7</version> </dependency>
2.測(cè)試類
/** * 使用hutool調(diào)用chatGPT */ public class App { public static void main(String[] args) throws Exception { //通過(guò)登錄chatgpt截取頁(yè)面的token信息 String au = "token"; String sendMsg = "發(fā)送的消息"; String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42"; String json = "{\"action\":\"next\",\"messages\":[{\"id\":\"adfe88e4-f92f-4f21-9145-e227068ce9ca\",\"role\":\"user\",\"content\":{\"content_type\":\"text\",\"parts\":[\""+sendMsg+"\"]}}],\"parent_message_id\":\"2a557b41-7e53-4fc4-a28f-cfb0c67b29a7\",\"model\":\"text-davinci-002-render\"}"; String body = HttpRequest.post("https://chat.openai.com/backend-api/conversation") .header("authorization", au) .header("user-agent", user_agent) .body(StrFormatter.format(json)).execute().body(); String[] splitR = body.split("\n\n"); String replaceFirst= splitR[splitR.length - 2].replaceFirst("data: ", ""); String res = JSON.parseObject(replaceFirst).getJSONObject("message").getJSONObject("content").getJSONArray("parts").getString(0); System.out.println(res); } }
到此這篇關(guān)于調(diào)用chatGPT實(shí)現(xiàn)代碼機(jī)器人的文章就介紹到這了,更多相關(guān)chatGPT代碼機(jī)器人內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java按時(shí)間梯度實(shí)現(xiàn)異步回調(diào)接口的方法
這篇文章主要介紹了Java按時(shí)間梯度實(shí)現(xiàn)異步回調(diào)接口,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Spring IOC和DI實(shí)現(xiàn)原理及實(shí)例解析
這篇文章主要介紹了Spring IOC和DI實(shí)現(xiàn)原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06java 遍歷request中的所有表單數(shù)據(jù)的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇java 遍歷request中的所有表單數(shù)據(jù)的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic()
本文主要介紹了Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic(),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06java實(shí)現(xiàn)刪除某條信息并刷新當(dāng)前頁(yè)操作
這篇文章主要介紹了java實(shí)現(xiàn)刪除某條信息并刷新當(dāng)前頁(yè)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11關(guān)于mybatis if else if 條件判斷SQL片段表達(dá)式取值和拼接問(wèn)題
這篇文章主要介紹了mybatis if else if 條件判斷SQL片段表達(dá)式取值和拼接,文章通過(guò)自己真實(shí)使用的例子給大家詳細(xì)介紹,需要的朋友可以參考下2021-09-09關(guān)于JAVA中stream流的基礎(chǔ)處理(獲取對(duì)象字段和對(duì)象批量處理等)
這篇文章主要介紹了關(guān)于JAVA中stream流的基礎(chǔ)處理,包含獲取對(duì)象字段、按字段排序、按字段去重、對(duì)象批量處理、指定字段轉(zhuǎn)數(shù)組等內(nèi)容,需要的朋友可以參考下2023-03-03Spring Date jpa 獲取最新一條數(shù)據(jù)的實(shí)例代碼
這篇文章主要介紹了Spring Date jpa 獲取最新一條數(shù)據(jù)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10