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

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

この記事では、基盤モデルAPIを使用してDatabricksでLLMを提供およびクエリする方法について説明します。

LLMDatabricksで モデルの提供とクエリを開始する最も簡単な方法は、トークン単位の従量課金 ベースで 基盤モデルを使用することです。APIsこの APIs は、 Databricks ワークスペースの Serving UI で自動的に利用可能になるトークン単位の従量課金エンドポイントから一般的な基盤モデルへのアクセスを提供します。 Databricks 基盤モデルのサポート対象モデルを参照してください APIs

また、 AI Playgroundを使用してトークン単位の従量課金モデルをテストしてチャットすることもできます。 LLM とのチャットおよびAI Playgroundを使用した生成AI アプリのプロトタイピング を参照してください。

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

必要条件

important

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

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

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

次の例は、Databricks ノートブックで実行することを目的としています。 このコード例では、トークン単位の従量課金エンドポイント databricks-meta-llama-3-1-405b-instruct で提供される Meta Llama 3.1 405B Instruct モデルに対してクエリを実行します。

この例では、OpenAI クライアントを使用して、クエリを実行するモデルをホストするモデルサービングエンドポイントの名前を model フィールドに入力することで、モデルをクエリします。 個人用アクセス トークンを使用して DATABRICKS_TOKEN API と Databricks ワークスペース インスタンス を設定し、OpenAI クライアントを Databricks に接続します。

Python
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' が表示された場合は、 !pip install -U openai を使用して openai のバージョンをアップグレードします。 パッケージをインストールしたら、 dbutils.library.restartPython() を実行します。

期待される出力:

Bash

{
"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
}
}

次のステップ