广告
返回顶部
首页 > 资讯 > 服务器 >基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的方法
  • 596
分享到

基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的方法

摘要

目录最终接口效果演示ChatGPT介绍SpringBoot介绍构建springBoot项目Post请求解析接口控制类打包发布接口到服务器? ChatGPT是最近很热门的AI智能聊天机

? ChatGPT是最近很热门的AI智能聊天机器人
? 本文使用SpringBoot+OpenAI的官方API接口,自己实现一个可以返回对话数据的接口并上线服务器
? 用途方面相比于普通的聊天ai更加的广泛,甚至可以帮助你改BUG,写代码!!!

最终接口效果演示

ChatGPT介绍

ChatGPT是一款基于自然语言处理技术的聊天机器人。它使用受控语料库,并使用最先进的深度学习技术来学习用户的输入,以便以最相似的方式回应。ChatGPT可以模拟真实的人类对话,并能够更贴近用户的需求,提供更有价值的服务。

SpringBoot介绍

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为一个重要的先驱。

Spring Boot为Spring应用提供了一种快速的起步方式,可用来创建独立的,生产级的基于Spring的应用程序。它提供了一种更快捷的方式来创建Spring应用,并且不需要任何XML配置。Spring Boot提供了可选择的高级特性,如持久层技术和安全性,可以让你快速构建令人满意的WEB应用程序和服务。

构建SpringBoot项目

项目主要使用的Maven依赖如下,通过Maven构建项目即可

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yopai</groupId>
    <artifactId>openapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>openapi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastJSON</artifactId>
            <version>2.0.21</version>
        </dependency>

Post请求解析

RestTemplate是Spring框架的一个用于访问RESTful服务的客户端库,它提供了一组简单的、可扩展的方法来访问RESTful服务。它可以访问Http服务,并以字符串、Java对象或多种格式的数据(如jsON)进行序列化和反序列化。RestTemplate支持多种HTTP方法,如GET、POST、PUT、DELETE等,可以用来访问RESTful服务,并获取服务器返回的结果。

 public static String sendPost(String data) {
        RestTemplate client = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization","Bearer <YourAPI>");
        httpHeaders.add("Content-Type", "application/json"); // 传递请求体时必须设置
//        String requestJson = "{\n" +
//                "    \"model\": \"text-davinci-003\",\n" +
//                "     \"prompt\": \"你好\",\n" +
//                "      \"temperature\": 0, \n" +
//                "      \"max_tokens\": 2048\n" +
//                "}";
        String requestJson = String.fORMat(
                "{\n" +
                        "    \"model\": \"text-davinci-003\",\n" +
                        "     \"prompt\": \"%s\",\n" +
                        "      \"temperature\": 0, \n" +
                        "      \"max_tokens\": 2048\n" +
                        "}",data
        );
        HttpEntity<String> entity = new HttpEntity<String>(requestJson,httpHeaders);
        ResponseEntity<String> response = client.exchange("https://api.openai.com/v1/completions", HttpMethod.POST, entity, String.class);
        System.out.println(response.getBody());
        JSONObject jsonObject = JSONObject.parseObject(response.getBody());
        JSONArray choices = jsonObject.getJSONArray("choices");
        String text = choices.getJSONObject(0).getString("text");
//        Object o = jsonObject.get("\"choices\"");
        return text;
    }

接口控制类

    @PostMapping("gpt")
    public JsonData get(@RequestBody Promat promat){
        String text = HttpGPT.sendPost(promat.getData());
        System.out.println(promat);
        JsonData jsonData = JsonData.bulidSuccess(text);
        return jsonData;
    }

打包发布接口到服务器

通过idea将项目进行打包后上传到服务器,运行以下命令即可完成线上部署
java -jar :运行打包好的项目
nohup:让项目在后台一直运行
之后把LocalHost修改成服务器的公网IP即可

到此这篇关于基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的文章就介绍到这了,更多相关ChatGPT+SpringBoot打造智能聊天AI机器人接口内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的方法

本文链接: https://www.lsjlt.com/news/196367.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作