refactor: AI助手接口改为post

This commit is contained in:
ivmiku
2024-09-04 22:25:57 +08:00
parent ee64bca6fd
commit 2a36709093
10 changed files with 71 additions and 29 deletions

View File

@@ -1,12 +1,15 @@
package com.ivmiku.tutorial.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ivmiku.tutorial.entity.AssistantQuery;
import com.ivmiku.tutorial.response.Result;
import com.ivmiku.tutorial.service.AssistantService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@@ -16,10 +19,10 @@ public class AssistantController {
@Resource
private AssistantService assistantService;
@GetMapping("/response")
public Object getResponse(@RequestParam String input, @RequestParam int size, @RequestParam String language) {
String userInput = assistantService.speechRecognition(input, size, language);
Map<String, Object> map = assistantService.getResponse(userInput, language);
@PostMapping("/response")
public Object getResponse(@RequestBody AssistantQuery query) {
String userInput = assistantService.speechRecognition(query.getContent(), query.getSize(), query.getLanguage());
Map<String, Object> map = assistantService.getResponse(userInput, query.getLanguage());
if (map == null) {
return Result.error("请求出错");
}

View File

@@ -0,0 +1,10 @@
package com.ivmiku.tutorial.entity;
import lombok.Data;
@Data
public class AssistantQuery {
private String content;
private Integer size;
private String language;
}

View File

@@ -65,10 +65,11 @@ public class AssistantService {
}
JSONArray choices = result.getJSONArray("choices");
JSONObject message = choices.getJSONObject(0);
return message.getString("message");
JSONObject content = message.getJSONObject("message");
return content.getString("content");
}
public String textToSpeech(String content) {
public String textToSpeech(String content, String language) {
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
@@ -80,6 +81,9 @@ public class AssistantService {
req.setText(content);
req.setSessionId(SnowflakeUtil.getNext());
req.setVoiceType(101006L);
if (language.equals("English")) {
req.setPrimaryLanguage(2L);
}
TextToVoiceResponse resp = client.TextToVoice(req);
return resp.getAudio();
} catch (TencentCloudSDKException e) {
@@ -159,7 +163,7 @@ public class AssistantService {
content = getAiResponse(query, language);
}
Map<String, Object> map = new HashMap<>();
map.put("content", textToSpeech(content));
map.put("content", textToSpeech(content, language));
return map;
}
}

View File

@@ -53,7 +53,7 @@ public class NavigateService {
}
public Map<String, Object> scanTicket(BufferedImage image) {
String base64Img = ImgUtil.toBase64(image, "jpg");
String base64Img = ImgUtil.toBase64(image, "png");
SmartStructuralOCRV2Response resp;
try {
Credential cred = new Credential(secretId, secretKey);

View File

@@ -10,7 +10,7 @@ server.port=8432
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Shuodedaoli114514
spring.datasource.url=jdbc:mysql://mysql:4514/tutorial?useUnicode=true&characterEncoding=utf8&useSSL=false&ServerTimezone=Asia/Shanghai
spring.datasource.url=jdbc:mysql://mysql:3306/tutorial?useUnicode=true&characterEncoding=utf8&useSSL=false&ServerTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.cloud.nacos.discovery.server-addr=nacos:8848
spring.cloud.nacos.discovery.enabled=true