在本文中,您將瞭解如何在不同於用來建立部署的專案或中樞中設定現有的無伺服器 API。
這很重要
處於預覽狀態的模型會在模型目錄中的模型卡片上標示為 預覽 。
模型目錄中的特定模型可以部署為無伺服器 API。 這種部署可讓您以 API 的形式取用模型,而不必在您的訂用帳戶上裝載模型,同時讓組織保持所需的企業安全性和合規性。 此部署選項不需要您的訂用帳戶提供配額。
在不同於用於建立部署的專案或中樞内取用無伺服器 API 端點的需求,可能會發生下列情況:
- 您想要將部署集中到指定的專案或中樞,並從組織中的不同專案或中樞取用它們。
- 您必須在特定 Azure 區域中的中樞部署模型,而該模型在該區域可使用無伺服器部署。 不過,您需要從另一個區域取用它,但這特定模型在此區域無法使用無伺服器部署。
先決條件
具有有效付款方式的 Azure 訂用帳戶。 免費版或試用版 Azure 訂用帳戶將無法運作。 如果您沒有 Azure 訂用帳戶,請建立付費 Azure 帳戶以開始。
如果您使用中樞型專案,則為 Microsoft Foundry 中樞 (部分內容可能是機器或 AI 翻譯)。
Foundry 專案不使用集線器。 如需詳細資訊,請參閱 項目類型。
如果您沒有專案, 請建立以中樞為基礎的專案。
部署至無伺服器 API 的模型。 本文假設您先前已部署 Meta-Llama-3-8B-Instruct 模型。 若要了解如何將此模型部署為無伺服器 API,請參閱 將模型部署為無伺服器 API。
你需要安裝以下軟體才能使用 Foundry:
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=<project-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
建立無伺服器 API 端點連線
請遵循這些步驟,以建立連線:
連線至部署端點的專案或中樞:
小提示
因為你可以自訂 Microsoft Foundry 入口網站的 左側窗格 ,你可能會看到與這些步驟中顯示不同的項目。 如果您沒有看到您要尋找的內容,請選取左窗格底部的 ... 更多。
進入 Foundry ,並導向你想連接的端點所在的專案。
設定 CLI 以指向專案:
az account set --subscription <subscription>
az configure --defaults workspace=<project-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="<project-name>",
)
取得您要連線的端點 URL 和憑證。 在這個範例中,您會取得名稱為 meta-llama3-8b-qwerty 的端點的詳細資料。
在 Foundry 入口網站的專案左側欄,前往 「我的資產>模型+端點」 ,查看專案中的部署清單。
選取您要連線至的部署。
複製 目標 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)
現在,連線到您要在其中建立連線的專案或中樞:
設定 CLI 以指向專案:
az account set --subscription <subscription>
az configure --defaults workspace=<project-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="<project-name>",
)
在專案中建立連線:
從 Foundry 入口網站的專案中,前往左側側邊欄底部,選擇 管理中心。
從管理中心的左側提要字段中,選取 [ 已連線的資源]。
選取新增關係。
選取 [無伺服器模型]。
在 [目標 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"
))
現在,連線已可供取用。
若要驗證連線是否已在運作:
返回 Foundry 協作平台中的專案。
從項目左側的側邊欄中,前往建立並自訂>提示流程。
選取 [建立] 以建立新流程。
在 聊天流程 方塊中選取 建立。
為您的 [提示流程] 命名,並選取 [建立]。
從圖表中選取 [聊天] 節點,以前往 [聊天] 區段。
針對 [連線],請開啟挑選清單,選取您剛才建立的連線,此案例為 [meta-llama3-8b-connection]。
在頂端瀏覽列選取 [啟用計算工作階段],以啟用提示流程自動執行階段。
選取 [聊天] 選項。 您現在可以傳送訊息並取得回應。
相關內容