Databricks で LLM のクエリを開始する

この記事では、Databricks で Foundation Model API を使って LLM の提供とクエリの実行を始める方法について説明します。

Databricks で LLM モデルの提供とクエリを始める最も簡単な方法は、トークン単位の支払いベースで Foundation Model API を使うことです。 この API は、Databricks ワークスペースのサービス UI で自動的に使用できるようになるトークン単位の支払いエンドポイントから、一般的な基盤モデルへのアクセスを提供します。 「トークン単位の支払いでサポートされているモデル」をご覧ください。

AI Playground を使用して、トークンごとの有料モデルをテストしてチャットすることもできます。 「AI プレイグラウンドを使ってサポートされている LLM とチャットする」をご覧ください。

運用環境のワークロードの場合、特にパフォーマンスの保証が必要な微調整されたモデルまたはワークロードがある場合は、Databricks では、プロビジョニングされたスループット エンドポイントでの Foundation Model API の使用にアップグレードすることをお勧めします。

要件

重要

運用シナリオのセキュリティのベスト プラクティスとして、Databricks では、運用時の認証にコンピューター間 OAuth トークンを使用することをお勧めします。

テストおよび開発の場合は、Databricks では、ワークスペース ユーザーではなく、サービス プリンシパルに属する個人用アクセス トークンを使用することをお勧めします。 サービス プリンシパルのトークンを作成するには、「サービス プリンシパルのトークンを管理する」をご覧ください。

Foundation Model API の使用を開始する

次の例では、トークン単位の支払いエンドポイント (databricks-dbrx-instruct) で提供される databricks-dbrx-instruct モデルのクエリを実行します。 DBRX Instruct モデルの詳細を確認します。

この例では、OpenAI クライアントを使い、model フィールドに、クエリ対象のモデルをホストするモデル提供エンドポイントの名前を設定して、モデルのクエリを実行します。 個人用アクセス トークンを使用して、 DATABRICKS_TOKENDatabricks ワークスペース インスタンス を設定して、OpenAI クライアントを Databricks に接続します。

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_id>.databricks.com/serving-endpoints', # your Databricks workspace instance
)

chat_completion = client.chat.completions.create(
  messages=[
    {
      "role": "system",
      "content": "You are an AI assistant",
    },
    {
      "role": "user",
      "content": "What is a mixture of experts model?",
    }
  ],
  model="databricks-dbrx-instruct",
  max_tokens=256
)

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

予想される出力:


{
  "id": "xxxxxxxxxxxxx",
  "object": "chat.completion",
  "created": "xxxxxxxxx",
  "model": "databricks-dbrx-instruct",
  "choices": [
    {
      "index": 0,
      "message":
        {
          "role": "assistant",
          "content": "A Mixture of Experts (MoE) model is a machine learning technique that combines the predictions of multiple expert models to improve overall performance. Each expert model specializes in a specific subset of the data, and the MoE model uses a gating network to determine which expert to use for a given input."
        },
      "finish_reason": "stop"
    }
  ],
  "usage":
    {
      "prompt_tokens": 123,
      "completion_tokens": 23,
      "total_tokens": 146
    }
}

次のステップ