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;
@GetMapping("/response")
public Object getResponse(@RequestParam String input) {
Map<String, Object> map = assistantService.getResponse(input);
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);
if (map == null) {
return Result.error("请求出错");
}

View File

@@ -34,10 +34,16 @@ public class AssistantService {
private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50";
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<>();
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<>();
message2.put("role", "user");
message2.put("content", userInput);
@@ -59,10 +65,7 @@ public class AssistantService {
}
JSONArray choices = result.getJSONArray("choices");
JSONObject message = choices.getJSONObject(0);
JSONObject content = message.getJSONObject("message");
Map<String, Object> map = new HashMap<>();
map.put("content", content.get("content"));
return map;
return message.getString("message");
}
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{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
@@ -93,7 +96,11 @@ public class AssistantService {
clientProfile.setHttpProfile(httpProfile);
AsrClient client = new AsrClient(cred, "", clientProfile);
SentenceRecognitionRequest req = new SentenceRecognitionRequest();
req.setEngSerViceType("16k_zh");
if ("Chinese".equals(language)) {
req.setEngSerViceType("16k_zh");
} else if ("English".equals(language)) {
req.setEngSerViceType("16k_en");
}
req.setSourceType(1L);
req.setVoiceFormat("wav");
req.setData(content);
@@ -143,15 +150,16 @@ public class AssistantService {
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<>();
String tag = extractTag(query);
String content;
content = searchGuidance(tag);
if (content != null) {
result.put("content", content);
return result;
if (content == null) {
content = getAiResponse(query, language);
}
return getAiResponse(query);
Map<String, Object> map = new HashMap<>();
map.put("content", textToSpeech(content));
return map;
}
}