From 1583407f5d1b93584546a6a4e73cc497eef14489 Mon Sep 17 00:00:00 2001 From: zyx <438329180@qq.com> Date: Sun, 5 Feb 2023 16:47:09 +0800 Subject: [PATCH] =?UTF-8?q?chatgpt=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpdemo/controller/ChatgptController.java | 32 ++++++++ .../zyx/mpdemo/service/ChatgptService.java | 73 +++++++++++++++++++ src/main/resources/application.yml | 3 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/zyx/mpdemo/controller/ChatgptController.java create mode 100644 src/main/java/com/zyx/mpdemo/service/ChatgptService.java diff --git a/src/main/java/com/zyx/mpdemo/controller/ChatgptController.java b/src/main/java/com/zyx/mpdemo/controller/ChatgptController.java new file mode 100644 index 0000000..5a41ef4 --- /dev/null +++ b/src/main/java/com/zyx/mpdemo/controller/ChatgptController.java @@ -0,0 +1,32 @@ +package com.zyx.mpdemo.controller; + +import com.zyx.mpdemo.common.resp.CommonResponse; +import com.zyx.mpdemo.service.ChatgptService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; + +/** + * @author Yaxi.Zhang + * @since 2023/2/5 15:58 + */ +@Slf4j +@Controller +@ResponseBody +@RequestMapping("/chatgpt") +public class ChatgptController { + + @Resource + private ChatgptService chatgptService; + + @GetMapping("/answer") + public CommonResponse getCourseInfo(@RequestParam String question) { + return CommonResponse.success(chatgptService.getAnswer(question), "获取答案成功"); + } + +} diff --git a/src/main/java/com/zyx/mpdemo/service/ChatgptService.java b/src/main/java/com/zyx/mpdemo/service/ChatgptService.java new file mode 100644 index 0000000..b763941 --- /dev/null +++ b/src/main/java/com/zyx/mpdemo/service/ChatgptService.java @@ -0,0 +1,73 @@ +package com.zyx.mpdemo.service; + +import cn.hutool.core.net.url.UrlBuilder; +import cn.hutool.http.ContentType; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Yaxi.Zhang + * @since 2023/2/5 16:36 + */ +@Service +public class ChatgptService { + + @Value("${chatgpt.api-key}") + private String apiKey; + + public String getAnswer(String question) { + String body = getAnswerBody(question); + return answerFromBody(body); + } + + private String getAnswerBody(String question) { + return HttpUtil + .createPost(getAnswerUrl()) + .addHeaders(getHeaders()) + .body(getQuesStr(question), ContentType.JSON.getValue()) + .setReadTimeout(60 * 30 * 1000) + .setConnectionTimeout(60 * 30 * 1000) + .setMaxRedirectCount(3) + .execute() + .body(); + } + + private String getAnswerUrl() { + return UrlBuilder.of() + .setScheme("https") + .setHost("api.openai.com") + .addPath("v1/completions") + .build(); + } + + private Map getHeaders() { + Map headers = new HashMap<>(); + headers.put("Authorization", "Bearer " + apiKey); + headers.put("Content-Type", "application/json"); + return headers; + } + + private String getQuesStr(String question) { + Map reqMap = new HashMap<>(); + reqMap.put("model", "text-davinci-003"); + reqMap.put("prompt", question); + reqMap.put("temperature", 0); + reqMap.put("max_tokens", 2048); + return JSONUtil.toJsonStr(reqMap); + } + + private String answerFromBody(String body) { + JSONObject jsonObject = JSONUtil.parseObj(body); + JSONArray choices = jsonObject.getJSONArray("choices"); + JSONObject choice = choices.getJSONObject(0); + return choice.getStr("text"); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fe5970d..3eee388 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -24,7 +24,8 @@ spring: max-lifetime: 60000 connection-timeout: 30000 connection-test-query: SELECT 1 - +chatgpt: + api-key: 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 mybatis-plus: # 设置MyBatis-Plus的全局配置 # global-config: