在本文中,您將瞭解如何在與部署所在的工作區不同的工作區中設定現有的標準部署。
模型目錄中的某些模型可以部署為標準部署。 這種部署可讓您以 API 形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性與合規性。 此部署選項不需要您的訂用帳戶提供配額。
可能會出現需要在建立部署的工作區之外的其他工作區中取用標準部署的情形,例如以下幾種情況:
- 您想要在指定的工作區中集中您的部署,並從組織中不同工作區取用。
- 您必須在特定 Azure 區域中部署模型,而該區域可支援模型的無伺服器部署。 不過,您需要從另一個區域取用,而無伺服器部署在該區域不適用於特定模型。
必要條件
具有有效付款方式的 Azure 訂用帳戶。 免費版或試用版 Azure 訂用帳戶將無法運作。 如果您沒有 Azure 訂用帳戶,請建立付費 Azure 帳戶以開始。
一個您想要從中取用現有部署的 Azure Machine Learning 工作區。
部署至標準部署的模型。 本文假設您先前已部署 Meta-Llama-3-8B-Instruct 模型。 若要瞭解如何將此模型部署為標準部署,請參閱 將模型部署為標準部署。
您必須安裝下列軟體,才能使用 Azure Machine Learning:
Azure CLI 和 適用於 Azure Machine Learning 的 ml 延伸模組。
az extension add -n ml
如果您已安裝延伸模組,請確定您已安裝最新版本。
az extension update -n ml
安裝延伸模組後,請加以設定:
az account set --subscription <subscription>
az configure --defaults workspace=<workspace-name> group=<resource-group> location=<location>
安裝適用於 Python 的 Azure Machine Learning SDK。
pip install -U azure-ai-ml
安裝之後,請匯入所需的命名空間:
from azure.ai.ml import MLClient
from azure.identity import InteractiveBrowserCredential
from azure.ai.ml.entities import ServerlessEndpoint, ServerlessConnection
建立標準部署連線
請遵循這些步驟,以建立連線:
連線至部署端點的工作區:
設定 CLI 以指向工作區:
az account set --subscription <subscription>
az configure --defaults workspace=<workspace-name> group=<resource-group> location=<location>
建立連線至工作區的用戶端:
client = MLClient(
credential=InteractiveBrowserCredential(tenant_id="<tenant-id>"),
subscription_id="<subscription-id>",
resource_group_name="<resource-group>",
workspace_name="<workspace-name>",
)
取得您要連線之端點的端點 URL 和認證。 在這個範例中,您會取得端點名稱為 meta-llama3-8b-qwerty 的詳細資料。
從左側提要欄位選取 [端點]。
選取 [無伺服器端點] 索引標籤 以顯示標準部署。
選取想要連線的端點。
在端點的 [詳細資料] 索引標籤上,複製 [目標 URI] 和 [金鑰]的值。
az ml serverless-endpoint get-credentials -n meta-llama3-8b-qwerty
endpoint_name = "meta-llama3-8b-qwerty"
endpoint_keys = client.serverless_endpoints.get_keys(endpoint_name)
print(endpoint_keys.primary_key)
print(endpoint_keys.secondary_key)
現在,連線到您要在其中建立連線的工作區並取用端點。
在工作區中建立連線:
前往需要建立連線的工作區。
移至左窗格中的 [ 管理] 區段,然後選取 [ 連線]。
選取 建立。
選取 [無伺服器模型]。
在 [目標 URI]上貼上您先前複製的值。
在 [金鑰]上貼上您先前複製的值。
為連線指定名稱,此案例中為 meta-llama3-8b-connection。
請選取新增連線。
建立連線定義:
connection.yml
name: meta-llama3-8b-connection
type: serverless
endpoint: https://meta-llama3-8b-qwerty-serverless.inference.ai.azure.com
api_key: 1234567890qwertyuiop
az ml connection create -f connection.yml
client.connections.create_or_update(ServerlessConnection(
name="meta-llama3-8b-connection",
endpoint="https://meta-llama3-8b-qwerty-serverless.inference.ai.azure.com",
api_key="1234567890qwertyuiop"
))
現在,連線已可供取用。
若要驗證連線是否已在運作:
從 Azure Machine Learning Studio 的左窗格中,移至 [撰寫>提示流程]。
選取 [建立] 以建立新流程。
在 [聊天流程] 方塊中選取 [建立]。
為您的 [提示流程] 命名,並選取 [建立]。
從圖表中選取 [聊天] 節點,以前往 [聊天] 區段。
針對 [連線],請開啟挑選清單,選取您剛才建立的連線,此案例為 [meta-llama3-8b-connection]。
在頂端瀏覽列選取 [啟用計算工作階段],以啟用提示流程自動執行階段。
選取 [聊天] 選項。 您現在可以傳送訊息並取得回應。
相關內容