|
|
|
|
@@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|