feat: 语音助手相关api

This commit is contained in:
ivmiku
2024-08-12 20:18:10 +08:00
parent e2a408488f
commit 14e9974c77
9 changed files with 210 additions and 5 deletions

View File

@@ -0,0 +1,33 @@
package com.ivmiku.tutorial.config;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Sa-Token 权限认证 配置类
*/
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
// 注册 Sa-Token 全局过滤器
@Bean
public SaServletFilter getSaServletFilter() {
return new SaServletFilter()
.addInclude("/**")
.addExclude("/favicon.ico")
.setAuth(obj -> {
// 校验 Same-Token 身份凭证 —— 以下两句代码可简化为SaSameUtil.checkCurrentRequestToken();
String token = SaHolder.getRequest().getHeader(SaSameUtil.SAME_TOKEN);
SaSameUtil.checkToken(token);
})
.setError(e -> {
return SaResult.error(e.getMessage());
})
;
}
}

View File

@@ -0,0 +1,34 @@
package com.ivmiku.tutorial.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ivmiku.tutorial.response.Result;
import com.ivmiku.tutorial.service.AssistantService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/assistant")
@SaCheckLogin
public class AssistantController {
@Resource
private AssistantService assistantService;
@GetMapping("/response")
public Object getResponse(@RequestParam String input) {
Map<String, Object> map = assistantService.getResponse(input);
if (map == null) {
return Result.error("请求出错");
}
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

@@ -34,6 +34,9 @@ public class NavigateController {
String userId = (String) StpUtil.getLoginId();
if (!origin.isEmpty() && !destination.isEmpty()) {
Map<String, Object> route = navigateService.getRoute(origin, destination);
if (route == null) {
return JSON.toJSON(Result.error("路线规划失败!"));
}
List<RouteStep> stepsList = (List<RouteStep>) route.get("steps");
STEPS_MAP.put(userId, stepsList);
Map<String, Object> map = new HashMap<>();
@@ -50,13 +53,24 @@ public class NavigateController {
map.put("step", STEPS_MAP.get(userId).get(step-1));
return JSON.toJSON(Result.ok(map));
}
return JSON.toJSON(Result.error("非法的请求参数"));
return Result.error("非法的请求参数");
}
@PostMapping("/ticket")
public Object scanTicket(@RequestPart MultipartFile file) throws IOException {
BufferedImage image = ImageIO.read(file.getInputStream());
Map<String, Object> map = navigateService.scanTicket(image);
return JSON.toJSON(Result.ok(map));
return Result.ok(map);
}
@GetMapping("/geocode")
public Object getGeoCode(@RequestParam String address, @RequestParam(defaultValue = "") String city) {
String geoCode = navigateService.getGeoCode(address, city);
if (geoCode == null) {
return JSON.toJSON(Result.error("未获取到有效信息"));
}
Map<String, Object> map = new HashMap<>();
map.put("geocode", geoCode);
return Result.ok(map);
}
}

View File

@@ -0,0 +1,77 @@
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.ivmiku.tutorial.utils.SnowflakeUtil;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
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 org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class AssistantService {
private final String auth = "bad1f6cad39a4c26aa9fe9e4324e096f:ZDczMDIwOTg3NjlhODdmYWVjYTY0YjM1";
private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50";
private final String secretKey = "KajjcNyNaaUCqQroqpzNoMtTHNj4Lbil";
public Map<String, Object> getResponse(String userInput) {
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", userInput);
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);
JSONObject content = message.getJSONObject("message");
Map<String, Object> map = new HashMap<>();
map.put("content", content.get("content"));
return map;
}
public String textToSpeech(String content) {
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("tts.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
TtsClient client = new TtsClient(cred, "ap-shanghai", clientProfile);
TextToVoiceRequest req = new TextToVoiceRequest();
req.setText(content);
req.setSessionId(SnowflakeUtil.getNext());
req.setVoiceType(101006L);
TextToVoiceResponse resp = client.TextToVoice(req);
return resp.getAudio();
} catch (TencentCloudSDKException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -36,7 +36,11 @@ public class NavigateService {
.execute();
String result = response.body();
response.close();
JSONObject route = JSONObject.parseObject(JSONObject.parseObject(result).getString("route"));
JSONObject resultObj = JSONObject.parseObject(result);
if (resultObj.getInteger("status")==0) {
return null;
}
JSONObject route = JSONObject.parseObject(resultObj.getString("route"));
JSONArray paths = JSONArray.parseArray(route.getString("paths"));
JSONObject path = paths.getJSONObject(0);
String distance = path.getString("distance");
@@ -81,4 +85,25 @@ public class NavigateService {
}
return result;
}
public String getGeoCode(String address, String city) {
Map<String, Object> params = new HashMap<>();
params.put("key", key);
params.put("address", address);
if (!city.isEmpty()) {
params.put("city", city);
}
HttpResponse response = HttpRequest.get("https://restapi.amap.com/v3/geocode/geo")
.form(params)
.execute();
String result = response.body();
response.close();
JSONObject resultObj = JSONObject.parseObject(result);
if (resultObj.getInteger("status")==0) {
return null;
}
JSONArray array = resultObj.getJSONArray("geocodes");
JSONObject location = array.getJSONObject(0);
return location.getString("location");
}
}

View File

@@ -0,0 +1,14 @@
package com.ivmiku.tutorial.utils;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
public class SnowflakeUtil {
private static SnowflakeGenerator generator;
static {
generator = new SnowflakeGenerator();
}
public static String getNext() {
return String.valueOf(generator.next());
}
}