Unity AI Gatewayを使って、Azure DatabricksモデルAPIを使ってUnityカタログ内のモデルサービスをクエリし、OpenAI互換SDKやネイティブプロバイダーAPI、またはSQLを利用できます。
メモ
モデルサービスが モデルプロバイダーのサービス 宛先にルーティングされると、そのモデルサービスのUnity AIゲートウェイ機能(レート制限、ガードレール、推論テーブル、フォールバックなど)のみが適用されます。 モデルプロバイダーサービス自体に設定されているUnity AI Gatewayの機能はスキップされます。
モデルAPIのクエリを始める
モデルAPIを2段階でクエリします:
ステップ1:すぐに使えるモデルAPIを選ぶ
Azure Databricks system.aiやsystem.ai.claude-sonnet-4-5のような、system.ai.gpt-5-6-solスキーマ内ですぐに使えるモデルAPIを提供しています。 これらは追加のセットアップなしですぐに利用可能です。
ステップ2:統一されたOpenAI互換APIを使ってリクエストを送信する
OpenAI Python SDKと共にMLflowチャット完了APIを活用してください:
from openai import OpenAI
import os
DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')
client = OpenAI(
api_key=DATABRICKS_TOKEN, # your personal access token
base_url="https://<workspace-url>/ai-gateway/mlflow/v1" # your Databricks workspace instance
)
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": "What is Databricks?"},
],
model="system.ai.claude-sonnet-4-5",
max_tokens=256
)
print(chat_completion.choices[0].message.content)
その他の選択肢については、 DatabricksでLLMクエリを始める方法をご覧ください。
Requirements
- Unity AI Gateway でサポートされているリージョン内のAzure Databricks ワークスペース。
- ワークスペースに対して有効になっている Unity カタログ。 「Unity Catalog のワークスペースを有効にする」を参照してください。
- クエリを実行するためのワークスペース権限: ワークスペース アクセス、またはアカウントで Unity AI Gateway へのコンシューマー アクセス プレビューが有効になっている コンシューマー アクセス(パブリック プレビュー)。 「Manage entitlements」および「Manage Azure Databricks previews」をご覧ください。
サポートされている API と統合
Unity AI Gateway では、次の API と統合がサポートされています。
- 統合 API: Azure Databricks 上のモデルに対してクエリを実行するための OpenAI 互換インターフェイス。 各モデルのクエリ方法を変更することなく、異なるプロバイダーのモデルをシームレスに切り替えます。
- ネイティブ API: 最新のモデルとプロバイダー固有の機能にアクセスするためのプロバイダー固有のインターフェイス。
- コーディング エージェント: コーディング エージェントを Unity AI Gateway と統合して、AI 支援型開発ワークフローに一元的なガバナンスと監視を追加します。 コーディング エージェントの統合を参照してください。
- Databricks Apps 上のエージェント: Unity AI Gateway 経由で LLM トラフィックをルーティングするエージェントを Databricks Apps で作成してデプロイします。 手順 4 を参照してください。Unity AI Gateway を使用して Databricks Apps 上のエージェントから LLM の使用を管理します。
-
ai_query:ai_queryを使用して、SQL または Python から Azure Databricks 提供のモデル サービスを照会し、バッチ推論を実行します。ai_queryを使用したモデル サービスのクエリを参照してください。
統合 API を使用してモデル サービスにクエリを実行する
統合 API は、Azure Databricks 上のモデルに対してクエリを実行するための OpenAI 互換インターフェイスを提供します。 統合 API を使用すると、コードを変更することなく、異なるプロバイダーのモデルをシームレスに切り替えることができます。
MLflow チャット補完 API
MLflow チャット出力候補 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="<model-service>",
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": "<model-service>",
"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
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
MLflow Embeddings API
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="<model-service>"
)
print(embeddings.data[0].embedding)
REST API
curl \
-u token:$DATABRICKS_TOKEN \
-X POST \
-H "Content-Type: application/json" \
-d '{
"model": "<model-service>",
"input": "What is Databricks?"
}' \
https://<workspace-url>/ai-gateway/mlflow/v1/embeddings
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
スーパーバイザーAPI
スーパーバイザーAPI
Supervisor API (/mlflow/v1/responses) は、ベータ版でエージェントを構築するための OpenResponses 互換のプロバイダーに依存しない API です。 ワークスペース管理者は プレビュー ページから有効にできます。
Manage Azure Databricks プレビューを参照してください。 コードを変更せずに、プロバイダー間でエージェントのユース ケースに最適なモデルを選択します。
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="<model-service>",
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": "<model-service>",
"input": [
{"role": "user", "content": "What is Databricks?"}
]
}' \
https://<workspace-url>/ai-gateway/mlflow/v1/responses
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
を使用してモデル サービスにクエリを実行する ai_query
ai_query関数を使って、SQLやPythonから直接モデルサービスをクエリできます。 これにより、バッチ推論ワークロードの 使用状況追跡 情報をキャプチャできます。
メモ
-
ai_queryUnity AI Gatewayのサポートは、Azure Databricksが提供するモデルのみで利用可能です。 このページの他の場所で使われているdatabricks-モデルサービス名ではなく、databricks-claude-sonnet-4-5プレフィックス(例:databricks-gpt-5-6-solやsystem.ai)を含むAzure Databricks foundationモデルエンドポイント名をパスしてください。 Unity AI Gateway で作成したモデル サービスはまだサポートされていません。 -
使用状況の追跡は、
ai_queryバッチ推論ワークロードにのみ適用されます。 レート制限、ガードレール、推論テーブル、フォールバックなどのその他の Unity AI Gateway 機能は適用されません。
ai_queryを持つモデルサービスにクエリするには、モデルサービスに対してai_queryを実行します。
SELECT ai_query(
'databricks-claude-sonnet-4-5',
'Summarize the following text: ' || text_column
) AS summary
FROM my_table
LIMIT 10
使用追跡システムテーブル(system.ai_gateway.usage)は、サービスをモデル化するためにai_queryを通じて行われたリクエストをキャプチャします。 これらの要求は 、組み込みの使用状況ダッシュボードにも表示されます。
完全な ai_query 構文とパラメーターリファレンスについては、 ai_query 関数を参照してください。 ベスト プラクティスとサポートされているモデルについては、「ai_queryの使用」を参照してください。
ネイティブ API を使用してモデル サービスにクエリを実行する
ネイティブ API は、プロバイダー固有のインターフェイスを提供して、Azure Databricks 上のモデルに対してクエリを実行します。 ネイティブ API を使用して、プロバイダー固有の最新の機能にアクセスします。
各ネイティブ API は、基になるモデルが一致する API 形式を使用するモデル サービスでのみ機能します。
- OpenAI Responses API を使用して、OpenAI (GPT) モデルによってサポートされるモデル サービスにクエリを実行します。
- Anthropic Messages API を使用して、Claude モデルによってサポートされるモデル サービスにクエリを実行します。
- Google Gemini API を使用して、Gemini モデルに基づくモデル サービスにクエリを実行します。
基になるモデルに関係なくモデル サービスに対してクエリを実行するには、代わりに 統合 API を 使用します。
OpenAIレスポンスAPI
OpenAIレスポンス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="<model-service>",
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": "<model-service>",
"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
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
Anthropic Messages API
Anthropic メッセージ 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="<model-service>",
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": "<model-service>",
"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
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
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="<model-service>",
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/<model-service>:generateContent
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。
使用状況追跡のタグ要求
Databricks-Ai-Gateway-Request-Tags HTTP ヘッダーを使用して、個々の要求にカスタム キー値タグをアタッチできます。 要求タグはrequest_tagsシステム テーブルと推論テーブルの両方の列に記録されるため、プロジェクト、チーム、環境、またはその他のディメンション別にコスト、属性の使用状況、フィルター分析を追跡できます。
ヘッダー値は、文字列値に文字列キーをマッピングする JSON オブジェクトである必要があります。 例えば次が挙げられます。
{ "project": "chatbot", "team": "ml-platform", "environment": "production" }
extra_headers パラメーター (Python) を使用するか、ヘッダーを直接渡して (REST API) 要求にタグをアタッチします。
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
<workspace-url>をAzure Databricksワークスペースの URL に置き換え、<model-service>をモデル サービスの完全修飾名に置き換えます。