首頁 > 軟體

如何呼叫chatGPT實現程式碼機器人

2022-12-11 14:01:40

最近chatGPT也是非常的火爆,相信大家都看到了,現在提供一種Java呼叫chatGPT的方法,我們主要通過兩個工具來實現,一就是httpclient,二就是hutool,你覺得那種好理解你就用那種即可!

獲取chatGPT登入Token資訊

1.需要擁有chatGPT賬號,進入官網需要科學上網自行解決!

官網:ChatGPT

註冊需使用國外手機號!

視訊教學:https://www.bilibili.com/video/BV1HP4y1Q7QP/

2.在登入時開啟f12,抓取關鍵包

 3.需要注意的是此token只能存活一天,所以說你要對接到QQ機器人或者是微信機器人的情況下,需要每天手動獲取一次token,需要對接QQ機器人可以看我往期的部落格,只需要將以下兩種方式的任意一種封裝成方法進行呼叫即可!

基於SpringBoot實現QQ機器人_木芒果呀的部落格-CSDN部落格_android_phone、android_pad、android_watch

一、通過Httpclient實現呼叫chatGPT

1.在專案中引入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.測試類

/**
 * 使用httpclient呼叫chatGPT
 */
public class App {
    public static void main(String[] args) throws Exception {
        //通過登入chatgpt擷取頁面的token資訊
        String au = "token";
        String sendMsg = "傳送的訊息";
        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("nn");
        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);
    }
}

二、通過hutool實現呼叫chatGPT

1.在專案中引入依賴

        <!-- 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.測試類

/**
 * 使用hutool呼叫chatGPT
 */
public class App {
    public static void main(String[] args) throws Exception {
        //通過登入chatgpt擷取頁面的token資訊
        String au = "token";
        String sendMsg = "傳送的訊息";
        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("nn");
        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);
    }
}

到此這篇關於呼叫chatGPT實現程式碼機器人的文章就介紹到這了,更多相關chatGPT程式碼機器人內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com