|
|
@@ -0,0 +1,72 @@
|
|
|
+package shop.alien.store.controller;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import shop.alien.store.util.ai.AiAuthTokenUtil;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Api(tags = {"ai聊天"})
|
|
|
+@CrossOrigin
|
|
|
+@RestController
|
|
|
+@RequestMapping("/aiChat")
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class AiChatController {
|
|
|
+
|
|
|
+ private final AiAuthTokenUtil aiAuthTokenUtil;
|
|
|
+
|
|
|
+ private final RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Value("${third-party-ai-chat.base-url:http://192.168.2.250:9000/ai/life-manager/api/v1/question_classification/classify_and_route}")
|
|
|
+ private String aiChatUrl;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用 AI 服务,获取聊天结果
|
|
|
+ *
|
|
|
+ * @return 聊天结果
|
|
|
+ */
|
|
|
+ @RequestMapping("/aiChatResult")
|
|
|
+ public ResponseEntity<String> aiChat(@RequestParam("question") String question){
|
|
|
+ String accessToken = aiAuthTokenUtil.getAccessToken();
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ if (!StringUtils.hasText(accessToken)) {
|
|
|
+ data.put("fail","登录失败");
|
|
|
+ return ResponseEntity.badRequest().body(data.toJSONString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化请求体Map
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("question", question);
|
|
|
+ HttpHeaders aiHeaders = new HttpHeaders();
|
|
|
+ aiHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ aiHeaders.set("Authorization", "Bearer " + accessToken);
|
|
|
+// aiHeaders.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1cHN0b3JlQGFkbWluLmNvbSIsImlkIjo2LCJ0aW1lIjoxNzYyOTI1NDAzLjY1MTY5MjZ9.07lz8Ox2cGC28UCmqcKCt5R6Rfwtgs-Eiu0ttgWRxws");
|
|
|
+
|
|
|
+ HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, aiHeaders);
|
|
|
+
|
|
|
+ try {
|
|
|
+ ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(aiChatUrl, request, String.class);
|
|
|
+ return stringResponseEntity;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用AI文本审核接口 接口异常------", e);
|
|
|
+ }
|
|
|
+ return ResponseEntity.badRequest().body(null);
|
|
|
+ }
|
|
|
+}
|