本方案阐述了如何通过定制热词功能,为语音识别模型提供一个优先识别的词汇列表,以显著提升特定业务术语、人名、地名等专有词汇的识别准确率。其核心是创建一个包含关键词及其权重的热词列表,并在发起识别请求时引用该表,从而引导模型对这些词汇进行识别倾向性增强。
业务场景说明
在金融、医疗、法律或媒体等专业领域,标准的语音识别模型可能难以准确识别行业术语、产品名称或特定人名。例如,在一部关于孔子的纪录片中,语音“游览曲阜阙里人家”可能被错误识别为“游览曲阜缺里人家”。这种错误不仅影响内容的可读性,还可能导致信息失实,增加后期人工校对的成本与工作量。
通过实施定制热词方案,目标是让语音识别服务能够准确识别出“阙里人家”这类专有词汇,确保转写结果的精确性,从而实现自动化流程的可靠性。
快速开始
在开始之前,需要完成SDK的安装并配置好API访问凭证。
以下示例代码展示如何创建一个热词列表,并通过实时接口调用paraformer-realtime-v2模型识别本地音频文件。
import os
import dashscope
from dashscope.audio.asr import VocabularyService, Recognition
# 从环境变量获取API Key
# 若没有配置环境变量,请用百炼API Key将下行替换为:dashscope.api_key = "sk-xxx"
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
def run_hotword_recognition_workflow():
"""一个完整的创建、使用和管理热词表的示例。"""
service = VocabularyService()
vocabulary_id = None
# 1. 定义热词数据和目标模型
target_model = "paraformer-realtime-v2"
my_vocabulary_data = [
{"text": "阿里云", "weight": 4, "lang": "zh"},
{"text": "DashScope", "weight": 4, "lang": "en"},
{"text": "语音识别", "weight": 3, "lang": "zh"}
]
try:
# 2. 创建热词表
# SDK的create_vocabulary方法是同步的,它会等待异步任务完成后再返回ID
print("正在创建热词表...")
vocabulary_id = service.create_vocabulary(
prefix="mycompany", # 自定义前缀,用于分类管理
target_model=target_model,
vocabulary=my_vocabulary_data
)
print(f"热词表创建成功,ID: {vocabulary_id}")
# 3. 查询并验证热词表内容
retrieved_vocab = service.query_vocabulary(vocabulary_id)
print("查询到的热词表内容:", retrieved_vocab)
# 4. 使用热词表进行语音识别
# 确保有一个名为 test_audio.wav 的16kHz采样率的音频文件
print("\n正在使用热词表进行识别...")
recognition = Recognition(
model=target_model,
format='wav',
sample_rate=16000,
callback=None,
vocabulary_id=vocabulary_id,
language_hints=['zh', 'en'] # 激活中英文热词
)
# [需要确认:请确保本地存在名为 test_audio.wav 的音频文件]
result = recognition.call('asr_example.wav')
print("识别结果:", result.output)
except Exception as e:
print(f"操作过程中发生错误: {e}")
finally:
# 5. 清理资源(在生产环境中根据需要决定是否删除)
if vocabulary_id:
print(f"\n准备删除热词表: {vocabulary_id}")
service.delete_vocabulary(vocabulary_id)
print("热词表已删除。")
pass
if __name__ == "__main__":
run_hotword_recognition_workflow()import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
import com.alibaba.dashscope.exception.ApiException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Main {
// 若没有配置环境变量,请用百炼API Key将下行替换为:private static final String API_KEY = "sk-xxx";
private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY");
public static void main(String[] args) {
String targetModel = "paraformer-realtime-v2";
String vocabularyId = null;
VocabularyService service = new VocabularyService(API_KEY);
try {
// 1. 准备热词数据
JsonArray vocabulary = new JsonArray();
List<Hotword> wordList = new ArrayList<>();
wordList.add(new Hotword("吴贻弓", 4, "zh"));
wordList.add(new Hotword("阙里人家", 4, "zh"));
for (Hotword word : wordList) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("text", word.text);
jsonObject.addProperty("weight", word.weight);
jsonObject.addProperty("lang", word.lang);
vocabulary.add(jsonObject);
}
// 2. 创建热词表
System.out.println("正在创建热词表...");
Vocabulary myVocabulary = service.createVocabulary(
targetModel,
"mycompany",
vocabulary
);
vocabularyId = myVocabulary.getVocabularyId();
System.out.println("热词表创建成功,ID: " + vocabularyId);
// 3. 配置识别参数
RecognitionParam param = RecognitionParam.builder()
.model("paraformer-realtime-v2")
.format("wav")
.sampleRate(16000)
.apiKey(API_KEY)
.vocabularyId(vocabularyId)
// “language_hints”只支持paraformer-v2和paraformer-realtime-v2模型
.parameter("language_hints", new String[] {"zh"})
.build();
// 4. 执行识别
// [需要确认:请确保项目根目录存在名为 test_audio.wav 的音频文件]
System.out.println("\n正在使用热词表进行识别...");
Recognition recognizer = new Recognition();
String resultJson = recognizer.call(param, new File("asr_example.wav"));
System.out.println("识别结果: " + resultJson);
} catch (ApiException e) {
System.err.println("API 操作失败: " + e.getMessage());
System.err.println("HTTP状态码: " + e.getStatus());
} catch (Exception e) {
System.err.println("发生未知错误: " + e.getMessage());
e.printStackTrace();
} finally {
// 5. 清理资源
if (vocabularyId != null) {
System.out.println("\n准备删除热词表: " + vocabularyId);
try {
service.deleteVocabulary(vocabularyId);
System.out.println("热词表已删除。");
} catch (Exception e) {
System.err.println("删除热词表失败: " + e.getMessage());
}
}
System.exit(0);
}
}
static class Hotword {
String text;
int weight;
String lang;
public Hotword(String text, int weight, String lang) {
this.text = text;
this.weight = weight;
this.lang = lang;
}
}
}重要限制和注意事项
配额限制
每个热词列表最多添加500个词。
每个账号默认分配10个热词列表额度,如果有需要创建更多请联系我们。
模型支持情况
实时语音识别:paraformer-realtime-v2,paraformer-realtime-8k-v2,fun-asr-realtime,fun-asr-realtime-2025-09-15
录音文件识别:paraformer-v2,paraformer-8k-v2,fun-asr,fun-asr-2025-08-25,fun-asr-mtl,fun-asr-mtl-2025-08-25
计费说明
目前,定制热词功能的创建和使用均不产生额外费用。
功能详解
定制热词功能的实现流程分为两个主要阶段:热词列表的创建与管理,以及在语音识别任务中应用热词列表。
热词列表创建与管理:开发者通过SDK或RESTful API(参见热词API详情)提交一个包含特定词汇和权重的列表。服务接收后,会返回热词列表 ID (
vocabulary_id)。此 ID 是后续调用识别服务的凭证。热词列表是一个JSON数组,每个对象代表一个热词,包含如下字段:
参数
类型
是否必填
说明
text
string
是
热词文本。限制规则:
• 中文:不超过15个汉字
• 英文:不超过7个单词(按空格分词)
• 中英混合:汉字与英文字母总数不超过15个
weight
int
是
热词权重,取值范围[1-5]。
• 推荐起始值:3或4
• 权重过高(5)可能导致过度匹配,影响其他词汇识别准确性
• 建议根据验证集效果逐步调整
lang
string
否
语言代码。具体语种和语言代码对应关系请参考语音识别模型API。
必须与调用语音识别服务时的
language_hints参数匹配才能生效。示例:电影名称热词列表 以下示例展示了如何为中英文电影名称配置热词(共6个热词)。
[ {"text": "赛德克巴莱", "weight": 4, "lang": "zh"}, {"text": "Seediq Bale", "weight": 4, "lang": "en"}, {"text": "夏洛特烦恼", "weight": 4, "lang": "zh"}, {"text": "Goodbye Mr. Loser", "weight": 4, "lang": "en"}, {"text": "阙里人家", "weight": 4, "lang": "zh"}, {"text": "Confucius Family", "weight": 4, "lang": "en"} ]应用热词列表进行识别:在发起语音识别请求时,将音频数据与之前获取的
vocabulary_id一并提交。语音识别引擎在进行解码时,会加载该热词列表,并根据其中定义的权重,提高相应词汇在识别候选结果中的优先级,最终输出更精确的识别文本。
创建热词列表和应用热词列表进行识别时使用的语音识别模型必须相同。
热词API详情
使用不同API时,请确保使用同一账号进行操作。
创建热词列表
Python SDK
通过VocabularyService类的create_vocabulary方法创建热词列表。VocabularyService类的引入方式:from dashscope.audio.asr import *。
create_vocabulary方法返回热词列表ID。
def create_vocabulary(self, target_model: str, prefix: str, vocabulary: List[dict]) -> str:
'''
创建热词列表
param: target_model 热词列表对应的语音识别模型版本
param: prefix 热词列表自定义前缀,仅允许数字和小写字母,小于十个字符。
param: vocabulary 热词列表
return: 热词列表标识符 vocabulary_id
'''Java SDK
通过VocabularyService类的createVocabulary方法创建热词列表。VocabularyService类的引入方式:import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;。
createVocabulary方法返回热词列表ID。
/**
* 创建新热词
*
* @param targetModel 热词对应的语音识别模型版本
* @param prefix 热词自定义前缀,仅允许数字和小写字母,小于十个字符。
* @param vocabulary 热词列表
* @return 热词列表对象
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public Vocabulary createVocabulary(String targetModel, String prefix, JsonArray vocabulary)
throws NoApiKeyException, InputRequiredException RESTful API
基本信息
URL | |
请求方法 | POST |
请求头 | |
消息体 | 包含所有请求参数的消息体如下,对于可选字段,在实际业务中可根据需求省略: |
请求参数
参数 | 类型 | 默认值 | 是否必须 | 说明 |
model | string | - | 是 | 固定为 |
action | string | - | 是 | 固定为 |
target_model | string | - | 是 | 热词列表对应的语音识别模型。 |
prefix | string | - | 是 | 热词列表自定义前缀,仅允许数字和小写字母,小于十个字符。 |
vocabulary | object[] | - | 是 | 热词列表。 |
响应参数
参数 | 类型 | 说明 |
vocabulary_id | string | 热词列表ID。 |
查询所有热词列表
Python SDK
通过VocabularyService类的list_vocabularies方法查询所有热词列表。VocabularyService类的引入方式:from dashscope.audio.asr import *。
def list_vocabularies(self, prefix=None, page_index: int = 0, page_size: int = 10) -> List[dict]:
'''
查询已创建的所有热词列表
param: prefix 自定义前缀,如果设定则只返回指定前缀的热词列表。
param: page_index 查询的页索引
param: page_size 查询页大小
return: 热词列表标识符列表
'''响应示例:
[
{
"gmt_create": "2025-04-22 14:23:35",
"vocabulary_id": "yourVocabularyId",
"gmt_modified": "2025-04-22 14:23:35",
"status": "OK"
}
]响应参数:
参数 | 类型 | 说明 |
vocabulary_id | string | 热词列表ID。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
status | string | 状态:
|
Java SDK
通过VocabularyService类的listVocabulary方法查询所有热词列表。VocabularyService类的引入方式:import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;。
/**
* 查询已创建的所有热词列表。默认的页索引为0,默认的页大小为10
*
* @param prefix 热词自定义前缀
* @return 热词列表
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public Vocabulary[] listVocabulary(String prefix)
throws NoApiKeyException, InputRequiredException
/**
* 查询已创建的所有热词列表
*
* @param prefix 热词自定义前缀
* @param pageIndex 查询的页索引
* @param pageSize 查询的页大小
* @return 热词列表
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public Vocabulary[] listVocabulary(String prefix, int pageIndex, int pageSize)
throws NoApiKeyException, InputRequiredException响应示例:
[
{
"gmt_create": "2025-04-22 14:23:35",
"vocabulary_id": "yourVocabularyId",
"gmt_modified": "2025-04-22 14:23:35",
"status": "OK"
}
]响应参数:
参数 | 类型 | 说明 |
vocabulary_id | string | 热词列表ID。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
status | string | 状态:
|
RESTful API
基本信息
URL | |
请求方法 | POST |
请求头 | |
消息体 | 包含所有请求参数的消息体如下,对于可选字段,在实际业务中可根据需求省略: |
请求参数
参数 | 类型 | 默认值 | 是否必须 | 说明 |
model | string | - | 是 | 固定为 |
action | string | - | 是 | 固定为 |
prefix | string | - | 是 | 热词列表自定义前缀,仅允许数字和小写字母,小于十个字符。 |
page_index | integer | 0 | 否 | 页码索引,从0开始计数。 |
page_size | integer | 10 | 否 | 每页包含数据条数。 |
响应参数
参数 | 类型 | 说明 |
vocabulary_id | string | 热词列表ID。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
status | string | 状态:
|
查询指定热词列表
Python SDK
通过VocabularyService类的query_vocabulary方法查询指定热词列表。VocabularyService类的引入方式:from dashscope.audio.asr import *。
def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
'''
获取热词列表内容
param: vocabulary_id 热词列表标识符
return: 热词列表
'''响应示例:
{
"gmt_create": "2025-04-22 14:23:35",
"vocabulary": [
{
"weight": 4,
"text": "吴贻弓",
"lang": "zh"
},
{
"weight": 4,
"text": "阙里人家",
"lang": "zh"
}
],
"target_model": "paraformer-realtime-v2",
"gmt_modified": "2025-04-22 14:23:35",
"status": "OK"
}响应参数:
参数 | 类型 | 说明 |
vocabulary | object[] | 热词列表。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
target_model | string | 热词列表对应的语音识别模型。 |
status | string | 状态:
|
Java SDK
通过VocabularyService类的queryVocabulary方法查询指定热词列表。VocabularyService类的引入方式:import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;。
/**
* 查询指定热词列表
*
* @param vocabularyId 需要查询的热词列表
* @return 热词列表
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public Vocabulary queryVocabulary(String vocabularyId)
throws NoApiKeyException, InputRequiredException响应示例:
{
"gmt_create": "2025-04-22 14:23:35",
"vocabulary": [
{
"weight": 4,
"text": "吴贻弓",
"lang": "zh"
},
{
"weight": 4,
"text": "阙里人家",
"lang": "zh"
}
],
"target_model": "paraformer-realtime-v2",
"gmt_modified": "2025-04-22 14:23:35",
"status": "OK"
}响应参数:
参数 | 类型 | 说明 |
vocabulary | object[] | 热词列表。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
target_model | string | 热词列表对应的语音识别模型。 |
status | string | 状态:
|
RESTful API
基本信息
URL | |
请求方法 | POST |
请求头 | |
消息体 | 包含所有请求参数的消息体如下,对于可选字段,在实际业务中可根据需求省略: |
请求参数
参数 | 类型 | 默认值 | 是否必须 | 说明 |
model | string | - | 是 | 固定为 |
action | string | - | 是 | 固定为 |
vocabulary_id | string | - | 是 | 待查询热词列表ID。 |
响应参数
参数 | 类型 | 说明 |
vocabulary | object[] | 热词列表。 |
gmt_create | string | 创建热词列表的时间。 |
gmt_modified | string | 修改热词列表的时间。 |
target_model | string | 热词列表对应的语音识别模型。 |
status | string | 状态:
|
更新热词列表
Python SDK
通过VocabularyService类的update_vocabulary方法更新热词列表。VocabularyService类的引入方式:from dashscope.audio.asr import *。
def update_vocabulary(self, vocabulary_id: str, vocabulary: List[dict]) -> None:
'''
用新的热词列表替换已有热词列表
param: vocabulary_id 需要替换的热词列表标识符
param: vocabulary 热词列表
'''Java SDK
通过VocabularyService类的updateVocabulary方法更新热词列表。VocabularyService类的引入方式:import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;。
/**
* 更新热词列表
*
* @param vocabularyId 需要更新的热词列表
* @param vocabulary 热词列表对象
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public void updateVocabulary(String vocabularyId, JsonArray vocabulary)
throws NoApiKeyException, InputRequiredExceptionRESTful API
基本信息
URL | |
请求方法 | POST |
请求头 | |
消息体 | 包含所有请求参数的消息体如下,对于可选字段,在实际业务中可根据需求省略: |
请求参数
参数 | 类型 | 默认值 | 是否必须 | 说明 |
model | string | - | 是 | 固定为 |
action | string | - | 是 | 固定为 |
vocabulary_id | string | - | 是 | 待更新热词列表ID。 |
vocabulary | object[] | - | 是 | 更新后的热词列表字典。 |
响应参数
删除热词列表
Python SDK
通过VocabularyService类的delete_vocabulary方法删除热词列表。VocabularyService类的引入方式:from dashscope.audio.asr import *。
def delete_vocabulary(self, vocabulary_id: str) -> None:
'''
删除热词列表
param: vocabulary_id 需要删除的热词列表标识符
'''Java SDK
通过VocabularyService类的deleteVocabulary方法删除热词列表。VocabularyService类的引入方式:import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;。
/**
* 删除热词列表
*
* @param vocabularyId 需要删除的热词列表
* @throws NoApiKeyException 如果apikey为空
* @throws InputRequiredException 如果必须参数为空
*/
public void deleteVocabulary(String vocabularyId)
throws NoApiKeyException, InputRequiredExceptionRESTful API
基本信息
URL | |
请求方法 | POST |
请求头 | |
消息体 | 包含所有请求参数的消息体如下,对于可选字段,在实际业务中可根据需求省略: |
请求参数
参数 | 类型 | 默认值 | 是否必须 | 说明 |
model | string | - | 是 | 固定为 |
action | string | - | 是 | 固定为 |
vocabulary_id | string | - | 是 | 待删除热词列表ID。 |
响应参数
错误码说明
如遇报错问题,请参见错误信息进行排查。