怎么调用chatGPT实现代码机器人

发布时间:2022-12-13 10:40:41 作者:iii
来源:亿速云 阅读:348

这篇文章主要介绍“怎么调用chatGPT实现代码机器人”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么调用chatGPT实现代码机器人”文章能帮助大家解决问题。

获取chatGPT登录Token信息

1.需要拥有chatGPT账号,进入官网需要科学上网自行解决

2.在登录时打开f12,抓取关键包

怎么调用chatGPT实现代码机器人

 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("\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);
    }
}

二、通过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("\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);
    }
}

关于“怎么调用chatGPT实现代码机器人”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. mongodb之备份和恢复介绍
  2. 如何使用echo输出颜色

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

chatgpt

上一篇:JS中call()、apply()和bind()函数怎么使用

下一篇:javascript ES6中set集合、map集合如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》