DatabricksでAIモデルにクエリを実行する

Databricks と Python SDK

Avi Steinberg

Senior Software Engineer

Foundationモデル

Foundationモデルの画像

  • 大規模な事前学習済みニューラルネットワーク
  • 多様な大規模データセットで学習
  • Databricks内外でホスト可能
1 https://docs.databricks.com/aws/en/machine-learning/model-serving/foundation-model-overview
Databricks と Python SDK

主なチャット補完モデル

Databricks外でホスト:

  • OpenAIのChatGPT
  • AnthropicのClaude
  • Google CloudのGemini

OpenAI ChatGPT

Anthropic Claude

Google Gemini

Databricks上でホスト:

  • Meta Llama

$$

Databricks Meta Llama

Databricks と Python SDK

Databricks Meta Llama

  • MetaはDatabricks上でMeta LlamaをホストするためにDatabricksと提携
  • MetaによってビルドおよびトレーニングされたLLM
  • 性能面でOpenAI GPTと競合

Databricks Meta Llama

1 1. https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/supported-models#meta-llama-3-3-70b-instruct 2. https://www.databricks.com/product/pricing/foundation-model-serving
Databricks と Python SDK

サービングエンドポイント

  • DatabricksホストのFoundationモデルに既存
  • 外部モデルおよびカスタムモデルにも作成可能
  • Databricks SDKを使用してFoundationモデルにクエリ可能

サービングエンドポイント

1 https://docs.databricks.com/api/workspace/servingendpoints
Databricks と Python SDK

サービングエンドポイントAPI

  • 対応するサービングエンドポイントへのAPIリクエストでAIモデルにクエリ可能
  • WorkspaceClientserving_endpoints属性からアクセス
1 https://docs.databricks.com/api/workspace/servingendpoints
Databricks と Python SDK

チャットメッセージロール

SYSTEM:

モデルへの応答方法を指示する

USER:

ユーザーがモデルに送信するクエリ

プロンプト例:

"You are a helpful python coding assistant."

プロンプト例:

"How do for loops work in Python?"

1 https://databricks-sdk-py.readthedocs.io/en/stable/dbdataclasses/serving.html#databricks.sdk.service.serving.ChatMessageRole
Databricks と Python SDK

Databricksでチャットモデルにクエリを実行する

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ChatMessage, ChatMessageRole
w = WorkspaceClient()
response = w.serving_endpoints.query(
    name="databricks-meta-llama-3-3-70b-instruct",
    messages=[
        ChatMessage(
            role=ChatMessageRole.SYSTEM, content="You are a helpful assistant."
        ),
        ChatMessage(role=ChatMessageRole.USER, content="<your-question>"),
    ],
    max_tokens=128)
print(f"RESPONSE:\n{response.choices[0].message.content}")
1 https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/supported-models#meta-llama-33-70b-instruct
Databricks と Python SDK

さあ、練習しましょう!

Databricks と Python SDK

Preparing Video For Download...