本文說明如何擷取適當的驗證認證和 URL,以便查詢路由最佳化 模型服務 或 功能服務 端點。
需求
- 已啟用路由最佳化的模型服務端點或功能服務端點。 請參閱 服務端點的路由優化。
- 查詢路由最佳化端點僅支援使用 OAuth 令牌。 不支援個人存取權杖。
取得路徑最佳化的 URL
警告
從 2025 年 9 月 22 日開始,所有新建立的路由最佳化端點都必須專門透過路由最佳化 URL 進行查詢。 在此日期之後建立的端點不支援透過工作區 URL 進行查詢。
如果您的路由最佳化端點是在 2025 年 9 月 22 日之前建立的:
標準工作區 URL 也可用於查詢端點。 標準工作區 URL 路徑不提供 路由最佳化的優點。
https://<databricks-workspace>/serving-endpoints/<endpoint-name>/invocations在此日期之前建立的路由最佳化端點會繼續支援這兩個叫用 URL:路由最佳化 URL 路徑和標準工作區 URL 路徑。
當您建立路由最佳化端點時,會為端點建立下列路由最佳化 URL。
https://<unique-id>.<shard>.serving.azuredatabricks.net/<workspace-id>/serving-endpoints/<endpoint-name>/invocations
您可以從以下位置取得此 URL:
提供使用者介面
REST API
使用 GET /api/2.0/serving-endpoints/{name} API 呼叫。 URL 存在於端點的回應物件中,做為 endpoint_url。 只有在終端經過路由最佳化時,才會填入此欄位。
GET /api/2.0/serving-endpoints/my-endpoint
{
"name": "my-endpoint"
}
Databricks SDK
使用 Serving Endpoints API get 呼叫功能。 URL 存在於端點的回應物件中,做為 endpoint_url。 只有在終端經過路由最佳化時,才會填入此欄位。
from databricks.sdk import WorkspaceClient
workspace = WorkspaceClient()
workspace.serving_endpoints.get("my-endpoint")
擷取 OAuth 令牌並查詢端點
若要查詢經最佳化路由的端點,您必須使用 OAuth 令牌。 Databricks 建議在生產應用程式中使用 服務主體,以程式方式獲取 OAuth 權杖。 下列各節說明如何取得測試與生產環境的 OAuth 令牌之建議指引。
使用 Serving UI 取得 OAuth 令牌
下列步驟示範如何在服務 UI 中擷取權杖。 建議這些步驟用於開發和測試您的端點。
針對生產用途,例如在應用程式中使用路由最佳化的端點,系統會透過服務主體來擷取您的 Token。 如需獲取在生產環境中使用的 OAuth 令牌的推薦指引,請參閱以程式方式取得 OAuth 令牌。
從工作區的 [服務 UI]:
- 在 [服務端點] 頁面上,選取路由最佳化端點以查看端點詳細資料。
- 在 [端點詳細數據] 頁面上,選取 [ 使用 ] 按鈕。
- 選取 擷取令牌 索引標籤。
- 選取 [擷取 OAuth 令牌] 按鈕。 此權杖的有效期限為 1 小時。 如果您的目前的權杖過期,請擷取新的權杖。
擷取 OAuth 令牌之後,請使用您的端點 URL 和 OAuth 令牌來查詢端點。
REST API
以下是 REST API 範例:
URL="<endpoint-url>"
OAUTH_TOKEN="<token>"
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OAUTH_TOKEN" \
--data "@data.json" \
"$URL"
Python
以下是 Python 範例:
import requests
import json
url = "<url>"
oauth_token = "<token>"
data = {
"dataframe_split": {
"columns": ["feature_1", "feature_2"],
"data": [
[0.12, 0.34],
[0.56, 0.78],
[0.90, 0.11]
]
}
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {oauth_token}"
}
response = requests.post(url, headers=headers, json=data)
# Print the response
print("Status Code:", response.status_code)
print("Response Body:", response.text)
以程式設計方式擷取 OAuth 權杖
針對生產環境,Databricks 建議設定服務主體以嵌入您的應用程式中,並以程式化方式獲取 OAuth 令牌。 這些擷取的令牌可用來查詢最佳路由端點。
請遵循使用 OAuth 授權服務主體存取 Azure Databricks 中的步驟,至步驟 2 建立服務主體、指派許可權,以及建立服務主體的 OAuth 秘密。 建立服務主體之後,您必須至少為服務主體提供端點的 查詢許可權 。 請參閱 管理模型服務端點的許可。
Databricks Python SDK 提供 API,可直接查詢路由最佳化端點。
備註
Databricks SDK 也可在 Go 中使用,請參閱 Databricks SDK for Go。
下一個範例需要下列專案,才能使用 Databricks SDK 查詢路由最佳化端點:
- 提供端點名稱 (SDK 會根據此名稱擷取正確的端點 URL)
- 服務主體用戶端識別碼
- 服務主體憑證密碼
- 工作區主機名
from databricks.sdk import WorkspaceClient
import databricks.sdk.core as client
endpoint_name = "<Serving-Endpoint-Name>" ## Insert the endpoint name here
# Initialize Databricks SDK
c = client.Config(
host="<Workspace-Host>", ## For example, my-workspace.cloud.databricks.com
client_id="<Client-Id>", ## Service principal ID
client_secret="<Secret>" ## Service principal secret
)
w = WorkspaceClient(
config = c
)
response = w.serving_endpoints_data_plane.query(endpoint_name, dataframe_records = ....)
手動獲取 OAuth 令牌
針對 Databricks SDK 或服務 UI 無法用來擷取 OAuth 令牌的案例,您可以手動擷取 OAuth 令牌。 本節中的指引主要適用於使用者想要用來查詢生產中端點的自定義用戶端案例。
當您手動擷取 OAuth 令牌時,必須在要求中指定 authorization_details 。
- 在
<token-endpoint-URL>中,將https://<databricks-instance>替換為您的 Databricks 部署的工作區 URL,以建構https://<databricks-instance>/oidc/v1/token。 例如,https://my-workspace.0.azuredatabricks.net/oidc/v1/token
- 將
<client-id>替換為服務主體的客戶端 ID,也稱為應用程式識別碼。 - 將
<client-secret>取代為您建立的服務主體的 OAuth 密碼。
- 將
<endpoint-id>取代為路由最佳化端點的端點 ID。 這是您可以在端點 URL 中找到的端點字母數字識別碼hostName。 例如,如果服務端點為https://abcdefg.0.serving.azuredatabricks.net/9999999/serving-endpoints/test,則端點識別碼為abcdefg。
- 將
<action>取代為提供給服務主體的動作權限。 動作可以是query_inference_endpoint,也可以是manage_inference_endpoint。
REST API
以下是 REST API 範例:
export CLIENT_ID=<client-id>
export CLIENT_SECRET=<client-secret>
export ENDPOINT_ID=<endpoint-id>
export ACTION=<action> # for example, 'query_inference_endpoint'
curl --request POST \
--url <token-endpoint-URL> \
--user "$CLIENT_ID:$CLIENT_SECRET" \
--data 'grant_type=client_credentials&scope=all-apis'
--data-urlencode 'authorization_details=[{"type":"workspace_permission","object_type":"serving-endpoints","object_path":"'"/serving-endpoints/$ENDPOINT_ID"'","actions": ["'"$ACTION"'"]}]'
Python
以下是 Python 範例:
import os
import requests
# Set your environment variables or replace them directly here
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
ENDPOINT_ID = os.getenv("ENDPOINT_ID")
ACTION = "query_inference_endpoint" # Can also be `manage_inference_endpoint`
# Token endpoint URL
TOKEN_URL = "<token-endpoint-URL>"
# Build the payload, note the creation of authorization_details
payload = { 'grant_type': 'client_credentials', 'scope': 'all-apis', 'authorization_details': f'''[{{"type":"workspace_permission","object_type":"serving-endpoints","object_path":"/serving-endpoints/{ENDPOINT_ID}","actions":["{ACTION}"]}}]''' }
# Make the POST request with basic auth
response = requests.post( TOKEN_URL, auth=(CLIENT_ID, CLIENT_SECRET), data=payload )
# Check the response
if response.ok:
token_response = response.json()
access_token = token_response.get("access_token")
if access_token:
print(f"Access Token: {access_token}")
else:
print("access_token not found in response.")
else: print(f"Failed to fetch token: {response.status_code} {response.text}")
擷取 OAuth 令牌之後,請使用您的端點 URL 和 OAuth 令牌來查詢端點。
REST API
以下是 REST API 範例:
URL="<endpoint-url>"
OAUTH_TOKEN="<token>"
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OAUTH_TOKEN" \
--data "@data.json" \
"$URL"
Python
以下是 Python 範例:
import requests
import json
url = "<url>"
oauth_token = "<token>"
data = {
"dataframe_split": {
"columns": ["feature_1", "feature_2"],
"data": [
[0.12, 0.34],
[0.56, 0.78],
[0.90, 0.11]
]
}
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {oauth_token}"
}
response = requests.post(url, headers=headers, json=data)
# Print the response
print("Status Code:", response.status_code)
print("Response Body:", response.text)