快速入門:使用 Azure OpenAI Whisper 模型進行語音轉換文字

本快速入門說明如何使用 Azure OpenAI Whisper 模型進行語音轉換文字轉換。 Whisper 模型可以使用多種語言來轉譯人類語音,也可以將其他語言轉譯成英文。

Whisper 模型的檔案大小限制為 25 MB。 如果您需要轉譯大於 25 MB 的檔案,可以使用 Azure AI 語音批次謄寫 API。

必要條件

設定

擷取金鑰和端點

若要成功對 Azure OpenAI 進行呼叫,您需要端點金鑰

變數名稱
AZURE_OPENAI_ENDPOINT 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 或者,您可以在 [Azure OpenAI Studio]>[遊樂場]>[程式碼檢視] 中找到該值。 範例端點為:https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 您可以使用 KEY1KEY2

移至您在 Azure 入口網站中的資源。 您可以在 [資源管理] 區段中找到 [端點和金鑰]。 複製您的端點和存取金鑰,因為您需要這兩者才能驗證 API 呼叫。 您可以使用 KEY1KEY2。 隨時持有兩個金鑰可讓您安全地輪替和重新產生金鑰,而不會造成服務中斷。

此螢幕擷取畫面顯示 Azure 入口網站中某個 Azure OpenAI 資源的概觀使用者介面,其中以紅色圓圈顯示端點和存取金鑰位置。

環境變數

為您的金鑰和端點建立及指派永續性環境變數。

重要

如果您使用 API 金鑰,請將其安全地儲存在別處,例如 Azure Key Vault。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。

如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求 (英文)。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

建立 REST API 要求和回應

在 bash 殼層中,執行下列命令。 您必須將 YourDeploymentName 取代為在部署 Whisper 模型時所選擇的部署名稱。 部署名稱不一定要與模型名稱相同。 除非您選擇與基礎模型名稱相同的部署名稱,否則輸入模型名稱會導致錯誤。

curl $AZURE_OPENAI_ENDPOINT/openai/deployments/YourDeploymentName/audio/transcriptions?api-version=2024-02-01 \
 -H "api-key: $AZURE_OPENAI_API_KEY" \
 -H "Content-Type: multipart/form-data" \
 -F file="@./wikipediaOcelot.wav"

具有範例端點的先前命令第一行將如下所示:

curl https://aoai-docs.openai.azure.com/openai/deployments/{YourDeploymentName}/audio/transcriptions?api-version=2024-02-01 \

您可以從 GitHub 的 Azure AI 語音 SDK 存放庫取得範例音訊檔案,例如 wikipediaOcelot.wav

重要

在生產環境中,請使用安全方式來儲存及存取 Azure Key Vault 等認證。 如需有關認證安全性的詳細資訊,請參閱 Azure AI 服務安全性

輸出

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

必要條件

設定

擷取金鑰和端點

若要成功對 Azure OpenAI 進行呼叫,您需要端點金鑰

變數名稱
AZURE_OPENAI_ENDPOINT 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 或者,您可以在 [Azure OpenAI Studio]>[遊樂場]>[程式碼檢視] 中找到該值。 範例端點為:https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 您可以使用 KEY1KEY2

移至您在 Azure 入口網站中的資源。 您可以在 [資源管理] 區段中找到 [端點和金鑰]。 複製您的端點和存取金鑰,因為您需要這兩者才能驗證 API 呼叫。 您可以使用 KEY1KEY2。 隨時持有兩個金鑰可讓您安全地輪替和重新產生金鑰,而不會造成服務中斷。

此螢幕擷取畫面顯示 Azure 入口網站中某個 Azure OpenAI 資源的概觀使用者介面,其中以紅色圓圈顯示端點和存取金鑰位置。

環境變數

為您的金鑰和端點建立及指派永續性環境變數。

重要

如果您使用 API 金鑰,請將其安全地儲存在別處,例如 Azure Key Vault。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。

如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求 (英文)。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

若要設定無密碼驗證,您必須:

  1. 使用 @azure/identity 套件。
  2. Cognitive Services User 角色指派給您的使用者帳戶。 這可以在 Azure 入口網站底下的 [存取控制 (IAM)]> [新增角色指派] 中完成。
  3. 使用 Azure CLI 登入,例如 az login

建立 Python 環境

使用下列項目安裝 OpenAI Python 用戶端程式庫:

pip install openai

建立 Python 應用程式

  1. 建立命名為 quickstart.py 的新 Python 檔案。 然後,以您慣用的編輯器或 IDE 加以開啟。

  2. quickstart.py 的內容取代為下列程式碼。 修改程式碼以新增部署名稱:

    import os
    from openai import AzureOpenAI
        
    client = AzureOpenAI(
        api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
        api_version="2024-02-01",
        azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )
    
    deployment_id = "YOUR-DEPLOYMENT-NAME-HERE" #This will correspond to the custom name you chose for your deployment when you deployed a model."
    audio_test_file = "./wikipediaOcelot.wav"
    
    result = client.audio.transcriptions.create(
        file=open(audio_test_file, "rb"),            
        model=deployment_id
    )
    
    print(result)

使用快速入門檔案上的 python 命令來執行應用程式:

python quickstart.py

您可以從 GitHub 的 Azure AI 語音 SDK 存放庫取得範例音訊檔案,例如 wikipediaOcelot.wav

重要

在生產環境中,請使用安全方式來儲存及存取 Azure Key Vault 等認證。 如需有關認證安全性的詳細資訊,請參閱 Azure AI 服務安全性

輸出

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

原始程式碼 (英文) | 套件 (npm) (英文) | 範例 (英文)

必要條件

設定

擷取金鑰和端點

若要成功對 Azure OpenAI 進行呼叫,您需要端點金鑰

變數名稱
AZURE_OPENAI_ENDPOINT 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 或者,您可以在 [Azure OpenAI Studio]>[遊樂場]>[程式碼檢視] 中找到該值。 範例端點為:https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 您可以使用 KEY1KEY2

移至您在 Azure 入口網站中的資源。 您可以在 [資源管理] 區段中找到 [端點和金鑰]。 複製您的端點和存取金鑰,因為您需要這兩者才能驗證 API 呼叫。 您可以使用 KEY1KEY2。 隨時持有兩個金鑰可讓您安全地輪替和重新產生金鑰,而不會造成服務中斷。

此螢幕擷取畫面顯示 Azure 入口網站中某個 Azure OpenAI 資源的概觀使用者介面,其中以紅色圓圈顯示端點和存取金鑰位置。

環境變數

為您的金鑰和端點建立及指派永續性環境變數。

重要

如果您使用 API 金鑰,請將其安全地儲存在別處,例如 Azure Key Vault。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。

如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求 (英文)。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

若要設定無密碼驗證,您必須:

  1. 使用 @azure/identity 套件。
  2. Cognitive Services User 角色指派給您的使用者帳戶。 這可以在 Azure 入口網站底下的 [存取控制 (IAM)]> [新增角色指派] 中完成。
  3. 使用 Azure CLI 登入,例如 az login

建立節點應用程式

在主控台視窗 (例如 cmd、PowerShell 或 Bash) 中,為您的應用程式建立新的目錄,並瀏覽至該目錄。 然後執行 npm init 命令以使用 package.json 檔案建立節點應用程式。

npm init

安裝用戶端程式庫

使用下列項目安裝用戶端程式庫:

npm install openai @azure/identity

應用程式的 package.json 檔案將會隨著相依項目更新。

建立應用程式範例

  1. 建立一個名為 Whisper.js 的新檔案,然後在您慣用的程式碼編輯器中將其開啟。 將以下程式碼複製到 Whisper.js 檔案:

    require("dotenv/config");
    const { createReadStream } = require("fs");
    const { AzureOpenAI } = require("openai");
    
    // You will need to set these environment variables or edit the following values
    const audioFilePath = process.env["AUDIO_FILE_PATH"] || "<audio file path>";
    const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
    const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
    
    // Required Azure OpenAI deployment name and API version
    const apiVersion = "2024-08-01-preview";
    const deploymentName = "whisper";
    
    function getClient() {
      return new AzureOpenAI({
        endpoint,
        apiKey,
        apiVersion,
        deployment: deploymentName,
      });
    }
    
    export async function main() {
      console.log("== Transcribe Audio Sample ==");
    
      const client = getClient();
      const result = await client.audio.transcriptions.create({
        model: "",
        file: createReadStream(audioFilePath),
      });
    
      console.log(`Transcription: ${result.text}`);
    }
    
    main().catch((err) => {
      console.error("The sample encountered an error:", err);
    });
    
  2. 使用下列命令來執行指令碼:

    node Whisper.js
    

您可以從 GitHub 的 Azure AI 語音 SDK 存放庫取得範例音訊檔案,例如 wikipediaOcelot.wav

重要

在生產環境中,請使用安全方式來儲存及存取 Azure Key Vault 等認證。 如需有關認證安全性的詳細資訊,請參閱 Azure AI 服務安全性

輸出

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

必要條件

設定

擷取金鑰和端點

若要成功對 Azure OpenAI 進行呼叫,您需要端點金鑰

變數名稱
AZURE_OPENAI_ENDPOINT 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 或者,您可以在 [Azure OpenAI Studio]>[遊樂場]>[程式碼檢視] 中找到該值。 範例端點為:https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 您可以使用 KEY1KEY2

移至您在 Azure 入口網站中的資源。 您可以在 [資源管理] 區段中找到 [端點和金鑰]。 複製您的端點和存取金鑰,因為您需要這兩者才能驗證 API 呼叫。 您可以使用 KEY1KEY2。 隨時持有兩個金鑰可讓您安全地輪替和重新產生金鑰,而不會造成服務中斷。

此螢幕擷取畫面顯示 Azure 入口網站中某個 Azure OpenAI 資源的概觀使用者介面,其中以紅色圓圈顯示端點和存取金鑰位置。

環境變數

為您的金鑰和端點建立及指派永續性環境變數。

重要

如果您使用 API 金鑰,請將其安全地儲存在別處,例如 Azure Key Vault。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。

如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求 (英文)。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

建立 PowerShell 應用程式

執行下列命令。 您必須將 YourDeploymentName 取代為在部署 Whisper 模型時所選擇的部署名稱。 部署名稱不一定要與模型名稱相同。 除非您選擇與基礎模型名稱相同的部署名稱,否則輸入模型名稱會導致錯誤。

# Azure OpenAI metadata variables
$openai = @{
    api_key     = $Env:AZURE_OPENAI_API_KEY
    api_base    = $Env:AZURE_OPENAI_ENDPOINT # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
    api_version = '2024-02-01' # this may change in the future
    name        = 'YourDeploymentName' #This will correspond to the custom name you chose for your deployment when you deployed a model.
}

# Header for authentication
$headers = [ordered]@{
    'api-key' = $openai.api_key
}

$form = @{ file = get-item -path './wikipediaOcelot.wav' }

# Send a completion call to generate an answer
$url = "$($openai.api_base)/openai/deployments/$($openai.name)/audio/transcriptions?api-version=$($openai.api_version)"

$response = Invoke-RestMethod -Uri $url -Headers $headers -Form $form -Method Post -ContentType 'multipart/form-data'
return $response.text

您可以從 GitHub 的 Azure AI 語音 SDK 存放庫取得範例音訊檔案,例如 wikipediaOcelot.wav

重要

在生產環境中,請使用安全方法 (例如使用 Azure Key Vault 進行 PowerShell 秘密管理) 來儲存及存取認證。 如需有關認證安全性的詳細資訊,請參閱 Azure AI 服務安全性

輸出

The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs.

清除資源

如果您想要清除和移除 Azure OpenAI 資源,可以刪除資源。 刪除資源之前,您必須先刪除任何已部署的模型。

下一步