リクエストのタグ付け
Databricks-Ai-Gateway-Request-Tags HTTPヘッダーを使用して、個々のリクエストにカスタムのキーと値のタグを付加します。Unity Gatewayは、使用状況の追跡システムテーブルと推論テーブルの両方のrequest_tags列にリクエストタグをLogsします。これにより、コストを追跡し、使用状況を帰属させ、プロジェクト、チーム、環境、その他のディメンションでアナリティクスをフィルタリングできます。
ヘッダーの値は、文字列のキーを文字列の値にマッピングするJSONオブジェクトである必要があります。例:
JSON
{ "project": "chatbot", "team": "ml-platform", "environment": "production" }
モデル サービス リクエストにタグを付ける
extra_headers パラメーターを使用する (Python)、またはヘッダーを直接渡す (REST API) ことで、モデルサービスのリクエストにタグをアタッチします:
- Python (OpenAI SDK)
- Python (Anthropic SDK)
- REST API
Python
from openai import OpenAI
import json
import os
DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url="https://<workspace-url>/ai-gateway/mlflow/v1"
)
request_tags = {"project": "chatbot", "team": "ml-platform"}
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": "What is Databricks?"},
],
model="<model-service>",
max_tokens=256,
extra_headers={
"Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags)
}
)
Python
import anthropic
import json
import os
DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')
request_tags = {"project": "chatbot", "team": "ml-platform"}
client = anthropic.Anthropic(
api_key="unused",
base_url="https://<workspace-url>/ai-gateway/anthropic",
default_headers={
"Authorization": f"Bearer {DATABRICKS_TOKEN}",
"Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags),
},
)
message = client.messages.create(
model="<model-service>",
max_tokens=256,
messages=[
{"role": "user", "content": "What is Databricks?"},
],
)
Bash
curl \
-u token:$DATABRICKS_TOKEN \
-X POST \
-H "Content-Type: application/json" \
-H 'Databricks-Ai-Gateway-Request-Tags: {"project": "chatbot", "team": "ml-platform"}' \
-d '{
"model": "<model-service>",
"max_tokens": 256,
"messages": [
{"role": "user", "content": "What is Databricks?"}
]
}' \
https://<workspace-url>/ai-gateway/mlflow/v1/chat/completions
<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。
モデルプロバイダーサービスのリクエストにタグを付ける
モデルプロバイダーサービスに対して直接クエリーを実行する場合は、Databricks-Model-Provider-Service ヘッダーとともに tags ヘッダーを送信します。
- Python
- REST
Python
from openai import OpenAI
import json
client = OpenAI(
api_key="<databricks-token>",
base_url="https://<workspace-url>/ai-gateway/openai/v1",
default_headers={"Databricks-Model-Provider-Service": "main.default.openai_prod"},
)
request_tags = {"project": "chatbot", "team": "ml-platform"}
response = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "What is Databricks?"}],
extra_headers={"Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags)},
)
Bash
curl https://<workspace-url>/ai-gateway/openai/v1/chat/completions \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Databricks-Model-Provider-Service: main.default.openai_prod" \
-H "Content-Type: application/json" \
-H 'Databricks-Ai-Gateway-Request-Tags: {"project": "chatbot", "team": "ml-platform"}' \
-d '{
"model": "gpt-5.5",
"messages": [{"role": "user", "content": "What is Databricks?"}]
}'