メインコンテンツまでスキップ

モデルサービスをクエリする

備考

ベータ版

この機能はベータ版です。アカウント管理者は、アカウント コンソールの [プレビュー] ページからこの機能へのアクセスを制御できます。 Databricksのプレビューを管理するを参照してください。

このページでは、サポートされているAPIsを使用してUnity Catalogでモデルサービスをクエリする方法について説明します。

要件

Supported APIs and integrations

Unity AI Gatewayは、次のAPIsと統合をサポートしています:

モデルサービスをクエリーする ai_query

ai_query関数を使用して、SQLまたはPythonからDatabricksが提供するモデルサービスを直接クエリできます。これにより、バッチ推論ワークロードのために、使用状況追跡情報を取得できます。

注記
  • ai_query Unity AI Gateway のサポートは、Databricks が提供するモデルサービスにのみ利用できます(たとえば、databricks-gpt-5-4 または databricks-claude-sonnet-4)。Unity AI Gateway で作成したモデルサービスは、まだサポートされていません。
  • ai_queryバッチ推論ワークロードには、使用状況追跡のみが適用されます。レート制限、ガードレール、推論テーブル、フォールバックなどのその他のUnity AI Gateway機能は適用されません。

利用を開始するには以下の手順を踏みます。

  1. お使いのアカウントで Unity AI Gateway のプレビューを有効にします。「Databricks プレビューの管理」を参照してください。
  2. Databricks が提供するモデルサービスを ai_query を使用してクエリする:
SQL
SELECT ai_query(
'databricks-gpt-5-4',
'Summarize the following text: ' || text_column
) AS summary
FROM my_table
LIMIT 10

ai_queryを介してDatabricksが提供するモデルサービスに対して行われたリクエストは、使用状況追跡システムテーブル (system.ai_gateway.usage) にキャプチャされます。これらのリクエストは、組み込みの使用状況ダッシュボードにも表示されます。

詳細なai_query構文とパラメーターのリファレンスについては、ai_query関数を参照してください。ベストプラクティスとサポートされているモデルについては、Use ai_queryを参照してください。

統合されたAPIsでモデルサービスをクエリする

統合されたAPIsは、OpenAI互換のインターフェースを提供し、Databricks上のモデルをクエリします。統合されたAPIsを使用して、コードを変更することなく、異なるプロバイダーのモデル間をシームレスに切り替えることができます。

MLflow Chat Completions API

MLflow Chat Completions 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)

<workspace-url> を 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)

<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。

スーパーバイザーAPI

スーパーバイザー API

The Supervisor API (/mlflow/v1/responses) は、OpenResponses対応でプロバイダーに依存しない、ベータ版でエージェントを構築するための API です。アカウント管理者は、 [プレビュー] ページからアクセスを有効にできます。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)

<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。

ネイティブAPIsを使用したモデルサービスのクエリ

ネイティブAPIsは、Databricksでモデルをクエリするためのプロバイダー固有のインターフェイスを提供します。ネイティブAPIを使用して、最新のプロバイダー固有の機能にアクセスします。

OpenAI Responses API

OpenAI Responses 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)

<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。

Anthropic Messages API

Anthropic Messages 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={
&quot;Authorization&quot;: f&quot;Bearer {DATABRICKS_TOKEN}&quot;,
},
)

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)

<workspace-url> を 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={
&quot;Authorization&quot;: f&quot;Bearer {DATABRICKS_TOKEN}&quot;,
},
),
)

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)

<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。

使用状況の追跡のためにリクエストにタグを付ける

カスタムのキーと値のタグを Databricks-Ai-Gateway-Request-Tags HTTPヘッダーを使用して個々のリクエストにアタッチできます。リクエストタグは、使用状況追跡システムテーブルと推論テーブルの両方の request_tags 列にログ記録され、これにより、プロジェクト、チーム、環境、またはその他のディメンションによってコストの追跡、使用状況の割り当て、およびアナリティクスのフィルタリングが可能になります。

ヘッダー値は、文字列のキーを文字列の値にマッピングするJSONオブジェクトである必要があります。例えば:

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

リクエストにタグをアタッチするには、extra_headersパラメーター (Python) を使用するか、直接ヘッダーを渡します (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={
&quot;Databricks-Ai-Gateway-Request-Tags&quot;: json.dumps(request_tags)
}
)

<workspace-url> を Databricks ワークスペースの URL に置き換え、<model-service> をモデルサービスの完全修飾名に置き換えます。

次のステップ