科大讯飞【免费】的开源模型实现录音转写与角色判定
在智能客服、语音分析等场景中,我们经常需要从对话记录中自动识别不同说话者的身份角色。本文将介绍如何利用大语言模型(LLM)实现对话人身份判定,将原始的Speaker标识转换为具体的业务角色。
技术架构概述
我们的身份识别系统主要包含以下几个核心组件:
语音识别(ASR)模块:将语音转换为文本
身份判定引擎:基于大模型的智能角色识别
JSON格式化输出:标准化的结果返回
核心代码实现
1. 主控类设计
public class Model_1_7B {public static final Gson gson = new Gson();public static final String MaaS_KEY = "XXX";// 主方法 - 测试入口public static void main(String[] args) {String asrRes = "Speaker1:喂。*******************************************************************************Speaker2:你好。*******************************************************************************Speaker2:您好,您这边是需要恩施地区的二手车源是吗?";String finalQuestion = buildIdentityQuestion(asrRes);String result = useDeepSeek(finalQuestion, MaaS_KEY);System.out.println("身份识别结果: " + result);}// 身份判定核心方法public static String identityDetermination(String asrRes) {String finalQuestion = buildIdentityQuestion(asrRes);return useDeepSeek(finalQuestion, MaaS_KEY);}// 构建提示词private static String buildIdentityQuestion(String asrRes) {return "你的角色是:对话人身份判定专家。\n"+ "你的任务是:根据对话记录,判断Speaker的身份,严格按照此JSON返回{\"speaker1\":\"xxx\",\"speaker2\":\"xxx\"}。\n"+ "我得要求:已知speaker身份有【二手车收车商、车源线索销售人员】\n"+ "根据以上要求,我输入的关键词是:" + asrRes;} }
2. 大模型API调用模块
public static String useDeepSeek(String question, String maasKey) {String url = "http://maas-api.cn-huabei-1.xf-yun.com/v1/chat/completions";try {URL apiUrl = new URL(url);HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();// 设置连接参数connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Authorization", "Bearer " + maasKey);connection.setDoOutput(true);connection.setConnectTimeout(30000);connection.setReadTimeout(30000);// 构建请求体JSONObject message = new JSONObject();message.put("role", "user");message.put("content", question);JSONArray messagesArray = new JSONArray();messagesArray.put(message);JSONObject payload = new JSONObject();payload.put("model", "xop3qwen1b7");payload.put("messages", messagesArray);payload.put("stream", false);payload.put("max_tokens", 8192);// 发送请求try (OutputStream os = connection.getOutputStream()) {byte[] input = payload.toString().getBytes(StandardCharsets.UTF_8);os.write(input, 0, input.length);}// 处理响应int responseCode = connection.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) {try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {StringBuilder response = new StringBuilder();String line;while ((line = reader.readLine()) != null) {response.append(line);}// 解析大模型返回结果JsonParse jsonParse = gson.fromJson(response.toString(), JsonParse.class);List<Choices> choiceList = jsonParse.choices;for (Choices tempChoice : choiceList) {System.out.println("模型推理内容: " + tempChoice.message.reasoning_content);System.out.println("最终回答: " + tempChoice.message.content);return tempChoice.message.content;}}} else {// 错误处理handleErrorResponse(connection, responseCode);}connection.disconnect();} catch (Exception e) {System.out.println("An error occurred: " + e.getMessage());return "An error occurred: " + e.getMessage();}return "身份区分大模型调用错误"; }
3. 响应数据结构
// 大模型响应解析类 static class JsonParse {List<Choices> choices; }static class Choices {Message message; }static class Message {String reasoning_content; // 模型推理过程String content; // 最终输出内容 }
关键技术点
1. 提示词工程(Prompt Engineering)
我们设计了专门的提示词来引导大模型完成身份识别任务:
角色定义:明确模型作为"对话人身份判定专家"
任务说明:清晰描述需要完成的具体任务
输出格式:严格指定JSON返回格式
身份选项:限定可选的说话者身份类型
2. 错误处理机制
private static void handleErrorResponse(HttpURLConnection connection, int responseCode) {try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {StringBuilder errorResponse = new StringBuilder();String line;while ((line = reader.readLine()) != null) {errorResponse.append(line);}System.out.println("HTTP Error: " + responseCode + " - " + errorResponse.toString());} catch (IOException e) {System.out.println("Error reading error stream: " + e.getMessage());} }
应用示例
输入对话文本:
Speaker1:喂。 ******************************************************************************* Speaker2:你好。 ******************************************************************************* Speaker2:您好,您这边是需要恩施地区的二手车源是吗?
大模型返回结果:
{"speaker1":"二手车收车商","speaker2":"车源线索销售人员"}
转换后结果:
二手车收车商:喂。 ******************************************************************************* 车源线索销售人员:你好。 ******************************************************************************* 车源线索销售人员:您好,您这边是需要恩施地区的二手车源是吗?
优势与特点
高准确性:利用大语言模型的深层语义理解能力
灵活可配置:通过修改提示词即可适应不同的业务场景
标准化输出:统一的JSON格式便于后续处理
实时处理:支持流式处理,满足实时应用需求
应用场景
智能客服系统:自动识别客户和客服人员对话
语音分析平台:批量处理录音文件并标注说话者身份
质量检测:监控客服对话质量,分析服务流程
数据标注:自动化生成训练数据,减少人工标注成本
总结
本文介绍了一套基于大语言模型的对话人身份识别系统,通过精心设计的提示词和稳定的API调用,实现了从原始对话到角色标注的自动化转换。这种方法不仅提高了处理效率,还保证了识别准确性,为相关领域的应用提供了有力的技术支撑。
该方案具有良好的可扩展性,通过调整提示词中的身份选项,可以轻松适配到金融、医疗、教育等不同行业的对话分析场景中。