前言
ChatGPT已经组件放开了,现在都可以基于它写插件了。但是说实话我还真没想到可以用它干嘛,也许可以用它结合文字语音开发一个老人小孩需要的智能的说话陪伴啥的。
今天我就先分享下SpringBoot结合ChatGPT,先看看对话效果。
一、依赖引入
这个基本上没啥依赖引入哦,我这里就是一个干干净净的SpringBoot项目,引入Hutool的工具包就行了。看看我的整体依赖吧,直接上pom.xml文件。
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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>
二、接口开发
1.项目结构
2.配置文件
ChatGPT:connect-timeout:60000# HTTP请求连接超时时间read-timeout:60000# HTTP请求读取超时时间variables:# 自定义变量:apiKey: youApiKey # 你的 OpenAI 的 API KEYmodel: text-davinci-003# ChartGPT 的模型maxTokens:50# 最大 Token 数temperature:0.5# 该值越大每次返回的结果越随机,即相似度越小
3.接口实现代码
GPTRequest
packagecom.xiaotian.superapi.chatgpt.entity;importlombok.Data;@DatapublicclassGPTRequest{/**
* 问题
*/privateString askStr;/**
* 回答
*/privateString replyStr;}
GPTResponse
packagecom.xiaotian.superapi.chatgpt.entity;importlombok.Data;importjava.util.List;/**
* GPT-3 返回对象
* @author zhengwen
*/@DatapublicclassGPTResponse{privateString id;privateString object;privateString created;privateString model;privateList<GPTChoice> choices;}
GPTChoice
packagecom.xiaotian.superapi.chatgpt.entity;importlombok.Data;/**
* GPT-3 返回choice对象
* @author zhengwen
*/@DatapublicclassGPTChoice{privateString text;privateInteger index;}
ChatGPTController
packagecom.xiaotian.superapi.chatgpt.controller;importcn.hutool.json.JSONUtil;importcom.xiaotian.superapi.chatgpt.entity.GPTRequest;importcom.xiaotian.superapi.chatgpt.service.ChartGPTService;importjakarta.annotation.Resource;importlombok.extern.slf4j.Slf4j;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/**
* GPT-3接口
*
* @author zhengwen
*/@Slf4j@RestController@RequestMapping("/chatGpt")publicclassChatGPTController{@ResourceprivateChartGPTService chartGPTService;/**
* openAI GPT-3
*
* @param gptRequest 条件对象
* @return 出参对象
*/@PostMapping("/askAi")publicStringaskAi(@RequestBodyGPTRequest gptRequest){String replyStr = chartGPTService.send(gptRequest.getAskStr());
gptRequest.setReplyStr(replyStr);returnJSONUtil.toJsonStr(gptRequest);}}
ChartGPTService
packagecom.xiaotian.superapi.chatgpt.service;publicinterfaceChartGPTService{Stringsend(String prompt);}
ChartGPTServiceImpl
packagecom.xiaotian.superapi.chatgpt.service.impl;importcn.hutool.http.Header;importcn.hutool.http.HttpResponse;importcn.hutool.http.HttpUtil;importcn.hutool.json.JSONObject;importcn.hutool.json.JSONUtil;importcom.xiaotian.superapi.chatgpt.entity.GPTResponse;importcom.xiaotian.superapi.chatgpt.service.ChartGPTService;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Service;importjava.util.HashMap;importjava.util.Map;@Slf4j@ServicepublicclassChartGPTServiceImplimplementsChartGPTService{@Value("${ChatGPT.variables.apiKey}")privateString apiKey;@Value("${ChatGPT.variables.maxTokens}")privateString maxTokens;@Value("${ChatGPT.variables.model}")privateString model;@Value("${ChatGPT.variables.temperature}")privateString temperature;@OverridepublicStringsend(String prompt){JSONObject bodyJson =newJSONObject();
bodyJson.put("prompt", prompt);
bodyJson.put("max_tokens",Integer.parseInt(maxTokens));
bodyJson.put("temperature",Double.parseDouble(temperature));Map<String,Object> headMap =newHashMap<>();
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”:“今天你吃饭了吗”
}
我的几个示例
下面是几个问的示例:
总结
- 不得不说ChatGPT确实强大,涉及各学科
- 这个在加上讯飞语言SDK那妥妥的就是一个”小爱同学“
- 真要上,这里分享的代码还需要优化打磨哦 反正看起来就是啥都可以问它,简直是江湖百晓生、智者,上知天文下至地理,无所不知啊。有点好玩,就分享到这里,希望能博大家一笑。
版权归原作者 肥仔哥哥1930 所有, 如有侵权,请联系我们删除。