parent
8263bdbf37
commit
1583407f5d
@ -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<String> getCourseInfo(@RequestParam String question) {
|
||||||
|
return CommonResponse.success(chatgptService.getAnswer(question), "获取答案成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String, String> getHeaders() {
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
headers.put("Authorization", "Bearer " + apiKey);
|
||||||
|
headers.put("Content-Type", "application/json");
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getQuesStr(String question) {
|
||||||
|
Map<String, Object> 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue