Etichettatura delle richieste

Assegnare tag chiave-valore personalizzati a singole richieste utilizzando l'intestazione Databricks-Ai-Gateway-Request-Tags HTTP. Unity Gateway registra i tag delle richieste nella colonna request_tags sia nella tabella del sistema di tracciamento dell'uso che nelle tabelle di inferenza, permettendoti di tracciare i costi, attribuire l'utilizzo e filtrare le analisi per progetto, team, ambiente o altra dimensione.

Nelle richieste in cui vengono applicate le policy di servizio, le policy personalizzate possono anche valutare questi tag come event:context.request_tags. Ad esempio, una policy può richiedere la presenza di un tag prima di consentire una richiesta. Vedi i tag di richiesta nelle politiche di servizio.

Il valore dell'intestazione deve essere un oggetto JSON che associa chiavi stringa a valori stringa. Ad esempio:

{ "project": "chatbot", "team": "ml-platform", "environment": "production" }

Assegna un tag a una richiesta di servizio del modello

Usa il parametro extra_headers (Python) o passa direttamente l'intestazione (REST API) per associare i tag a una richiesta del servizio di modello:

Python (OpenAI SDK)

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 (Anthropic SDK)

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?"},
  ],
)

REST API

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

Sostituire <workspace-url> con l'URL dell'area di lavoro Azure Databricks e <model-service> con il nome completo del servizio modello.

Etichetta una richiesta di servizio del provider del modello

Quando interroghi direttamente un servizio di fornitore di modello, invia l'intestazione del tag accanto all'intestazione Databricks-Model-Provider-Service :

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)},
)

REST

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?"}]
  }'