Le SDK Python permet de créer, lister, interroger, mettre à jour et supprimer des vocabulaires personnalisés pour la reconnaissance vocale.
ImportantAlibaba Cloud Model Studio a publié des domaines spécifiques aux espaces de travail pour les régions Chine (Pékin) et Singapour. Ces nouveaux domaines dédiés offrent des performances supérieures et une stabilité accrue pour les requêtes d'inférence. Nous vous recommandons de migrer vers ces nouveaux domaines :
- Chine (Pékin) : de
dashscope.aliyuncs.comvers{WorkspaceId}.cn-beijing.maas.aliyuncs.com - Singapour : de
dashscope-intl.aliyuncs.comvers{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
Remplacez {WorkspaceId} par votre ID d'espace de travail. Les domaines existants restent pleinement fonctionnels.
Guide utilisateur : Améliorer la précision de la reconnaissance.
ImportantLes vocabulaires personnalisés sont uniquement pris en charge dans l'espace de travail principal. Les sous-espaces de travail ne les prennent pas en charge.
Endpoint
Par défaut, le SDK se connecte à l'endpoint de la région Chine (Pékin). Pour utiliser une autre région, définissez dashscope.base_http_api_url avant tout appel API.
Singapore
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1
Remplacez {WorkspaceId} par votre ID d'espace de travail.
China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1
Remplacez {WorkspaceId} par votre ID d'espace de travail.
Pour utiliser la région Singapour, configurez dashscope.base_http_api_url avant tout appel :
import dashscope
# Set this at the beginning of your code
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
Remarque :
- Les clés API varient selon les régions. Utilisez la clé API correspondant à votre région.
- Le paramètre de région est global et s'applique à tous les appels API DashScope.
VocabularyService
Package : dashscope.audio.asr.VocabularyService
Description : Crée, liste, interroge, met à jour et supprime des vocabulaires personnalisés.
Constructeur
VocabularyService(api_key: str = None, workspace: str = None, model: str = None)
Si api_key n'est pas transmis, le SDK utilise la variable globale dashscope.api_key.
create_vocabulary() - Créer un vocabulaire personnalisé
Signature de la méthode :
def create_vocabulary(
self,
target_model: str,
prefix: str,
vocabulary: List[dict]) -> str
Paramètres :
Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
target_model | str | Oui | Modèle de reconnaissance vocale utilisant ce vocabulaire. Cette valeur doit correspondre au modèle spécifié lors de l'appel à l'API de reconnaissance vocale. |
prefix | str | Oui | Préfixe personnalisé pour le vocabulaire. Seules les lettres minuscules et les chiffres sont autorisés, avec une longueur maximale de 10 caractères. |
vocabulary | List[dict] | Oui | Liste de mots chauds. Chaque entrée est un dictionnaire contenant les champs text, weight et lang. Pour plus d'informations, consultez Structure du dictionnaire de mots chauds. |
Valeur de retour :
Type | Description |
|---|---|
str | ID du vocabulaire créé. |
list_vocabularies() - Lister les vocabulaires personnalisés
Correspond à l'action HTTP API action: list_vocabulary (HTTP utilise le singulier, tandis que la méthode Python list_vocabularies utilise le pluriel).
Signature de la méthode :
def list_vocabularies(
self,
prefix: str = None,
page_index: int = 0,
page_size: int = 10) -> List[dict]
Paramètres :
Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
prefix | str | Non | Préfixe personnalisé du vocabulaire. Si spécifié, seuls les vocabulaires portant ce préfixe sont retournés. |
page_index | int | Non | Numéro de page, commençant à 0. Valeur par défaut : 0. |
page_size | int | Non | Nombre d'entrées par page. Valeur par défaut : 10. |
Valeur de retour :
Type | Description |
|---|---|
List[dict] | Liste d'objets vocabulaire, chacun contenant vocabulary_id, gmt_create, gmt_modified et status. |
Champs retournés par list_vocabularies() :
| Champ | Type | Description |
|---|---|---|
vocabulary_id | str | ID du vocabulaire. |
gmt_create | str | Date de création. |
gmt_modified | str | Date de dernière modification. |
status | str | État du vocabulaire :
|
query_vocabulary() - Interroger un vocabulaire personnalisé
Signature de la méthode :
def query_vocabulary(
self,
vocabulary_id: str) -> dict
Paramètres :
Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
vocabulary_id | str | Oui | ID du vocabulaire personnalisé à interroger. |
Valeur de retour :
Type | Description |
|---|---|
dict | Objet vocabulaire contenant vocabulary, target_model, gmt_create, gmt_modified et status. |
Champs retournés par query_vocabulary() :
| Champ | Type | Description |
|---|---|---|
vocabulary | List[dict] | Contenu de la liste des mots chauds. |
target_model | str | Modèle de reconnaissance vocale utilisant ce vocabulaire. Cette valeur doit correspondre au modèle spécifié lors de l'appel à l'API de reconnaissance vocale. |
gmt_create | str | Date de création. |
gmt_modified | str | Date de dernière modification. |
status | str | État du vocabulaire :
|
update_vocabulary() - Mettre à jour un vocabulaire personnalisé
Signature de la méthode :
def update_vocabulary(
self,
vocabulary_id: str,
vocabulary: List[dict]) -> None
Paramètres :
Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
vocabulary_id | str | Oui | ID du vocabulaire à mettre à jour. |
vocabulary | List[dict] | Oui | Nouveau vocabulaire. Ce contenu remplace intégralement les entrées existantes. |
Valeur de retour : None
delete_vocabulary() - Supprimer un vocabulaire personnalisé
Signature de la méthode :
def delete_vocabulary(
self,
vocabulary_id: str) -> None
Paramètres :
Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
vocabulary_id | str | Oui | ID du vocabulaire à supprimer. |
Valeur de retour : None
Structure du dictionnaire de mots chauds
Champs de chaque dictionnaire de vocabulaire :
| Champ | Type | Obligatoire | Description |
|---|---|---|---|
text | str | Oui | Texte de l'entrée du vocabulaire. La langue du texte doit être prise en charge par le modèle sélectionné. Les langues compatibles varient selon le modèle. Utilisez des mots réels plutôt que des combinaisons de caractères arbitraires afin d'améliorer la précision de la reconnaissance. Longueur maximale : 15 caractères pour un texte incluant des caractères non ASCII, ou 7 mots séparés par des espaces pour un texte exclusivement ASCII. |
weight | int | Oui | Poids de l'entrée du vocabulaire. Valeur recommandée : 4. Valeurs valides : 1 à 5. Si la précision de la reconnaissance ne s'améliore pas, augmentez le poids. Un poids excessivement élevé risque toutefois de réduire la précision de reconnaissance des autres mots. |
lang | str | Non | Code de langue de l'audio à reconnaître. Lorsque ce paramètre est défini, le système améliore la reconnaissance des entrées de vocabulaire dans la langue spécifiée. Si vous ne pouvez pas déterminer la langue à l'avance, laissez ce paramètre vide. Le modèle détectera automatiquement la langue. Valeurs valides (variables selon le modèle) :
|
Exemples de code
Tous les exemples lisent la clé API depuis une variable d'environnement.
Créer un vocabulaire personnalisé
import dashscope
from dashscope.audio.asr import *
import os
# The API keys for the Singapore and Beijing regions are different. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If no environment variable is configured, replace the following line with your Model Studio API key: dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# The following configuration is for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
prefix = 'testpfx'
target_model = "fun-asr"
my_vocabulary = [
{"text": "Seediq Bale", "weight": 4}
]
# Create a custom vocabulary
service = VocabularyService()
vocabulary_id = service.create_vocabulary(
prefix=prefix,
target_model=target_model,
vocabulary=my_vocabulary)
print(f"Vocabulary ID: {vocabulary_id}")
Lister les vocabulaires personnalisés
import dashscope
from dashscope.audio.asr import *
import json
import os
# The API keys for the Singapore and Beijing regions are different. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If no environment variable is configured, replace the following line with your Model Studio API key: dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# The following configuration is for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
service = VocabularyService()
vocabularies = service.list_vocabularies()
print(f"Vocabularies: {json.dumps(vocabularies)}")
Interroger un vocabulaire personnalisé
import dashscope
from dashscope.audio.asr import *
import json
import os
# The API keys for the Singapore and Beijing regions are different. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If no environment variable is configured, replace the following line with your Model Studio API key: dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# The following configuration is for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
service = VocabularyService()
# Replace with the actual vocabulary ID when querying
vocabulary = service.query_vocabulary("vocab-testpfx-xxx")
print(f"Vocabulary: {json.dumps(vocabulary, ensure_ascii=False)}")
Mettre à jour un vocabulaire personnalisé
import dashscope
from dashscope.audio.asr import *
import os
# The API keys for the Singapore and Beijing regions are different. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If no environment variable is configured, replace the following line with your Model Studio API key: dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# The following configuration is for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
service = VocabularyService()
my_vocabulary = [
{"text": "Seediq Bale", "weight": 4, "lang": "en"}
]
# Replace with the actual vocabulary ID when updating
service.update_vocabulary("vocab-testpfx-xxx", my_vocabulary)
Supprimer un vocabulaire personnalisé
import dashscope
from dashscope.audio.asr import *
import os
# The API keys for the Singapore and Beijing regions are different. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If no environment variable is configured, replace the following line with your Model Studio API key: dashscope.api_key = "sk-xxx"
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# The following configuration is for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
service = VocabularyService()
# Replace with the actual vocabulary ID when deleting
service.delete_vocabulary("vocab-testpfx-xxxx")