全部產品
Search
文件中心

Alibaba Cloud Model Studio:定製熱詞

更新時間:Jan 31, 2026

在語音辨識任務中,當特定的業務詞彙(如產品名、專有名詞)識別不準確時,可使用熱詞功能。通過將這些詞彙添加為熱詞,可以優先處理它們,從而提升最終的識別準確率。

熱詞簡介

熱詞功能通過提交一個JSON數組格式的詞彙列表來提升特定詞的識別準確率。數組中的每個對象用於定義一個熱詞及其屬性。

樣本:提升電影名稱識別率(適用於 Fun-ASR 和 Paraformer 系列模型)

[
    {"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"}
]

欄位說明

欄位

類型

是否必填

說明

text

string

支援

熱詞文本。

熱詞文本的語言必須在所選模型的支援範圍內,不同模型支援的語言各不相同。

熱詞用於提升識別/翻譯的準確率,因此請使用實際詞語而非任一字元組合。熱詞文本長度限制規則如下:

  • 含非ASCII字元時:字串的總字元數(包括漢字、日文假名、韓文諺文、俄文西裡爾字母等非ASCII字元以及ASCII字元)不能超過15個。

    樣本:

    • ✅ "厄洛替尼鹽酸鹽"(7字元)

    • ✅ "EGFR抑製劑"(7字元,其中“EGFR”是ASCII字元,長度為4)

    • ✅ "こんにちは"(5字元)

    • ✅ "Фенибут Белфарм"(15字元,中間的空格也被計入)

    • ❌ "Клофелин Белмедпрепараты"(24字元)

  • 純ASCII字元時:字串被空格分割的片段數不能超過7個。每個片段是由空格分隔的連續ASCII字元序列。

    樣本:

    • ✅ "Exothermic reaction" → 2個片段

    • ✅ "Human immunodeficiency virus type 1" → 5個片段

    • ❌ "The effect of temperature variations on enzyme activity in biochemical reactions" → 11個片段

weight

int

支援

熱詞權重。常用值:4。

取值範圍:[1, 5]。

如果效果不明顯可以適當增加權重,但是當權重較大時可能會引起負面效果,導致其他詞語識別不準確。

lang

string

不支援

語言代碼,支援對ASR模型對應的語種做熱詞加強。如果無法提前確定語種,可不設定,模型會自動識別語種。

具體語種和語言代碼對應關係請參考模型的API詳情頁。請在調用語音辨識服務使用指定同樣的語種,如果指定識別語種language_hints後其他語種熱詞會失效。

支援的模型

國際(新加坡)

Fun-ASR:

  • 即時語音辨識:fun-asr-realtime、fun-asr-realtime-2025-11-07

  • 錄音檔案識別:fun-asr、fun-asr-2025-11-07、fun-asr-2025-08-25、fun-asr-mtl、fun-asr-mtl-2025-08-25

中國內地(北京)

  • Fun-ASR:

    • 即時語音辨識:fun-asr-realtime、fun-asr-realtime-2025-11-07、fun-asr-realtime-2025-09-15

    • 錄音檔案識別:fun-asr、fun-asr-2025-11-07、fun-asr-2025-08-25、fun-asr-mtl、fun-asr-mtl-2025-08-25

  • Paraformer:

    • 即時語音辨識:paraformer-realtime-v2、paraformer-realtime-8k-v2

    • 錄音檔案識別:paraformer-v2、paraformer-8k-v2

計費說明

熱詞功能免費。

熱詞數量限制

  1. 每個帳號可建立10個熱詞列表(所有模型共用額度),如需擴容請進行申請

  2. 每個熱詞列表最多可添加500個熱詞

快速開始:從建立熱詞到使用

工作流程

建立熱詞列表與使用熱詞列表進行語音辨識是緊密關聯的兩個獨立步驟,遵循“先建立,後使用”的流程:

  1. 建立熱詞列表

    調用建立熱詞列表介面。此步驟必須指定target_model(Java中為targetModel),聲明建立的熱詞列表將由哪個語音辨識模型使用。

    若已有建立好的熱詞列表(調用查詢所有熱詞列表介面查看),可跳過這一步直接進行下一步。

  2. 使用熱詞列表進行語音辨識。

    調用語音辨識介面,傳入熱詞列表ID。此步驟指定的語音辨識模型必須和建立熱詞列表時指定的target_model(Java中為targetModel)一致。

準備工作

  1. 擷取API Key擷取API Key,為安全起見,推薦將API Key配置到環境變數。

  2. 安裝SDK:確保已安裝最新版DashScope SDK

範例程式碼

樣本中用到的音頻為:asr_example.wav

Python

import dashscope
from dashscope.audio.asr import *
import os


# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')

# 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

# 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:wss://dashscope.aliyuncs.com/api-ws/v1/inference
dashscope.base_websocket_api_url='wss://dashscope-intl.aliyuncs.com/api-ws/v1/inference'
prefix = 'testpfx'
target_model = "fun-asr-realtime"

my_vocabulary = [
    {"text": "語音實驗室", "weight": 4}
]

service = VocabularyService()
vocabulary_id = service.create_vocabulary(
      prefix=prefix,
      target_model=target_model,
      vocabulary=my_vocabulary)

if service.query_vocabulary(vocabulary_id)['status'] == 'OK':
    recognition = Recognition(model=target_model,
                          format='wav',
                          sample_rate=16000,
                          callback=None,
                          vocabulary_id=vocabulary_id)
    result = recognition.call('asr_example.wav')
    print(result.output)

service.delete_vocabulary(vocabulary_id)

Java

import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
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不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
    public static String apiKey = System.getenv("DASHSCOPE_API_KEY");

    public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
        // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
        Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
        // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:wss://dashscope.aliyuncs.com/api-ws/v1/inference
        Constants.baseWebsocketApiUrl = "wss://dashscope-intl.aliyuncs.com/api-ws/v1/inference";

        String targetModel = "fun-asr-realtime";

        JsonArray vocabularyJson = new JsonArray();
        List<Hotword> wordList = new ArrayList<>();
        wordList.add(new Hotword("語音實驗室", 4));

        for (Hotword word : wordList) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("text", word.text);
            jsonObject.addProperty("weight", word.weight);
            vocabularyJson.add(jsonObject);
        }

        VocabularyService service = new VocabularyService(apiKey);
        Vocabulary vocabulary = service.createVocabulary(targetModel, "testpfx", vocabularyJson);

        if ("OK".equals(service.queryVocabulary(vocabulary.getVocabularyId()).getStatus())) {
            Recognition recognizer = new Recognition();
            // 建立RecognitionParam
            RecognitionParam param =
                    RecognitionParam.builder()
                            .model(targetModel)
                            .apiKey(apiKey)
                            .format("wav")
                            .sampleRate(16000)
                            .build();

            try {
                System.out.println("識別結果:" + recognizer.call(param, new File("asr_example.wav")));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 任務結束後關閉 WebSocket 串連
                recognizer.getDuplexApi().close(1000, "bye");
            }
        }

        service.deleteVocabulary(vocabulary.getVocabularyId());
        System.exit(0);
    }
}

class Hotword {
    String text;
    int weight;

    public Hotword(String text, int weight) {
        this.text = text;
        this.weight = weight;
    }
}

API參考

使用不同API時,請確保使用同一帳號進行操作。

建立熱詞列表

熱詞列表JSON的格式請參見熱詞簡介

Python SDK

  • 介面說明

    重要

    target_model:使用熱詞列表的語音辨識模型,須和後續調用語音辨識介面時使用的語音辨識模型一致

    def create_vocabulary(self, target_model: str, prefix: str, vocabulary: List[dict]) -> str:
        '''
        建立熱詞列表
        param: target_model 熱詞列表對應的語音辨識模型,必須與後續調用語音辨識介面時使用的語音辨識模型一致
        param: prefix 熱詞列表自訂首碼,僅允許數字和小寫字母,小於十個字元。
        param: vocabulary 熱詞列表JSON
        return: 熱詞列表ID
        '''
  • 範例程式碼

    import dashscope
    from dashscope.audio.asr import *
    import os
    
    # 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
    dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
    
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    prefix = 'testpfx'
    target_model = "fun-asr"
    
    my_vocabulary = [
        {"text": "賽德克巴萊", "weight": 4}
    ]
    
    # 建立熱詞
    service = VocabularyService()
    vocabulary_id = service.create_vocabulary(
        prefix=prefix,
        target_model=target_model,
        vocabulary=my_vocabulary)
    
    print(f"熱詞列表ID為:{vocabulary_id}")

Java SDK

  • 介面說明

    重要

    targetModel:使用熱詞列表的語音辨識模型,須和後續調用語音辨識介面時使用的語音辨識模型一致

    /**
     * 建立新的熱詞列表
     *
     * @param targetModel 熱詞列表對應的語音辨識模型,必須與後續調用語音辨識介面時使用的語音辨識模型一致
     * @param prefix 熱詞自訂首碼,僅允許數字和小寫字母,小於十個字元。
     * @param vocabulary 熱詞列表JSON
     * @return 熱詞列表對象
     * @throws NoApiKeyException 如果apikey為空白
     * @throws InputRequiredException 如果必須參數為空白
     */
    public Vocabulary createVocabulary(String targetModel, String prefix, JsonArray vocabulary)
        throws NoApiKeyException, InputRequiredException 
  • 範例程式碼

    import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
    import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
    import com.alibaba.dashscope.exception.InputRequiredException;
    import com.alibaba.dashscope.exception.NoApiKeyException;
    import com.alibaba.dashscope.utils.Constants;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        // 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
        public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
    
        public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
            // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
            Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
            String targetModel = "fun-asr";
    
            JsonArray vocabularyJson = new JsonArray();
            List<Hotword> wordList = new ArrayList<>();
            wordList.add(new Hotword("吳貽弓", 4));
            wordList.add(new Hotword("闕裡人家", 4));
    
            for (Hotword word : wordList) {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("text", word.text);
                jsonObject.addProperty("weight", word.weight);
                vocabularyJson.add(jsonObject);
            }
    
            VocabularyService service = new VocabularyService(apiKey);
            Vocabulary vocabulary = service.createVocabulary(targetModel, "testpfx", vocabularyJson);
            System.out.println("熱詞列表ID:" + vocabulary.getVocabularyId());
        }
    }
    
    class Hotword {
        String text;
        int weight;
        String lang;
    
        public Hotword(String text, int weight) {
            this.text = text;
            this.weight = weight;
        }
    }

RESTful API

  • URL

    中國內地(北京):

    POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization

    國際(新加坡):

    POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization
  • 要求標頭

    參數

    類型

    是否必須

    說明

    Authorization

    string

    支援

    鑒權令牌,格式為Bearer <your_api_key>,使用時,將“<your_api_key>”替換為實際的API Key。

    Content-Type

    string

    支援

    請求體中傳輸的資料的媒體類型。固定為application/json

  • 訊息體

    包含所有請求參數的訊息體如下,對於可選欄位,在實際業務中可根據需求省略。

    重要

    注意區分如下參數:

    • model:定製熱詞模型,固定為speech-biasing

    • target_model:使用熱詞列表的語音辨識模型,須和後續調用語音辨識介面時使用的語音辨識模型一致

    {
        "model": "speech-biasing",
        "input": {
            "action": "create_vocabulary",
            "target_model": "fun-asr",
            "prefix": "testpfx",
            "vocabulary": [
              {"text": "賽德克巴萊", "weight": 4, "lang": "zh"}
            ]
        }
    }
  • 請求參數

    參數

    類型

    預設值

    是否必須

    說明

    model

    string

    -

    支援

    定製熱詞模型,固定為speech-biasing

    action

    string

    -

    支援

    操作類型,固定為create_vocabulary

    target_model

    string

    -

    支援

    使用熱詞列表的語音辨識模型。詳情請參見支援的模型

    必須與後續調用語音辨識介面時使用的語音辨識模型一致。

    prefix

    string

    -

    支援

    為熱詞列表指定一個便於識別的名稱(僅允許數字和小寫字母,長度小於十個字元)。

    該關鍵字會在熱詞列表ID中出現,例如關鍵字為“testpfx”,最終熱詞列表ID為“vocab-testpfx-51773d05xxxxxx”

    vocabulary

    array[object]

    -

    支援

    熱詞列表JSON,詳情請參見熱詞簡介

  • 響應參數

    點擊查看響應樣本

    {
        "output": {
            "vocabulary_id": "vocab-testpfx-5112c3de3705486baxxxxxxx"
        },
        "usage": {
            "count": 1
        },
        "request_id": "aee47022-2352-40fe-acfa-xxxx"
    }

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary_id

    string

    熱詞列表ID。

  • 範例程式碼

    cURL樣本如下(Java和Python樣本請參見對應的SDK)。

    若未將API Key配置到環境變數,需將樣本中的$DASHSCOPE_API_KEY替換為實際的API Key。

    # ======= 重要提示 =======
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization
    # 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # === 執行時請刪除該注釋 ===
    
    curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "speech-biasing",
        "input": {
            "action": "create_vocabulary",
            "target_model": "fun-asr",
            "prefix": "testpfx",
            "vocabulary": [
              {"text": "賽德克巴萊", "weight": 4}
            ]
        }
    }'

查詢所有熱詞列表

Python SDK

  • 介面說明

    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: 熱詞列表標識符列表
        '''
  • 範例程式碼

    import dashscope
    from dashscope.audio.asr import *
    import json
    import os
    
    # 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
    dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
    
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    service = VocabularyService()
    vocabularies = service.list_vocabularies()
    print(f"熱詞列表:{json.dumps(vocabularies)}")
  • 響應參數

    點擊查看響應樣本

    [
      {
        "gmt_create": "2025-04-22 14:23:35",
        "vocabulary_id": "vocab-testpfx-5112c3de3705486baxxxxxxx",
        "gmt_modified": "2025-04-22 14:23:35",
        "status": "OK"
      }
    ]

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary_id

    string

    熱詞列表ID。

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

Java SDK

  • 介面說明

    /**
     * 查詢已建立的所有熱詞列表。預設的頁索引為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
  • 範例程式碼

    import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
    import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
    import com.alibaba.dashscope.exception.InputRequiredException;
    import com.alibaba.dashscope.exception.NoApiKeyException;
    import com.alibaba.dashscope.utils.Constants;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    public class Main {
        // 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
        public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
    
        public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
            // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
            Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    
            VocabularyService service = new VocabularyService(apiKey);
            Vocabulary[] vocabularies = service.listVocabulary("testpfx");
            Gson gson = new GsonBuilder()
                    .setPrettyPrinting()
                    .create();
            System.out.println("熱詞列表:" + gson.toJson(vocabularies));
        }
    }
  • 點擊查看響應樣本

    [
      {
        "gmt_create": "2025-04-22 14:23:35",
        "vocabulary_id": "vocab-testpfx-5112c3de3705486baxxxxxxx",
        "gmt_modified": "2025-04-22 14:23:35",
        "status": "OK"
      }
    ]

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary_id

    string

    熱詞列表ID。

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

RESTful API

  • URL

    中國內地(北京):

    POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization

    國際(新加坡):

    POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization
  • 要求標頭

    參數

    類型

    是否必須

    說明

    Authorization

    string

    支援

    鑒權令牌,格式為Bearer <your_api_key>,使用時,將“<your_api_key>”替換為實際的API Key。

    Content-Type

    string

    支援

    請求體中傳輸的資料的媒體類型。固定為application/json

  • 訊息體

    包含所有請求參數的訊息體如下,對於可選欄位,在實際業務中可根據需求省略。

    重要

    model:定製熱詞模型,固定為speech-biasing

    {
        "model": "speech-biasing",
        "input": {
            "action": "list_vocabulary",
            "prefix": "testpfx",
            "page_index": 0,
            "page_size": 10
        }
    }
  • 請求參數

    參數

    類型

    預設值

    是否必須

    說明

    model

    string

    -

    支援

    定製熱詞模型,固定為speech-biasing

    action

    string

    -

    支援

    操作類型,固定為list_vocabulary

    prefix

    string

    -

    不支援

    熱詞列表自訂首碼,僅允許數字和小寫字母,小於十個字元。

    page_index

    integer

    0

    不支援

    頁碼索引,從0開始計數。

    page_size

    integer

    10

    不支援

    每頁包含資料條數。

  • 響應參數

    點擊查看響應樣本

    {
      "output": {
        "vocabulary_list": [
          {
            "gmt_create": "2025-12-19 11:47:11",
            "gmt_modified": "2025-12-19 11:47:11",
            "status": "OK",
            "vocabulary_id": "vocab-testpfx-xxxxxxxx"
          }
        ]
      },
      "usage": {
        "count": 1
      },
      "request_id": "10e8cde2-b711-4609-b19b-xxxxxx"
    }

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary_id

    string

    熱詞列表ID。

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

  • 範例程式碼

    cURL樣本如下(Java和Python樣本請參見對應的SDK)。

    若未將API Key配置到環境變數,需將樣本中的$DASHSCOPE_API_KEY替換為實際的API Key。

    # ======= 重要提示 =======
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization
    # 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # === 執行時請刪除該注釋 ===
    
    curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "speech-biasing",
        "input": {
            "action": "list_vocabulary",
            "prefix": "testpfx",
            "page_index": 0,
            "page_size": 10
        }
    }'

查詢指定熱詞列表

查詢指定熱詞列表時,由於已知熱詞列表ID,響應中不包含該欄位。

Python SDK

  • 介面說明

    def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
        '''
        擷取熱詞列表內容
        param: vocabulary_id 熱詞列表標識符
        return: 熱詞列表
        '''
  • 範例程式碼

    import dashscope
    from dashscope.audio.asr import *
    import json
    import os
    
    # 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
    dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
    
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    service = VocabularyService()
    # 查詢時替換為實際的熱詞列表ID
    vocabulary = service.query_vocabulary("vocab-testpfx-xxx")
    print(f"熱詞列表:{json.dumps(vocabulary, ensure_ascii=False)}")
  • 響應參數

    點擊查看響應樣本

    {
      "gmt_create": "2025-12-19 11:47:11",
      "gmt_modified": "2025-12-19 11:47:11",
      "status": "OK",
      "target_model": "fun-asr",
      "vocabulary": [
        {
          "lang": "zh",
          "text": "賽德克巴萊",
          "weight": 4
        }
      ]
    }

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary

    object[]

    熱詞列表字典。各欄位含義參見熱詞簡介

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    target_model

    string

    使用熱詞列表的語音辨識模型。詳情請參見支援的模型

    必須與後續調用語音辨識介面時使用的語音辨識模型一致。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

Java SDK

  • 介面說明

    /**
     * 查詢指定熱詞列表
     *
     * @param vocabularyId 需要查詢的熱詞列表
     * @return 熱詞列表對象
     * @throws NoApiKeyException 如果apikey為空白
     * @throws InputRequiredException 如果必須參數為空白
     */
    public Vocabulary queryVocabulary(String vocabularyId)
        throws NoApiKeyException, InputRequiredException
  • 範例程式碼

    import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
    import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
    import com.alibaba.dashscope.exception.InputRequiredException;
    import com.alibaba.dashscope.exception.NoApiKeyException;
    import com.alibaba.dashscope.utils.Constants;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    public class Main {
        // 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
        public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
    
        public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
            // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
            Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    
            VocabularyService service = new VocabularyService(apiKey);
            // 查詢時替換為實際的熱詞列表ID
            Vocabulary vocabulary = service.queryVocabulary("vocab-testpfx-xxxx");
            Gson gson = new GsonBuilder()
                    .setPrettyPrinting()
                    .create();
            System.out.println("熱詞列表:" + gson.toJson(vocabulary.getData()));
        }
    }
  • 響應參數

    點擊查看響應樣本

    {
      "gmt_create": "2025-12-19 11:47:11",
      "gmt_modified": "2025-12-19 11:47:11",
      "status": "OK",
      "target_model": "fun-asr",
      "vocabulary": [
        {
          "lang": "zh",
          "text": "賽德克巴萊",
          "weight": 4
        }
      ]
    }

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary

    object[]

    熱詞列表字典。各欄位含義參見熱詞簡介

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    target_model

    string

    使用熱詞列表的語音辨識模型。詳情請參見支援的模型

    必須與後續調用語音辨識介面時使用的語音辨識模型一致。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

RESTful API

  • URL

    中國內地(北京):

    POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization

    國際(新加坡):

    POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization
  • 要求標頭

    參數

    類型

    是否必須

    說明

    Authorization

    string

    支援

    鑒權令牌,格式為Bearer <your_api_key>,使用時,將“<your_api_key>”替換為實際的API Key。

    Content-Type

    string

    支援

    請求體中傳輸的資料的媒體類型。固定為application/json

  • 訊息體

    包含所有請求參數的訊息體如下,對於可選欄位,在實際業務中可根據需求省略。

    重要

    model:定製熱詞模型,固定為speech-biasing

    {
        "model": "speech-biasing",
        "input": {
            "action": "query_vocabulary",
            "vocabulary_id": "vocab-testpfx-xxxx"
        }
    }
  • 請求參數

    參數

    類型

    預設值

    是否必須

    說明

    model

    string

    -

    支援

    定製熱詞模型,固定為speech-biasing

    action

    string

    -

    支援

    操作類型,固定為query_vocabulary

    vocabulary_id

    string

    -

    支援

    待查詢熱詞列表ID。

  • 響應參數

    點擊查看響應樣本

    {
      "output": {
        "gmt_create": "2025-12-19 11:47:11",
        "gmt_modified": "2025-12-19 11:47:11",
        "status": "OK",
        "target_model": "fun-asr",
        "vocabulary": [
          {
            "lang": "zh",
            "text": "賽德克巴萊",
            "weight": 4
          }
        ]
      },
      "usage": {
        "count": 1
      },
      "request_id": "3d461d3f-b2c4-4de5-xxxx"
    }

    需關注的參數如下:

    參數

    類型

    說明

    vocabulary

    object[]

    熱詞列表字典。各欄位含義參見熱詞簡介

    gmt_create

    string

    建立熱詞列表的時間。

    gmt_modified

    string

    修改熱詞列表的時間。

    target_model

    string

    使用熱詞列表的語音辨識模型。詳情請參見支援的模型

    必須與後續調用語音辨識介面時使用的語音辨識模型一致。

    status

    string

    狀態:

    • OK:可調用

    • UNDEPLOYED:不可調用。

  • 範例程式碼

    cURL樣本如下(Java和Python樣本請參見對應的SDK)。

    若未將API Key配置到環境變數,需將樣本中的$DASHSCOPE_API_KEY替換為實際的API Key。

    # ======= 重要提示 =======
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization
    # 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # === 執行時請刪除該注釋 ===
    
    curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "speech-biasing",
        "input": {
            "action": "query_vocabulary",
            "vocabulary_id": "vocab-testpfx-xxxx"
        }
    }'

更新熱詞列表

Python SDK

  • 介面說明

    def update_vocabulary(self, vocabulary_id: str, vocabulary: List[dict]) -> None:
        '''
        用新的熱詞列表替換已有熱詞列表
        param: vocabulary_id 需要替換的熱詞列表標識符
        param: vocabulary 熱詞列表
        '''
  • 範例程式碼

    import dashscope
    from dashscope.audio.asr import *
    import os
    
    # 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
    dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
    
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    service = VocabularyService()
    my_vocabulary = [
        {"text": "賽德克巴萊", "weight": 4, "lang": "zh"}
    ]
    # 替換為實際的熱詞列表ID
    service.update_vocabulary("vocab-testpfx-xxx", my_vocabulary)

Java SDK

  • 介面說明

    /**
     * 更新熱詞列表
     *
     * @param vocabularyId 需要更新的熱詞列表
     * @param vocabulary 熱詞列表對象
     * @throws NoApiKeyException 如果apikey為空白
     * @throws InputRequiredException 如果必須參數為空白
     */
    public void updateVocabulary(String vocabularyId, JsonArray vocabulary)
        throws NoApiKeyException, InputRequiredException
  • 範例程式碼

    import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
    import com.alibaba.dashscope.exception.InputRequiredException;
    import com.alibaba.dashscope.exception.NoApiKeyException;
    import com.alibaba.dashscope.utils.Constants;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        // 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
        public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
    
        public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
            // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
            Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    
            JsonArray vocabularyJson = 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);
                vocabularyJson.add(jsonObject);
            }
    
            VocabularyService service = new VocabularyService(apiKey);
            // 替換為實際的熱詞列表ID
            service.updateVocabulary("vocab-testpfx-xxx", vocabularyJson);
        }
    }
    
    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;
        }
    }

RESTful API

  • URL

    中國內地(北京):

    POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization

    國際(新加坡):

    POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization
  • 要求標頭

    參數

    類型

    是否必須

    說明

    Authorization

    string

    支援

    鑒權令牌,格式為Bearer <your_api_key>,使用時,將“<your_api_key>”替換為實際的API Key。

    Content-Type

    string

    支援

    請求體中傳輸的資料的媒體類型。固定為application/json

  • 訊息體

    包含所有請求參數的訊息體如下,對於可選欄位,在實際業務中可根據需求省略。

    重要

    model:定製熱詞模型,固定為speech-biasing

    {
        "model": "speech-biasing",
        "input": {
            "action": "update_vocabulary",
            "vocabulary_id": "vocab-testpfx-6977ae49f65c4c3db054727cxxxxxxxx",
            "vocabulary": [
              {"text": "賽德克巴萊", "weight": 4, "lang": "zh"}
            ]
        }
    }
  • 請求參數

    參數

    類型

    預設值

    是否必須

    說明

    model

    string

    -

    支援

    定製熱詞模型,固定為speech-biasing

    action

    string

    -

    支援

    操作類型,固定為update_vocabulary

    vocabulary_id

    string

    -

    支援

    待更新熱詞列表ID。

    vocabulary

    object[]

    -

    支援

    更新後的熱詞列表字典。各欄位含義參見熱詞簡介

  • 響應參數

    點擊查看響應樣本

    {
      "output": {},
      "usage": {
        "count": 1
      },
      "request_id": "aee47022-2352-40fe-acfa-xxxx"
    }
  • 範例程式碼

    cURL樣本如下(Java和Python樣本請參見對應的SDK)。

    若未將API Key配置到環境變數,需將樣本中的$DASHSCOPE_API_KEY替換為實際的API Key。

    # ======= 重要提示 =======
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization
    # 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # === 執行時請刪除該注釋 ===
    
    curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "speech-biasing",
        "input": {
            "action": "update_vocabulary",
            "vocabulary_id": "vocab-testpfx-xxx",
            "vocabulary": [
              {"text": "賽德克巴萊", "weight": 4, "lang": "zh"}
            ]
        }
    }'

刪除熱詞列表

Python SDK

  • 介面說明

    def delete_vocabulary(self, vocabulary_id: str) -> None:
        '''
        刪除熱詞列表
        param: vocabulary_id 需要刪除的熱詞列表標識符
        '''
  • 範例程式碼

    import dashscope
    from dashscope.audio.asr import *
    import os
    
    # 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
    dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
    
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    service = VocabularyService()
    # 替換為實際的熱詞表ID
    service.delete_vocabulary("vocab-testpfx-xxxx")

Java SDK

  • 介面說明

    /**
     * 刪除熱詞列表
     *
     * @param vocabularyId 需要刪除的熱詞列表
     * @throws NoApiKeyException 如果apikey為空白
     * @throws InputRequiredException 如果必須參數為空白
     */
    public void deleteVocabulary(String vocabularyId)
        throws NoApiKeyException, InputRequiredException
  • 範例程式碼

    import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
    import com.alibaba.dashscope.exception.InputRequiredException;
    import com.alibaba.dashscope.exception.NoApiKeyException;
    import com.alibaba.dashscope.utils.Constants;
    
    public class Main {
        // 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
        // 若沒有配置環境變數,請用百鍊API Key將下行替換為:public static String apiKey = "sk-xxx"
        public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
    
        public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
            // 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
            Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    
            VocabularyService service = new VocabularyService(apiKey);
            // 刪除時替換為實際的熱詞列表ID
            service.deleteVocabulary("vocab-testpfx-xxxx");
        }
    }

RESTful API

  • URL

    中國內地(北京):

    POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization

    國際(新加坡):

    POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization
  • 要求標頭

    參數

    類型

    是否必須

    說明

    Authorization

    string

    支援

    鑒權令牌,格式為Bearer <your_api_key>,使用時,將“<your_api_key>”替換為實際的API Key。

    Content-Type

    string

    支援

    請求體中傳輸的資料的媒體類型。固定為application/json

  • 訊息體

    包含所有請求參數的訊息體如下,對於可選欄位,在實際業務中可根據需求省略。

    重要

    model:定製熱詞模型,固定為speech-biasing

    {
        "model": "speech-biasing",
        "input": {
            "action": "delete_vocabulary",
            "vocabulary_id": "vocab-testpfx-xxx"
        }
    }
  • 請求參數

    參數

    類型

    預設值

    是否必須

    說明

    model

    string

    -

    支援

    定製熱詞模型,固定為speech-biasing

    action

    string

    -

    支援

    操作類型,固定為delete_vocabulary

    vocabulary_id

    string

    -

    支援

    待刪除熱詞列表ID。

  • 響應參數

    點擊查看響應樣本

    {
      "output": {},
      "usage": {
        "count": 1
      },
      "request_id": "aee47022-2352-40fe-acfa-xxxx"
    }
  • 範例程式碼

    cURL樣本如下(Java和Python樣本請參見對應的SDK)。

    若未將API Key配置到環境變數,需將樣本中的$DASHSCOPE_API_KEY替換為實際的API Key。

    # ======= 重要提示 =======
    # 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization
    # 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
    # === 執行時請刪除該注釋 ===
    
    curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/audio/asr/customization \
    -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "speech-biasing",
        "input": {
            "action": "delete_vocabulary",
            "vocabulary_id": "vocab-testpfx-xxx"
        }
    }'

錯誤碼說明

如遇報錯問題,請參見錯誤資訊進行排查。