次の方法で共有


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

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

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

AI Playground を使用して、トークンごとの有料モデルをテストしてチャットすることもできます。 AI Playground を使用して LLM とチャットし、生成 AI アプリのプロトタイプを作成するを参照してください。

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

必要条件

重要

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

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

Foundation Model API の使用を開始する

次の例は、Databricks ノートブックで実行することを意図しています。 このコード例では、トークンごとの支払いエンドポイント で提供される databricks-meta-llama-3-1-405b-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-meta-llama-3-1-405b-instruct",
  max_tokens=256
)

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

注記

メッセージ ImportError: cannot import name 'OpenAI' from 'openai' が表示された場合、openai を使用して !pip install -U openai バージョンをアップグレードします。 パッケージをインストールした後、dbutils.library.restartPython() を実行します。

予想される出力:


{
  "id": "xxxxxxxxxxxxx",
  "object": "chat.completion",
  "created": "xxxxxxxxx",
  "model": "databricks-meta-llama-3-1-405b-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
    }
}