feat: 语音识别和预设回答

This commit is contained in:
ivmiku
2024-08-24 18:18:37 +08:00
parent d6492a9657
commit 7deaba288b
5 changed files with 115 additions and 19 deletions

View File

@@ -18,11 +18,6 @@
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@@ -31,10 +26,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-spring-boot3-starter</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
@@ -108,5 +104,10 @@
<artifactId>tencentcloud-sdk-java-tts</artifactId>
<version>3.1.1076</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-asr</artifactId>
<version>3.1.1083</version>
</dependency>
</dependencies>
</project>

View File

@@ -24,11 +24,4 @@ public class AssistantController {
}
return Result.ok(map);
}
@GetMapping("/tts")
public Object textToSpeech(@RequestParam String input) {
Map<String, Object> map = new HashMap<>();
map.put("content", assistantService.textToSpeech(input));
return Result.ok(map);
}
}

View File

@@ -0,0 +1,14 @@
package com.ivmiku.tutorial.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("guidance")
@Data
public class Guidance {
@TableId
private String id;
private String tag;
private String content;
}

View File

@@ -0,0 +1,8 @@
package com.ivmiku.tutorial.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ivmiku.tutorial.entity.Guidance;
public interface GuidanceMapper extends BaseMapper<Guidance> {
}

View File

@@ -2,11 +2,15 @@ package com.ivmiku.tutorial.service;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ivmiku.tutorial.entity.Guidance;
import com.ivmiku.tutorial.mapper.GuidanceMapper;
import com.ivmiku.tutorial.utils.SnowflakeUtil;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.SentenceRecognitionRequest;
import com.tencentcloudapi.asr.v20190614.models.SentenceRecognitionResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
@@ -14,18 +18,23 @@ import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tts.v20190823.TtsClient;
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceRequest;
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceResponse;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AssistantService {
@Resource
private GuidanceMapper guidanceMapper;
private final String auth = "bad1f6cad39a4c26aa9fe9e4324e096f:ZDczMDIwOTg3NjlhODdmYWVjYTY0YjM1";
private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50";
private final String secretKey = "KajjcNyNaaUCqQroqpzNoMtTHNj4Lbil";
public Map<String, Object> getResponse(String userInput) {
public Map<String, Object> getAiResponse(String userInput) {
Map<String, Object> message1 = new HashMap<>();
message1.put("role", "system");
message1.put("content", "模仿语音助手,对用户的问题给出简短的回答");
@@ -74,4 +83,75 @@ public class AssistantService {
throw new RuntimeException(e);
}
}
public String speechRecognition(String content, Integer length) {
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("asr.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AsrClient client = new AsrClient(cred, "", clientProfile);
SentenceRecognitionRequest req = new SentenceRecognitionRequest();
req.setEngSerViceType("16k_zh");
req.setSourceType(1L);
req.setVoiceFormat("wav");
req.setData(content);
req.setDataLen(Long.valueOf(length));
SentenceRecognitionResponse resp = client.SentenceRecognition(req);
return resp.getResult();
} catch (TencentCloudSDKException e) {
throw new RuntimeException(e);
}
}
public String searchGuidance(String tag) {
QueryWrapper<Guidance> queryWrapper = new QueryWrapper<>();
queryWrapper.like("tag", tag);
List<Guidance> list = guidanceMapper.selectList(queryWrapper);
if (list.isEmpty()) {
return null;
}
return list.getFirst().getContent();
}
public String extractTag(String query) {
Map<String, Object> message1 = new HashMap<>();
message1.put("role", "system");
message1.put("content", "根据给出的句子的意思,从以下标签中返回最匹配的一个:安全带、安检、飞机");
Map<String, Object> message2 = new HashMap<>();
message2.put("role", "user");
message2.put("content", query);
JSONArray array = new JSONArray();
array.add(message1);
array.add(message2);
JSONObject params = new JSONObject();
params.put("model", "general");
params.put("messages", array);
HttpResponse response = HttpRequest.post("https://spark-api-open.xf-yun.com/v1/chat/completions")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + auth)
.body(params.toJSONString())
.execute();
JSONObject result = JSONObject.parseObject(response.body());
response.close();
if (result.getInteger("code") != 0) {
return null;
}
JSONArray choices = result.getJSONArray("choices");
JSONObject message = choices.getJSONObject(0);
return String.valueOf(message.getJSONObject("message"));
}
public Map<String, Object> getResponse(String query) {
Map<String, Object> result = new HashMap<>();
String tag = extractTag(query);
String content;
content = searchGuidance(tag);
if (content != null) {
result.put("content", content);
return result;
}
return getAiResponse(query);
}
}