Java SpringBoot集成ChatGPT實(shí)現(xiàn)AI聊天
前言
ChatGPT已經(jīng)組件放開(kāi)了,現(xiàn)在都可以基于它寫(xiě)插件了。但是說(shuō)實(shí)話我還真沒(méi)想到可以用它干嘛,也許可以用它結(jié)合文字語(yǔ)音開(kāi)發(fā)一個(gè)老人小孩需要的智能的說(shuō)話陪伴啥的。
今天我就先分享下SpringBoot結(jié)合ChatGPT,先看看對(duì)話效果。
一、依賴引入
這個(gè)基本上沒(méi)啥依賴引入哦,我這里就是一個(gè)干干凈凈的SpringBoot項(xiàng)目,引入Hutool的工具包就行了。看看我的整體依賴吧,直接上pom.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaotian</groupId> <artifactId>superapi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>superapi</name> <description>superapi</description> <properties> <java.version>17</java.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- Fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.21</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
二、接口開(kāi)發(fā)
1.項(xiàng)目結(jié)構(gòu)
2.配置文件
ChatGPT: connect-timeout: 60000 # HTTP請(qǐng)求連接超時(shí)時(shí)間 read-timeout: 60000 # HTTP請(qǐng)求讀取超時(shí)時(shí)間 variables: # 自定義變量: apiKey: youApiKey # 你的 OpenAI 的 API KEY model: text-davinci-003 # ChartGPT 的模型 maxTokens: 50 # 最大 Token 數(shù) temperature: 0.5 # 該值越大每次返回的結(jié)果越隨機(jī),即相似度越小
3.接口實(shí)現(xiàn)代碼
GPTRequest
package com.xiaotian.superapi.chatgpt.entity; import lombok.Data; @Data public class GPTRequest { /** * 問(wèn)題 */ private String askStr; /** * 回答 */ private String replyStr; }
GPTResponse
package com.xiaotian.superapi.chatgpt.entity; import lombok.Data; import java.util.List; /** * GPT-3 返回對(duì)象 * @author zhengwen */ @Data public class GPTResponse { private String id; private String object; private String created; private String model; private List<GPTChoice> choices; }
GPTChoice
package com.xiaotian.superapi.chatgpt.entity; import lombok.Data; /** * GPT-3 返回choice對(duì)象 * @author zhengwen */ @Data public class GPTChoice { private String text; private Integer index; }
ChatGPTController
package com.xiaotian.superapi.chatgpt.controller; import cn.hutool.json.JSONUtil; import com.xiaotian.superapi.chatgpt.entity.GPTRequest; import com.xiaotian.superapi.chatgpt.service.ChartGPTService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * GPT-3接口 * * @author zhengwen */ @Slf4j @RestController @RequestMapping("/chatGpt") public class ChatGPTController { @Resource private ChartGPTService chartGPTService; /** * openAI GPT-3 * * @param gptRequest 條件對(duì)象 * @return 出參對(duì)象 */ @PostMapping("/askAi") public String askAi(@RequestBody GPTRequest gptRequest) { String replyStr = chartGPTService.send(gptRequest.getAskStr()); gptRequest.setReplyStr(replyStr); return JSONUtil.toJsonStr(gptRequest); } }
ChartGPTService
package com.xiaotian.superapi.chatgpt.service; public interface ChartGPTService { String send(String prompt); }
ChartGPTServiceImpl
package com.xiaotian.superapi.chatgpt.service.impl; import cn.hutool.http.Header; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.xiaotian.superapi.chatgpt.entity.GPTResponse; import com.xiaotian.superapi.chatgpt.service.ChartGPTService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @Slf4j @Service public class ChartGPTServiceImpl implements ChartGPTService { @Value("${ChatGPT.variables.apiKey}") private String apiKey; @Value("${ChatGPT.variables.maxTokens}") private String maxTokens; @Value("${ChatGPT.variables.model}") private String model; @Value("${ChatGPT.variables.temperature}") private String temperature; @Override public String send(String prompt) { JSONObject bodyJson = new JSONObject(); bodyJson.put("prompt", prompt); bodyJson.put("max_tokens", Integer.parseInt(maxTokens)); bodyJson.put("temperature", Double.parseDouble(temperature)); Map<String,Object> headMap = new HashMap<>(); headMap.put("Authorization", "Bearer " + apiKey); HttpResponse httpResponse = HttpUtil.createPost("https://api.openai.com/v1/engines/" + model + "/completions") .header(Header.AUTHORIZATION, "Bearer " + apiKey) .body(JSONUtil.toJsonStr(bodyJson)) .execute(); String resStr = httpResponse.body(); log.info("resStr: {}", resStr); GPTResponse gptResponse = JSONUtil.toBean(resStr, GPTResponse.class); return gptResponse.getChoices().get(0).getText().replaceAll("\\n",""); } }
三、使用
接口信息
url:/chatGpt/askAi
type:post
入?yún)ⅲ?br />{
“askStr”:“今天你吃飯了嗎”
}
我的幾個(gè)示例
下面是幾個(gè)問(wèn)的示例:
總結(jié)
- 不得不說(shuō)ChatGPT確實(shí)強(qiáng)大,涉及各學(xué)科
- 這個(gè)在加上訊飛語(yǔ)言SDK那妥妥的就是一個(gè)”小愛(ài)同學(xué)“
- 真要上,這里分享的代碼還需要優(yōu)化打磨哦
到此這篇關(guān)于Java SpringBoot集成ChatGPT實(shí)現(xiàn)AI聊天的文章就介紹到這了,更多相關(guān)SpringBoot ChatGPT實(shí)現(xiàn)AI聊天內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot詳解五種實(shí)現(xiàn)跨域的方式
跨域指的是瀏覽器不能執(zhí)?其他?站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對(duì)javascript施加的安全限制,這篇文章主要介紹了springboot實(shí)現(xiàn)跨域的5種方式,需要的朋友可以參考下2022-06-06spring?MVC實(shí)現(xiàn)簡(jiǎn)單登錄功能
這篇文章主要為大家詳細(xì)介紹了spring?MVC實(shí)現(xiàn)簡(jiǎn)單登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09java時(shí)間戳與日期相互轉(zhuǎn)換工具詳解
這篇文章主要為大家詳細(xì)介紹了java各種時(shí)間戳與日期之間相互轉(zhuǎn)換的工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12SpringBoot的jar包如何啟動(dòng)的實(shí)現(xiàn)
本文主要介紹了SpringBoot的jar包如何啟動(dòng)的實(shí)現(xiàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03jvm字符串常量池在什么內(nèi)存區(qū)域問(wèn)題解析
這篇文章主要介紹了jvm字符串常量池在什么內(nèi)存區(qū)域的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11使用maven創(chuàng)建web項(xiàng)目的方法步驟(圖文)
本篇文章主要介紹了使用maven創(chuàng)建web項(xiàng)目的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01