ChatGPT AI 智能问答助手设计实现思路 和 经验分享

项目脚手架搭建

​ 参考该篇文章 创建项目脚手架经验(基于gitee仓库)_爱吃芝士的土豆倪的博客-CSDN博客

​ 当有了完整的创建脚手架的流程之后,就需要分析一个项目需要什么子模块。按需添加子模块和父模块之间的关系。

分析知识星球接口信息

​ 主要是分析接口的调用。

​ ChatGPT AI只能问答的核心就是爬取到需要回答的问题,然后进行回答就可以了。

​ 当我们在知识星球上发起一个提问的时候,其实通过 开发者模式的 网络模块能够监控到这次调用的请求是什么?如下所示:

​ 既然我们能够监控到对应的接口,那么在浏览器发送的请求其实在代码里也可以控制。所以只需要分析出来 获取对应的评论用什么接口,发送回答用什么接口即可。

爬取知识星球信息

​ 想要真的获取到对应数据,通过一个上图的接口就可以实现了,当我们获取到了对应的问题之后,就可以去解析对应的回答接口了。

​ 以上就是需要解析的两个核心接口了。

​ 当在代码里爬取到问题,然后请求对应的问题进行回答即可。

对接ChatGPT,调用接口

​ 首先,为了使用ChatGPT的能力,需要拥有能够使用的keys。具体看这里如何快速调用ChatGPT API - 知乎 (zhihu.com)

​ 当有了key以后,还需要一个代理接口,不然得话,无法建立连接,具体的看这里 OpenAI API 代理 (openai-proxy.com)

​ 当具备这两点后就可以正常调用gpt的接口来完成问答了,模板如下:

java 复制代码
private Logger logger = LoggerFactory.getLogger(OpenAI.class);
 
    @Override
    public String doChatGPT(String question, String openAiKey, String openAiProxy, String model) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
 
        HttpPost post = new HttpPost("https://" + openAiProxy +"/v1/completions");
        post.addHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Bearer " + openAiKey);
 
        String paramJson = "{\"model\": \""+ model +"\", \"prompt\": \"" + question + "\", \"temperature\": 0, \"max_tokens\": 1024}";
 
        StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8"));
        post.setEntity(stringEntity);
 
        CloseableHttpResponse response = httpClient.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String jsonStr = EntityUtils.toString(response.getEntity());
            AIAnswer aiAnswer = JSON.parseObject(jsonStr, AIAnswer.class);
            StringBuilder answers = new StringBuilder();
            List<Choices> choices = aiAnswer.getChoices();
            for (Choices choice : choices) {
                answers.append(choice.getText());
            }
            return answers.toString();
        } else {
            throw new RuntimeException("api.openai.com Err Code is " + response.getStatusLine().getStatusCode());
        }
 
    }
 
 
public class AIAnswer {
    private String id;
 
    private String object;
 
    private int created;
 
    private String model;
 
    private List<Choices> choices;
 
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setObject(String object){
        this.object = object;
    }
    public String getObject(){
        return this.object;
    }
    public void setCreated(int created){
        this.created = created;
    }
    public int getCreated(){
        return this.created;
    }
    public void setModel(String model){
        this.model = model;
    }
    public String getModel(){
        return this.model;
    }
    public void setChoices(List<Choices> choices){
        this.choices = choices;
    }
    public List<Choices> getChoices(){
        return this.choices;
    }
 
}
 
public class Choices {
    private String text;
 
    private int index;
 
    private String logprobs;
 
    private String finish_reason;
 
    public String getText() {
        return text;
    }
 
    public void setText(String text) {
        this.text = text;
    }
 
    public int getIndex() {
        return index;
    }
 
    public void setIndex(int index) {
        this.index = index;
    }
 
    public String getLogprobs() {
        return logprobs;
    }
 
    public void setLogprobs(String logprobs) {
        this.logprobs = logprobs;
    }
 
    public String getFinish_reason() {
        return finish_reason;
    }
 
    public void setFinish_reason(String finish_reason) {
        this.finish_reason = finish_reason;
    }
 
}

整合知识星球与ChatGPT,完成自动化回答

​ 使用定时任务,首先通过接口获取到问题,然后调用chatgpt接口回答问题,最后将答案通过接口发送回去即可。

打包项目镜像,部署Docker运行

​ 首先需要将父模块 install,此时拿到 运行子模块的 jar包,使用 dockerfile进行 build。具体流程可以参考这里 Windows下部署SpringBoot的实践方案(Docker & Docker Desktop)_爱吃芝士的土豆倪的博客-CSDN博客

​ 在本项目中也可以实时的打印出日志来进行记录,使用 docker logs -f <容器id> 容器id 可以使用 docker ps 或者 docker desktop中获取。

多组任务服务配置

​ 为了实现多组任务服务配置,需要实现Spring框架中的EnvironmentAware 和 SchedulingConfigurer接口。其中EnvironmentAware 接口能够获取Spring应用程序上下文环境,并访问配置属性。SchedulingConfigurer 接口则允许配置定时任务的调度。

​ 本质上就是通过EnvironmentAware接口实现的方法 获取到配置文件对应的分组信息,并封装。然后在SchedulingConfigurer中使用 taskRegistrar.addCronTask 添加 定时任务即可!

​ 其中 定时任务实现了Runnable接口,提供了run方法的实现。当定时任务被调度并触发执行时,注册器会自动调用run方法来执行 定时任务中定义的任务逻辑。

相关推荐
H5css�海秀2 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
SuniaWang2 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
韩立学长2 小时前
Springboot校园跑腿业务系统0b7amk02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
sheji34162 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
代码栈上的思考3 小时前
消息队列:内存与磁盘数据中心设计与实现
后端·spring
程序员小假4 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha4 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34164 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Victor3565 小时前
MongoDB(57)如何优化MongoDB的查询性能?
后端
Victor3565 小时前
MongoDB(58)如何使用索引优化查询?
后端