feat: 语音助手新增多语言

This commit is contained in:
ivmiku
2024-08-28 18:01:22 +08:00
parent 01d3823a02
commit be3c8728fb
2 changed files with 24 additions and 15 deletions

View File

@@ -17,8 +17,9 @@ public class AssistantController {
private AssistantService assistantService; private AssistantService assistantService;
@GetMapping("/response") @GetMapping("/response")
public Object getResponse(@RequestParam String input) { public Object getResponse(@RequestParam String input, @RequestParam int size, @RequestParam String language) {
Map<String, Object> map = assistantService.getResponse(input); String userInput = assistantService.speechRecognition(input, size, language);
Map<String, Object> map = assistantService.getResponse(userInput, language);
if (map == null) { if (map == null) {
return Result.error("请求出错"); return Result.error("请求出错");
} }

View File

@@ -34,10 +34,16 @@ public class AssistantService {
private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50"; private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50";
private final String secretKey = "KajjcNyNaaUCqQroqpzNoMtTHNj4Lbil"; private final String secretKey = "KajjcNyNaaUCqQroqpzNoMtTHNj4Lbil";
public Map<String, Object> getAiResponse(String userInput) { public String getAiResponse(String userInput, String language) {
Map<String, Object> message1 = new HashMap<>(); Map<String, Object> message1 = new HashMap<>();
message1.put("role", "system"); message1.put("role", "system");
message1.put("content", "模仿语音助手,对用户的问题给出简短的回答"); String prompt = "模仿语音助手,对用户的问题给出简短的回答";
if ("Chinese".equals(language)) {
prompt += "用中文回答";
} else if ("English".equals(language)) {
prompt += "用英文回答";
}
message1.put("content", prompt);
Map<String, Object> message2 = new HashMap<>(); Map<String, Object> message2 = new HashMap<>();
message2.put("role", "user"); message2.put("role", "user");
message2.put("content", userInput); message2.put("content", userInput);
@@ -59,10 +65,7 @@ public class AssistantService {
} }
JSONArray choices = result.getJSONArray("choices"); JSONArray choices = result.getJSONArray("choices");
JSONObject message = choices.getJSONObject(0); JSONObject message = choices.getJSONObject(0);
JSONObject content = message.getJSONObject("message"); return message.getString("message");
Map<String, Object> map = new HashMap<>();
map.put("content", content.get("content"));
return map;
} }
public String textToSpeech(String content) { public String textToSpeech(String content) {
@@ -84,7 +87,7 @@ public class AssistantService {
} }
} }
public String speechRecognition(String content, Integer length) { public String speechRecognition(String content, Integer length, String language) {
try{ try{
Credential cred = new Credential(secretId, secretKey); Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile(); HttpProfile httpProfile = new HttpProfile();
@@ -93,7 +96,11 @@ public class AssistantService {
clientProfile.setHttpProfile(httpProfile); clientProfile.setHttpProfile(httpProfile);
AsrClient client = new AsrClient(cred, "", clientProfile); AsrClient client = new AsrClient(cred, "", clientProfile);
SentenceRecognitionRequest req = new SentenceRecognitionRequest(); SentenceRecognitionRequest req = new SentenceRecognitionRequest();
if ("Chinese".equals(language)) {
req.setEngSerViceType("16k_zh"); req.setEngSerViceType("16k_zh");
} else if ("English".equals(language)) {
req.setEngSerViceType("16k_en");
}
req.setSourceType(1L); req.setSourceType(1L);
req.setVoiceFormat("wav"); req.setVoiceFormat("wav");
req.setData(content); req.setData(content);
@@ -143,15 +150,16 @@ public class AssistantService {
return String.valueOf(message.getJSONObject("message")); return String.valueOf(message.getJSONObject("message"));
} }
public Map<String, Object> getResponse(String query) { public Map<String, Object> getResponse(String query, String language) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
String tag = extractTag(query); String tag = extractTag(query);
String content; String content;
content = searchGuidance(tag); content = searchGuidance(tag);
if (content != null) { if (content == null) {
result.put("content", content); content = getAiResponse(query, language);
return result;
} }
return getAiResponse(query); Map<String, Object> map = new HashMap<>();
map.put("content", textToSpeech(content));
return map;
} }
} }