Bladeren bron

Merge branch 'master' of http://git.izouma.com/xiongzhu/raex_back into dev-meta

sunkean 3 jaren geleden
bovenliggende
commit
639fcfd326

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -175,6 +175,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/tradeAuction/get/**").permitAll()
                 .antMatchers("/tradeAuctionOrder/all").permitAll()
                 .antMatchers("/metaVisitor/save").permitAll()
+                .antMatchers("/gpt3/**").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 45 - 0
src/main/java/com/izouma/nineth/web/GPTController.java

@@ -0,0 +1,45 @@
+package com.izouma.nineth.web;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.github.kevinsawicki.http.HttpRequest;
+import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.service.SysConfigService;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/gpt3")
+@AllArgsConstructor
+@NoArgsConstructor
+public class GPTController {
+
+    private SysConfigService sysConfigService;
+
+    @RequestMapping("/text")
+    public String text(@RequestParam String prompt) {
+        String token = sysConfigService.getString("chatGPT_token");
+        Map<String, Object> params = new HashMap<>();
+        params.put("model", "text-davinci-003");
+        params.put("prompt", prompt);
+        params.put("max_tokens", 2000);
+        params.put("temperature", 1);
+        HttpRequest request = HttpRequest.post("https://api.openai.com/v1/completions")
+                .header("Authorization", "Bearer " + token)
+                .contentType("application/json")
+                .send(JSON.toJSONString(params));
+        int code = request.code();
+        String body = request.body();
+        JSONObject res = JSON.parseObject(body);
+        if (code != 200) {
+            throw new BusinessException(res.getJSONObject("error").getString("message"));
+        }
+        return res.getJSONArray("choices").getJSONObject(0).getString("text").trim();
+    }
+}