Abfrage der Unity AI Gateway-Endpunkte

Von Bedeutung

Dieses Feature befindet sich in der Betaversion. Kontoadministratoren können den Zugriff auf dieses Feature über die Seite " Vorschau" der Kontokonsole steuern. Siehe Manage Azure Databricks Previews.

Auf dieser Seite wird beschrieben, wie Sie Unity AI Gateway-Endpunkte mithilfe unterstützter APIs abfragen.

Anforderungen

Unterstützte APIs und Integrationen

Unity AI Gateway unterstützt die folgenden APIs und Integrationen:

Abfrageendpunkte mit ai_query

Sie können die funktion ai_query verwenden, um Azure Databricks bereitgestellten Unity AI Gateway-Endpunkte direkt aus SQL oder Python abzufragen. Dadurch können Sie Informationen zur Nutzungsverfolgung für Ihre Batch-Inferenz-Workloads erfassen.

Note

  • ai_query-Unterstützung für Unity AI Gateway ist nur für Azure Databricks bereitgestellte Endpunkte verfügbar (z. B. databricks-gpt-5-4 oder databricks-claude-sonnet-4). Endpunkte, die Sie in Unity AI Gateway erstellen, werden noch nicht unterstützt.
  • Nur Nutzungsverfolgung gilt für ai_query Batch-Inferenz-Workloads. Andere Funktionen des Unity AI Gateway wie Ratenbegrenzungen, Guardrails, Inferenztabellen und Fallbacks finden keine Anwendung.

Um loszulegen:

  1. Aktivieren Sie die Unity AI Gateway-Vorschau für Ihr Konto. Siehe Manage Azure Databricks Previews.
  2. Abfragen eines von Azure Databricks bereitgestellten Endpunkts mithilfe von ai_query:
SELECT ai_query(
  'databricks-gpt-5-4',
  'Summarize the following text: ' || text_column
) AS summary
FROM my_table
LIMIT 10

Anfragen, die über ai_query an von Azure Databricks bereitgestellte Endpunkte gesendet werden, werden in der Systemtabelle zur Nutzungsverfolgung (system.ai_gateway.usage) erfasst. Diese Anforderungen werden auch im integrierten Verwendungsdashboard angezeigt.

Vollständige ai_query Syntax- und Parameterreferenz finden Sie unter ai_query Funktion. Bewährte Methoden und unterstützte Modelle finden Sie unter "Verwenden" ai_query.

Abfrageendpunkte mit einheitlichen APIs

Einheitliche APIs bieten eine openAI-kompatible Schnittstelle für Abfragemodelle auf Azure Databricks. Verwenden Sie einheitliche APIs, um nahtlos zwischen Modellen von verschiedenen Anbietern zu wechseln, ohne Ihren Code zu ändern.

MLflow-Chatabschluss-API

MLflow-Chatabschluss-API

Python

from openai import OpenAI
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = OpenAI(
  api_key=DATABRICKS_TOKEN,
  base_url="https://<workspace-url>/ai-gateway/mlflow/v1"
)

chat_completion = client.chat.completions.create(
  messages=[
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hello! How can I assist you today?"},
    {"role": "user", "content": "What is Databricks?"},
  ],
  model="<ai-gateway-endpoint>",
  max_tokens=256
)

print(chat_completion.choices[0].message.content)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<ai-gateway-endpoint>",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "Hello!"},
      {"role": "assistant", "content": "Hello! How can I assist you today?"},
      {"role": "user", "content": "What is Databricks?"}
    ]
  }' \
  https://<workspace-url>/ai-gateway/mlflow/v1/chat/completions

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

MLflow Embeddings-API

Die MLflow-Embeddings-API

Python

from openai import OpenAI
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = OpenAI(
  api_key=DATABRICKS_TOKEN,
  base_url="https://<workspace-url>/ai-gateway/mlflow/v1"
)

embeddings = client.embeddings.create(
  input="What is Databricks?",
  model="<ai-gateway-endpoint>"
)

print(embeddings.data[0].embedding)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<ai-gateway-endpoint>",
    "input": "What is Databricks?"
  }' \
  https://<workspace-url>/ai-gateway/mlflow/v1/embeddings

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Supervisor-API

Supervisor-API

Die Supervisor-API (/mlflow/v1/responses) ist eine openResponses-kompatible, anbieteragnostische API zum Erstellen von Agents in Beta. Kontoadministratoren können den Zugriff über die Vorschauseite aktivieren. Siehe Manage Azure Databricks Previews. Wählen Sie das beste Modell für Ihren Agent-Anwendungsfall für Anbieter aus, ohne Ihren Code zu ändern.

Python

from openai import OpenAI
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = OpenAI(
  api_key=DATABRICKS_TOKEN,
  base_url="https://<workspace-url>/ai-gateway/mlflow/v1"
)

response = client.responses.create(
  model="<ai-gateway-endpoint>",
  input=[{"role": "user", "content": "What is Databricks?"}]
)

print(response.output_text)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<ai-gateway-endpoint>",
    "input": [
      {"role": "user", "content": "What is Databricks?"}
    ]
  }' \
  https://<workspace-url>/ai-gateway/mlflow/v1/responses

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Abfragen von Endpunkten mit systemeigenen APIs

Systemeigene APIs bieten anbieterspezifische Schnittstellen für Abfragemodelle auf Azure Databricks. Verwenden Sie systemeigene APIs, um auf die neuesten anbieterspezifischen Features zuzugreifen.

OpenAI-Antwort-API

OpenAI-Antworten-API

Python

from openai import OpenAI
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = OpenAI(
  api_key=DATABRICKS_TOKEN,
  base_url="https://<workspace-url>/ai-gateway/openai/v1"
)

response = client.responses.create(
  model="<ai-gateway-endpoint>",
  max_output_tokens=256,
  input=[
    {
      "role": "user",
      "content": [{"type": "input_text", "text": "Hello!"}]
    },
    {
      "role": "assistant",
      "content": [{"type": "output_text", "text": "Hello! How can I assist you today?"}]
    },
    {
      "role": "user",
      "content": [{"type": "input_text", "text": "What is Databricks?"}]
    }
  ]
)

print(response.output)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<ai-gateway-endpoint>",
    "max_output_tokens": 256,
    "input": [
      {
        "role": "user",
        "content": [{"type": "input_text", "text": "Hello!"}]
      },
      {
        "role": "assistant",
        "content": [{"type": "output_text", "text": "Hello! How can I assist you today?"}]
      },
      {
        "role": "user",
        "content": [{"type": "input_text", "text": "What is Databricks?"}]
      }
    ]
  }' \
  https://<workspace-url>/ai-gateway/openai/v1/responses

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Anthropic Nachrichten-API

Anthropische Nachrichten-API

Python

import anthropic
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = anthropic.Anthropic(
  api_key="unused",
  base_url="https://<workspace-url>/ai-gateway/anthropic",
  default_headers={
    "Authorization": f"Bearer {DATABRICKS_TOKEN}",
  },
)

message = client.messages.create(
  model="<ai-gateway-endpoint>",
  max_tokens=256,
  messages=[
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hello! How can I assist you today?"},
    {"role": "user", "content": "What is Databricks?"},
  ],
)

print(message.content[0].text)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<ai-gateway-endpoint>",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "Hello!"},
      {"role": "assistant", "content": "Hello! How can I assist you today?"},
      {"role": "user", "content": "What is Databricks?"}
    ]
  }' \
  https://<workspace-url>/ai-gateway/anthropic/v1/messages

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Google Gemini-API

Google Gemini-API

Python

from google import genai
from google.genai import types
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = genai.Client(
  api_key="databricks",
  http_options=types.HttpOptions(
    base_url="https://<workspace-url>/ai-gateway/gemini",
    headers={
      "Authorization": f"Bearer {DATABRICKS_TOKEN}",
    },
  ),
)

response = client.models.generate_content(
  model="<ai-gateway-endpoint>",
  contents=[
    types.Content(
      role="user",
      parts=[types.Part(text="Hello!")],
    ),
    types.Content(
      role="model",
      parts=[types.Part(text="Hello! How can I assist you today?")],
    ),
    types.Content(
      role="user",
      parts=[types.Part(text="What is Databricks?")],
    ),
  ],
  config=types.GenerateContentConfig(
    max_output_tokens=256,
  ),
)

print(response.text)

REST API

curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "Hello!"}]
      },
      {
        "role": "model",
        "parts": [{"text": "Hello! How can I assist you today?"}]
      },
      {
        "role": "user",
        "parts": [{"text": "What is Databricks?"}]
      }
    ],
    "generationConfig": {
      "maxOutputTokens": 256
    }
  }' \
  https://<workspace-url>/ai-gateway/gemini/v1beta/models/<ai-gateway-endpoint>:generateContent

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Tag-Anforderungen für die Verwendungsnachverfolgung

Sie können einzelnen Anfragen benutzerdefinierte Schlüssel-Wert-Tags mithilfe des HTTP-Headers Databricks-Ai-Gateway-Request-Tags hinzufügen. Anforderungstags werden in der request_tags Spalte sowohl in der Tabelle des Verwendungsverfolgungssystems als auch in der Ableitungstabelle protokolliert, sodass Sie Kosten, Attributnutzung und Filteranalysen nach Projekt, Team, Umgebung oder einer anderen Dimension nachverfolgen können.

Der Header-Wert muss ein JSON-Objekt sein, das String-Schlüssel String-Werten zuordnet. Beispiel:

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

Verwenden Sie den parameter extra_headers (Python) oder übergeben Sie den Header direkt (REST-API), um Tags an eine Anforderung anzufügen:

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="<ai-gateway-endpoint>",
  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="<ai-gateway-endpoint>",
  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": "<ai-gateway-endpoint>",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "What is Databricks?"}
    ]
  }' \
  https://<workspace-url>/ai-gateway/mlflow/v1/chat/completions

Ersetzen Sie <workspace-url> durch Ihre AZURE DATABRICKS Arbeitsbereichs-URL und <ai-gateway-endpoint> durch ihren Unity AI Gateway-Endpunktnamen.

Nächste Schritte

  • Supervisor-API (Beta) – Ausführen von Multi-Turn-Agent-Workflows mit gehosteten Tools über /mlflow/v1/responses