您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java SpringBoot怎么集成ChatGPT實現AI聊天”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java SpringBoot怎么集成ChatGPT實現AI聊天”文章能幫助大家解決問題。
這個基本上沒啥依賴引入哦,我這里就是一個干干凈凈的SpringBoot項目,引入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>
ChatGPT: connect-timeout: 60000 # HTTP請求連接超時時間 read-timeout: 60000 # HTTP請求讀取超時時間 variables: # 自定義變量: apiKey: youApiKey # 你的 OpenAI 的 API KEY model: text-davinci-003 # ChartGPT 的模型 maxTokens: 50 # 最大 Token 數 temperature: 0.5 # 該值越大每次返回的結果越隨機,即相似度越小
GPTRequest
package com.xiaotian.superapi.chatgpt.entity; import lombok.Data; @Data public class GPTRequest { /** * 問題 */ private String askStr; /** * 回答 */ private String replyStr; }
GPTResponse
package com.xiaotian.superapi.chatgpt.entity; import lombok.Data; import java.util.List; /** * GPT-3 返回對象 * @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對象 * @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 條件對象 * @return 出參對象 */ @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
入參:
{
“askStr”:“今天你吃飯了嗎”
}
我的幾個示例
下面是幾個問的示例:
關于“Java SpringBoot怎么集成ChatGPT實現AI聊天”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。