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

この記事では、基盤モデルAPIsを使用して Databricks 上で LLM の提供とクエリを開始する方法について説明します。

Databricks で LLM モデルの提供とクエリを開始する最も簡単な方法は、 トークンごとの支払い ベースで プラットフォーム APIsを使用することです。APIs使用すると、Databricks ワークスペースの Serving UI で自動的に利用できるトークンごとの支払いエンドポイントから、一般的な基盤モデルへのアクセスが提供されます。 「ペイパートークンのサポート対象モデル」を参照してください。

AI Playground を使用して、トークンごとの支払いモデルをテストしたり、チャットしたりすることもできます。 「AI Playground を使用してサポートされている LLM とチャットする」を参照してください。

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

要件

重要

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

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

基盤モデルAPIsの使用を開始する

次の例では、トークンごとの支払いエンドポイントdatabricks-dbrx-instructで提供されるdatabricks-dbrx-instructモデルをクエリします。 DBRX Instruct モデルの詳細については、こちらを参照してください。

この例では、OpenAI クライアントを使用して、クエリするモデルをホストするモデルサービング エンドポイントの名前をmodelフィールドに入力することでモデルをクエリします。 個人のアクセスウイルスを使用してDATABRICKS_TOKENを設定し、 Databricks ワークスペース インスタンスを使用して 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
    }
}

次のステップ