共用方式為


如何使用 Azure AI Studio 部署大型語言模型

重要

本文所述的部分功能可能僅適用於預覽版。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

部署大型語言模型 (LLM) 使其可在網站、應用程式或其他生產環境中使用。 部署通常涉及伺服器或在雲端中主控模型,以及為使用者建立 API 或其他介面以與模型互動。 您可以針對生成式 AI 應用程式叫用虛擬機器部署進行即時推斷,例如聊天和 Copilot。

在本文中,您會瞭解如何在 Azure AI Studio 中部署大型語言模型。 您可以從模型目錄或專案部署模型。 您也可以使用 Azure Machine Learning SDK 來部署模型。 本文也涵蓋如何對已部署的模型執行推斷。

使用程式碼部署和推斷無伺服器 API 模型

部署模型

無伺服器 API 模型是您可以使用隨用隨付計費來部署的模型。 範例包括 Phi-3、Llama-2、Command R、Mistral Large 等等。 若為無伺服器 API 模型,除非您選擇微調模型,否則只會向您收取推斷費用。

取得模型識別碼

您可以使用 Azure Machine Learning SDK 來部署無伺服器 API 模型,但首先,讓我們瀏覽模型目錄並取得部署所需的模型 ID。

  1. 登入 AI Studio,並移至「首頁」頁面。

  2. 從左側側邊列中,選取「模型目錄」。

  3. 在「部署選項」篩選中,選取「無伺服器 API」。

    顯示如何在目錄中依無伺服器 API 模型進行篩選的螢幕擷取畫面。

  4. 選取模型。

  5. 從您選取模型的詳細資料頁面複製模型 ID。 這看起來如下:azureml://registries/azureml-cohere/models/Cohere-command-r-plus/versions/3

安裝 Azure Machine Learning SDK

接著,您需要安裝 Azure Machine Learning SDK。 在您的終端機中執行下列命令:

pip install azure-ai-ml
pip install azure-identity

部署無伺服器 API 模型

您需要使用 Azure AI 進行驗證。

from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azure.ai.ml.entities import MarketplaceSubscription, ServerlessEndpoint

# You can find your credential information in project settings.
client = MLClient(
    credential=DefaultAzureCredential(),
    subscription_id="your subscription name goes here",
    resource_group_name="your resource group name goes here",
    workspace_name="your project name goes here",
)

接著,讓我們參考您稍早找到的模型 ID。

# You can find the model ID on the model catalog.
model_id="azureml://registries/azureml-meta/models/Llama-2-70b-chat/versions/18" 

來自第三方模型提供者的無伺服器 API 模型需要 Azure Marketplace 訂用帳戶才能使用模型。 讓我們建立市集訂用帳戶。

注意

如果您要從 Microsoft 部署無伺服器 API 模型 (例如 Phi-3),則可以略過元件。

# You can customize the subscription name.
subscription_name="Meta-Llama-2-70b-chat" 

marketplace_subscription = MarketplaceSubscription(
    model_id=model_id,
    name=subscription_name,
)

marketplace_subscription = client.marketplace_subscriptions.begin_create_or_update(
    marketplace_subscription
).result()

最後,讓我們建立無伺服器端點。


endpoint_name="Meta-Llama-2-70b-chat-qwerty" # Your endpoint name must be unique

serverless_endpoint = ServerlessEndpoint(
    name=endpoint_name,
    model_id=model_id
)

created_endpoint = client.serverless_endpoints.begin_create_or_update(
    serverless_endpoint
).result()

取得無伺服器 API 端點和金鑰

endpoint_keys = client.serverless_endpoints.get_keys(endpoint_name)
print(endpoint_keys.primary_key)
print(endpoint_keys.secondary_key)

推斷部署

若要推斷,您想要使用特別迎合您所使用的不同模型類型和 SDK 的程式碼。 您可以在 Azure/azureml-examples 範例存放庫中找到程式碼範例。

使用程式碼部署和推斷受控計算部署

部署模型

AI Studio 模型目錄 提供超過 1,600 個模型,而部署這些模型最常見的方式是使用受控計算部署選項,這有時也稱為受控線上部署。

取得模型識別碼

您可以使用 Azure Machine Learning SDK 來部署受控計算模型,但首先,讓我們瀏覽模型目錄並取得部署所需的模型 ID。

  1. 登入 AI Studio,並移至「首頁」頁面。

  2. 從左側側邊列中,選取「模型目錄」。

  3. 在「部署選項」篩選器中,選取「受控計算」。

    顯示如何在目錄中依受控計算模型進行篩選的螢幕擷取畫面。

  4. 選取模型。

  5. 從您選取模型的詳細資料頁面複製模型 ID。 這看起來如下:azureml://registries/azureml/models/deepset-roberta-base-squad2/versions/16

安裝 Azure Machine Learning SDK

針對此步驟,您需要安裝 Azure Machine Learning SDK。

pip install azure-ai-ml
pip install azure-identity

部署模型

您需要使用 Azure AI 進行驗證。

from azure.ai.ml import MLClient
from azure.identity import InteractiveBrowserCredential

client = MLClient(
    credential=InteractiveBrowserCredential,
    subscription_id="your subscription name goes here",
    resource_group_name="your resource group name goes here",
    workspace_name="your project name goes here",
)

部署模型。

針對「受控計算部署」選項,您必須建立模型部署前的端點。 將端點視為可裝載多個模型部署的容器。 由於端點名稱在區域中必須唯一,因此在此範例中,我們會使用時間戳記來建立唯一的端點名稱。

import time, sys
from azure.ai.ml.entities import (
    ManagedOnlineEndpoint,
    ManagedOnlineDeployment,
    ProbeSettings,
)

# Make the endpoint name unique
timestamp = int(time.time())
online_endpoint_name = "customize your endpoint name here" + str(timestamp)

# Create an online endpoint
endpoint = ManagedOnlineEndpoint(
    name=online_endpoint_name,
    auth_mode="key",
)
workspace_ml_client.begin_create_or_update(endpoint).wait()

建立部署。 您可以在模型目錄中找到模型 ID。

model_name = "azureml://registries/azureml/models/deepset-roberta-base-squad2/versions/16" 

demo_deployment = ManagedOnlineDeployment(
    name="demo",
    endpoint_name=online_endpoint_name,
    model=model_name,
    instance_type="Standard_DS3_v2",
    instance_count=2,
    liveness_probe=ProbeSettings(
        failure_threshold=30,
        success_threshold=1,
        timeout=2,
        period=10,
        initial_delay=1000,
    ),
    readiness_probe=ProbeSettings(
        failure_threshold=10,
        success_threshold=1,
        timeout=10,
        period=10,
        initial_delay=1000,
    ),
)
workspace_ml_client.online_deployments.begin_create_or_update(demo_deployment).wait()
endpoint.traffic = {"demo": 100}
workspace_ml_client.begin_create_or_update(endpoint).result()

推斷部署

您需要範例 json 資料來測試推斷。 以下列程式碼建立 sample_score.json

{
  "inputs": {
    "question": [
      "Where do I live?",
      "Where do I live?",
      "What's my name?",
      "Which name is also used to describe the Amazon rainforest in English?"
    ],
    "context": [
      "My name is Wolfgang and I live in Berlin",
      "My name is Sarah and I live in London",
      "My name is Clara and I live in Berkeley.",
      "The Amazon rainforest (Portuguese: Floresta Amaz\u00f4nica or Amaz\u00f4nia; Spanish: Selva Amaz\u00f3nica, Amazon\u00eda or usually Amazonia; French: For\u00eat amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain \"Amazonas\" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
    ]
  }
}

讓我們使用 sample_score.json 來推斷。 根據您儲存範例 json 檔案的位置來變更位置。

scoring_file = "./sample_score.json" 
response = workspace_ml_client.online_endpoints.invoke(
    endpoint_name=online_endpoint_name,
    deployment_name="demo",
    request_file=scoring_file,
)
response_json = json.loads(response)
print(json.dumps(response_json, indent=2))

刪除部署端點

若要刪除 AI Studio 中的部署,請選取部署詳細資料頁面頂部面板上的「刪除」按鈕。

配額考量

若要使用即時端點部署和推斷,您會取用依個別區域為基礎、指派給您訂用帳戶的虛擬機器 (VM) 核心配額。 當您註冊 AI Studio 時,您會針對該區域中提供的數個 VM 系列獲得預設的 VM 配額。 您可以繼續建立部署,直到達到配額限制為止。 一旦發生這種情況,您可以要求配額增加。

下一步