首页 > 其他分享 >GPT自己找解决方案

GPT自己找解决方案

时间:2024-01-31 17:47:22浏览次数:26  
标签:String 解决方案 ChatGptMessage 自己 new GPT import message public

GPT自己找解决方案

 

 

前言

今天突然突发奇想,就想要用java来调用chatget的接口,实现自己的聊天机器人,但是网上找文章,属实是少的可怜(可能是不让发吧)。找到了一些文章,但是基本都是通过调用别人的库来完成的,导入其他的jar还有不低的学习成本,于是就自己使用HttpClient5写了一个,在这里讲解一下思路。

导包

对于http调用,我使用的是比较流行的httpclient5,然后直接创建了一个springboot项目,方便以后对外提供接口。

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.3</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.2.1</version>
        </dependency>

    </dependencies>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

基本说明

在编写代码之前,这个先给出HttpClient的Api文档 api文档

在这里插入图片描述

我们在编写代码之前需要了解官方提供的接口如何进行访问以及返回的结果是什么

请求参数

官方文档地址为 文档,请求参数必须填写的内容如下

{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}
 
  • 1
  • 2
  • 3
  • 4

一个是model,一个是messages。model根据自己的情况来选择,聊天的话就是gpt-3.5-turbo,下面的messages里面包含n个对象,每个对象有role和content,role表示角色,content表示内容。
下面为官方文档中的解释

在这里插入图片描述

简单理解就是我们要问问题,role就是user。如果要实现连续对话,那么就将返回的返回内容设置到messages中,role设置为返回的role。

响应参数

下面直接给出响应的内容

{
 'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
 'object': 'chat.completion',
 'created': 1677649420,
 'model': 'gpt-3.5-turbo',
 'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
 'choices': [
   {
    'message': {
      'role': 'assistant',
      'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
    'finish_reason': 'stop',
    'index': 0
   }
  ]
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

我们问问题的答案就在choices.message下的content中,而role就代表了chatGpt扮演的角色。看到这我们就应该知道该干嘛了吧肯定是创建对应的VO类啊。

创建请求和响应的VO类

下面5个类就对应了我们发送和接收的各种信息

ChatGptMessage类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptMessage {
        String role;
        String content;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

ChatGptRequestParameter 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptRequestParameter {

    String model = "gpt-3.5-turbo";

    List<ChatGptMessage> messages = new ArrayList<>();

    public void addMessages(ChatGptMessage message) {
        this.messages.add(message);
    }

}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

ChatGptResponseParameter 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatGptResponseParameter {

    String id;
    String object;
    String created;
    String model;
    Usage usage;
    List<Choices> choices;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Choices 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Choices {

    ChatGptMessage message;
    String finish_reason;
    Integer index;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Usage 类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Usage {

    String prompt_tokens;
    String completion_tokens;
    String total_tokens;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

代码编写

不说废话,首先创建一个CustomChatGpt类

public class CustomChatGpt {

}
 
  • 1
  • 2
  • 3

然后定义一些成员属性

    /**
     * 自己chatGpt的ApiKey
     */
    private String apiKey;
    /**
     * 使用的模型
     */
    private String model = "gpt-3.5-turbo-0301";
    /**
     * 对应的请求接口
     */
    private String url = "https://api.openai.com/v1/chat/completions";
    /**
     * 默认编码
     */
    private Charset charset = StandardCharsets.UTF_8;
        /**
     * 创建一个ChatGptRequestParameter,用于携带请求参数
     */
    private ChatGptRequestParameter chatGptRequestParameter = new ChatGptRequestParameter();
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

提供一个ApiKey的构造器,创建该对象必须要传入ApiKey

    public CustomChatGpt(String apiKey) {
        this.apiKey = apiKey;
    }
 
  • 1
  • 2
  • 3

定义一个响应超时时间

    /**
     * 响应超时时间,毫秒
     */
    private int responseTimeout = 10000;

    public void setResponseTimeout(int responseTimeout) {
        this.responseTimeout = responseTimeout;
    }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

编写一个getAnswer方法,要求传入一个CloseableHttpClient和一个问题

    public String getAnswer(CloseableHttpClient client, String question) {
    
    }
 
  • 1
  • 2
  • 3

继续实现方法,下面会完成一些参数的创建和设置

        // 创建一个HttpPost
        HttpPost httpPost = new HttpPost(url);
        // 创建一个ObjectMapper,用于解析和创建json
        ObjectMapper objectMapper = new ObjectMapper();
        // 设置请求参数
        chatGptRequestParameter.addMessages(new ChatGptMessage("user", question));
        HttpEntity httpEntity = null;
        try {
            // 对象转换为json字符串
            httpEntity = new StringEntity(objectMapper.writeValueAsString(chatGptRequestParameter), charset);
        } catch (JsonProcessingException e) {
            System.out.println(question + "->json转换异常");
            return null;
        }
        httpPost.setEntity(httpEntity);
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

下面会完成一些配置的设置

        // 设置请求头
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // 设置登录凭证
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);

        // 用于设置超时时间
        RequestConfig config = RequestConfig
                .custom()
                .setResponseTimeout(responseTimeout, TimeUnit.MILLISECONDS)
                .build();
        httpPost.setConfig(config);
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

下面代码会提交请求,解析响应,最后返回对应问题的答案

        try {
            // 提交请求
            return client.execute(httpPost, response -> {
                // 得到返回的内容
                String resStr = EntityUtils.toString(response.getEntity(), charset);
                // 转换为对象
                ChatGptResponseParameter responseParameter = objectMapper.readValue(resStr, ChatGptResponseParameter.class);
                String ans = "";
                // 遍历所有的Choices(一般都只有一个)
                for (Choices choice : responseParameter.getChoices()) {
                    ChatGptMessage message = choice.getMessage();
                    chatGptRequestParameter.addMessages(new ChatGptMessage(message.getRole(), message.getContent()));
                    String s = message.getContent().replaceAll("\n+", "\n");
                    ans += s;
                }
                // 返回信息
                return ans;
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 发生异常,移除刚刚添加的ChatGptMessage
        chatGptRequestParameter.getMessages().remove(chatGptRequestParameter.getMessages().size()-1);
        return "您当前的网络无法访问";
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

下面给出这个类的完整代码

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttpfx.vo.ChatGptMessage;
import com.ttpfx.vo.ChatGptRequestParameter;
import com.ttpfx.vo.ChatGptResponseParameter;
import com.ttpfx.vo.Choices;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * @author ttpfx
 * @date 2023/3/23
 */
public class CustomChatGpt {
    /**
     * 自己chatGpt的ApiKey
     */
    private String apiKey;
    /**
     * 使用的模型
     */
    private String model = "gpt-3.5-turbo-0301";
    /**
     * 对应的请求接口
     */
    private String url = "https://api.openai.com/v1/chat/completions";
    /**
     * 默认编码
     */
    private Charset charset = StandardCharsets.UTF_8;


    /**
     * 创建一个ChatGptRequestParameter,用于携带请求参数
     */
    private ChatGptRequestParameter chatGptRequestParameter = new ChatGptRequestParameter();

    /**
     * 相应超时时间,毫秒
     */
    private int responseTimeout = 1000;

    public void setResponseTimeout(int responseTimeout) {
        this.responseTimeout = responseTimeout;
    }

    public CustomChatGpt(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getAnswer(CloseableHttpClient client, String question) {

        // 创建一个HttpPost
        HttpPost httpPost = new HttpPost(url);
        // 创建一个ObjectMapper,用于解析和创建json
        ObjectMapper objectMapper = new ObjectMapper();

        // 设置请求参数
        chatGptRequestParameter.addMessages(new ChatGptMessage("user", question));
        HttpEntity httpEntity = null;
        try {
            // 对象转换为json字符串
            httpEntity = new StringEntity(objectMapper.writeValueAsString(chatGptRequestParameter), charset);
        } catch (JsonProcessingException e) {
            System.out.println(question + "->json转换异常");
            return null;
        }
        httpPost.setEntity(httpEntity);


        // 设置请求头
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // 设置登录凭证
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);

        // 用于设置超时时间
        RequestConfig config = RequestConfig
                .custom()
                .setResponseTimeout(responseTimeout, TimeUnit.MILLISECONDS)
                .build();
        httpPost.setConfig(config);
        try {
            // 提交请求
            return client.execute(httpPost, response -> {
                // 得到返回的内容
                String resStr = EntityUtils.toString(response.getEntity(), charset);
                // 转换为对象
                ChatGptResponseParameter responseParameter = objectMapper.readValue(resStr, ChatGptResponseParameter.class);
                String ans = "";
                // 遍历所有的Choices(一般都只有一个)
                for (Choices choice : responseParameter.getChoices()) {
                    ChatGptMessage message = choice.getMessage();
                    chatGptRequestParameter.addMessages(new ChatGptMessage(message.getRole(), message.getContent()));
                    String s = message.getContent().replaceAll("\n+", "\n");
                    ans += s;
                }
                // 返回信息
                return ans;
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 发生异常,移除刚刚添加的ChatGptMessage
        chatGptRequestParameter.getMessages().remove(chatGptRequestParameter.getMessages().size()-1);
        return "您当前的网络无法访问";
    }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

使用

下面就是测试代码,我们只需要传入一个CloseableHttpClient 和 question 即可

public class Test {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String apiKey = "自己的ApiKey";
        CustomChatGpt customChatGpt = new CustomChatGpt(apiKey);
        // 根据自己的网络设置吧
        customChatGpt.setResponseTimeout(20000);
        while (true) {
            System.out.print("\n请输入问题(q退出):");
            String question = new Scanner(System.in).nextLine();
            if ("q".equals(question)) break;
            long start = System.currentTimeMillis();
            String answer = customChatGpt.getAnswer(httpClient, question);
            long end = System.currentTimeMillis();
            System.out.println("该回答花费时间为:" + (end - start) / 1000.0 + "秒");
            System.out.println(answer);
        }
        httpClient.close();
    }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

下面为运行图片

在这里插入图片描述
在这里插入图片描述

最后说明

对于ApiKey,只能说难者不会,会者不难,这个没办法教。

如果代码无法运行,或者运行速度及其缓慢,请使用代理,在HttpClient里面可以很轻松的使用代理

        String proxyIp = "127.0.0.1";
        int proxyPort = 7890;
        HttpHost httpHost = new HttpHost(proxyIp, proxyPort);
 
  • 1
  • 2
  • 3

上面就是一个示例,对于代理,这里也就无法继续进行说明了。

如果我们完成了上面的功能,是不是就能够对外提供接口,然后写一个自己的网页端的ChatGpt或者弄一个聊天机器人呢?当然没问题啊


2023/3/27 23:27更新

很多人找我要这个小程序的demo,其实源代码上面都已经给出了。既然有人问,那么就给出这个demo的github地址 git地址

使用指令 git clone https://github.com/c-ttpfx/chatGPT.git 可以轻松的下载到本地


2023/3/31 19:54更新
上面这个代码大家应该发现了问题,就是返回很慢,原因就是我们请求不是异步的,是等待所有答案都给出了再返回,所以速度很慢,并且容易出现超时。

解决方法就是使用异步请求,大家请参照 使用chatgpt实现微信聊天小程序(秒回复),github开源(附带链接) ,基本可以实现秒回复

2023/3/31 14:40更新
最新的代码已经支持代理,参考使用chatgpt实现微信聊天小程序(秒回复),github开源(附带链接)

   

标签:String,解决方案,ChatGptMessage,自己,new,GPT,import,message,public
From: https://www.cnblogs.com/oopo/p/17999769

相关文章

  • 超级详细 JAVA 对接 ChatGPT 教程,实现自己的AI对话小助手
    1    前言大家好,由于近期需要对接了ChatGPTAPI所以特地记录下来,据介绍该模型是和当前官网使用的相同的模型,如果你还没体验过ChatGPT,那么今天就教大家如何打破网络壁垒,打造一个属于自己的智能助手把。本文包括APIKey的申请以及网络代理的搭建,那么事不宜迟,我们现在开......
  • java开发的chatGPT机器人系统
      ChatGPT机器人发展趋势:  更加个性化:随着数据和技术的不断进步,ChatGPT机器人将能够更加准确地理解用户的需求和偏好,并提供更加个性化的回复和服务。  多语言支持:随着ChatGPT在各个国家和地区的普及,对多语言支持的需求也越来越高。未来的ChatGPT机器人将支持更多......
  • ChatGPT全栈开发实战:从需求分析到数据可视化,一站式指南助你快速构建全面应用
    《ChatGPT全栈开发实战:从需求分析到数据可视化,一站式指南助你快速构建全面应用》是一本旨在引领我们进入全栈开发世界的综合指南。通过结合强大的ChatGPT技术和全栈开发的实践,我们将探索需求分析、后端开发、前端开发、测试、运维、算法优化、数据库、网络安全、人工智能、自然......
  • 关于 java如何集成chatgpt,如何开发接口,如何集成vue前端界面
    Java如何集成ChatGPT,如何开发接口,如何集成Vue前端界面随着人工智能技术的不断发展,聊天机器人已经成为了人们日常生活中不可或缺的一部分。ChatGPT是一种基于深度学习的聊天机器人技术,它可以通过学习大量的语料库来生成自然流畅的对话。本文将介绍如何使用Java语言集成ChatGPT,开......
  • 如何用ChatGPT使开发效率提高50%以上?
    简介  ChatGPT是一个大型语言模型,由OpenAI开发。它被训练用于进行对话式交互,能够理解和生成自然语言文本。ChatGPT可以用于多种任务和场景,包括但不限于:智能助手、创意生成、语言学习、编程辅助等。ChatGPT的优势在于它的广泛知识和对多个领域的理解。它可以利用其训练数据中......
  • 基于Python开发ChatGPT应用
    ChatGPT是目前非常热门的一种人工智能模型,它是基于深度学习技术的一种针对自然语言处理的算法。ChatGPT的应用非常广泛,可以应用于聊天机器人、文本生成、翻译、自动化等多个领域。本文将介绍如何使用Python开发一个基于ChatGPT模型的聊天机器人应用。 1.ChatGPT简介......
  • 同时使用300万个GPT是什么体验?ChatGPT新上线@功能
    据ChatGPT特邀灰度用户介绍,ChatGPT内测推出了“GPT@Mentions”功能,允许用户使用“@”标签+GPTs的名称来内联GPT商店中的任意一款GPTs,实现在同一对话窗口中与多个定制的GPT模型交互,就像将多个高级智能代理集成到您的私人助手中,每个代理都擅长解决特定的问题。目前GPT商店超300万个G......
  • MySQL大表分页查询的坑以及解决方案
    最近在做一个需求,需求内容中有一个功能点是查询指定用户标签里的用户id,这里做了分页查询,分页查询是用mysql的LIMIT设置offset和size值来实现的。在程序执行过程中会发现,如果查询的用户标签数据量很大时会出现慢查询告警,这里已经对mysql表的标签名称和用户id字段都加了索引,并且limi......
  • 【全栈白嫖】如何应用云服务0成本发布自己的产品
    现在云服务很多都有免费的额度,下面操作下如何结合全栈云服务,页面部署,数据库,缓存服务,零成本上线自己的产品全栈云northflankNorthflank是我们所说的全栈云平台,它允许开发者注册,连接他们的版本控制-GitHub、Bitbucket或GitLab-并通过Docker文件立即构建和部署他们的所有存......
  • 解决gpt返回json Python没法解析的情况
    importreimportjsondefreplace_newlines(match):#在匹配的字符串中替换\n和\rreturnmatch.group(0).replace('\n','\\n').replace('\r','\\r')defclean_json_str(json_str:str)->str:""&......