チュートリアル: 外部モデル エンドポイントを作成して OpenAI モデルにクエリを実行する

この記事では、MLflow Deployments SDK を使って、入力候補、チャット、埋め込み用の OpenAI モデルを提供する外部モデル エンドポイントを構成して、クエリを実行するための詳細な手順について説明します。 外部モデルに関する詳細をご確認ください。

Serving UI を使用してこのタスクを実行したい場合は、「外部モデル提供エンドポイントの作成」を参照してください。

要件

  • Databricks Runtime 13.0 ML 以降。
  • MLflow 2.9 以上。
  • OpenAI API キー。
  • Databricks CLI バージョン 0.205 以降をインストールします。

(省略可能) 手順 0: Databricks シークレット CLI を使って OpenAI API キーを格納する

API キーは、手順 3 でプレーンテキスト文字列として、または Azure Databricks シークレットを使用して指定できます。

OpenAI API キーをシークレットとして格納するために、Databricks シークレット CLI (バージョン 0.205 以降) を使用できます。 また、シークレット用の REST API を使用することもできます。

次の例では、my_openai_secret_scope というシークレット スコープを作成し、そのスコープ内にシークレット openai_api_key を作成します。

databricks secrets create-scope my_openai_secret_scope
databricks secrets put-secret my_openai_secret_scope openai_api_key

手順 1: 外部モデルがサポートされている MLflow をインストールする

次のコマンドを使用して、外部モデルがサポートされている MLflow をインストールします。

%pip install mlflow[genai]>=2.9.0

手順 2: 外部モデル エンドポイントを作成して管理する

重要

このセクションのコード例では、パブリック プレビューの MLflow Deployments CRUD SDK の使用方法を示しています。

大規模言語モデル (LLM) の外部モデル エンドポイントを作成するには、MLflow Deployments SDK の create_endpoint() メソッドを使用します。 また、Serving UI 内で外部モデル エンドポイントを作成することもできます。

次のコード スニペットでは、構成の served_entities セクションで指定されているように、OpenAI gpt-3.5-turbo-instruct の入力候補エンドポイントを作成します。 エンドポイントに関しては、nameopenai_api_key に各フィールドで一意な値を設定するようにしてください。

import mlflow.deployments

client = mlflow.deployments.get_deploy_client("databricks")
client.create_endpoint(
    name="openai-completions-endpoint",
    config={
        "served_entities": [{
            "name": "openai-completions",
            "external_model": {
                "name": "gpt-3.5-turbo-instruct",
                "provider": "openai",
                "task": "llm/v1/completions",
                "openai_config": {
                    "openai_api_key": "{{secrets/my_openai_secret_scope/openai_api_key}}"
                }
            }
        }]
    }
)

次のコード スニペットは、OpenAI API キーをプレーンテキスト文字列として指定し、上記と同じ入力候補エンドポイントを作成する別の方法を示しています。

import mlflow.deployments

client = mlflow.deployments.get_deploy_client("databricks")
client.create_endpoint(
    name="openai-completions-endpoint",
    config={
        "served_entities": [{
            "name": "openai-completions",
            "external_model": {
                "name": "gpt-3.5-turbo-instruct",
                "provider": "openai",
                "task": "llm/v1/completions",
                "openai_config": {
                    "openai_api_key_plaintext": "sk-yourApiKey"
                }
            }
        }]
    }
)

Azure OpenAI を使用している場合は、構成の openai_config セクションで Azure OpenAI デプロイ名、エンドポイント URL、API バージョンを指定することもできます。

client.create_endpoint(
    name="openai-completions-endpoint",
    config={
        "served_entities": [
          {
            "name": "openai-completions",
            "external_model": {
                "name": "gpt-3.5-turbo-instruct",
                "provider": "openai",
                "task": "llm/v1/completions",
                "openai_config": {
                    "openai_api_type": "azure",
                    "openai_api_key": "{{secrets/my_openai_secret_scope/openai_api_key}}",
                    "openai_api_base": "https://my-azure-openai-endpoint.openai.azure.com",
                    "openai_deployment_name": "my-gpt-35-turbo-deployment",
                    "openai_api_version": "2023-05-15"
                },
            },
          }
        ],
    },
)

エンドポイントを更新するには update_endpoint() を使用します。 次のコード スニペットは、エンドポイントのレート制限をユーザーごとに 1 分あたり 20 回の呼び出しに更新する方法を示しています。

client.update_endpoint(
    endpoint="openai-completions-endpoint",
    config={
        "rate_limits": [
            {
                "key": "user",
                "renewal_period": "minute",
                "calls": 20
            }
        ],
    },
)

手順 3: 外部モデル エンドポイントに要求を送信する

重要

このセクションのコード例では、MLflow Deployments SDKの predict() メソッドの使用方法を示します。

MLflow Deployments SDK の predict() メソッドを使って、チャット、入力候補、埋め込みの要求を外部モデル エンドポイントに送信できます。

次の例では、OpenAI によってホストされている gpt-3.5-turbo-instruct に要求を送信します。

completions_response = client.predict(
    endpoint="openai-completions-endpoint",
    inputs={
        "prompt": "What is the capital of France?",
        "temperature": 0.1,
        "max_tokens": 10,
        "n": 2
    }
)
completions_response == {
    "id": "cmpl-8QW0hdtUesKmhB3a1Vel6X25j2MDJ",
    "object": "text_completion",
    "created": 1701330267,
    "model": "gpt-3.5-turbo-instruct",
    "choices": [
        {
            "text": "The capital of France is Paris.",
            "index": 0,
            "finish_reason": "stop",
            "logprobs": None
        },
        {
            "text": "Paris is the capital of France",
            "index": 1,
            "finish_reason": "stop",
            "logprobs": None
        },
    ],
    "usage": {
        "prompt_tokens": 7,
        "completion_tokens": 16,
        "total_tokens": 23
    }
}

手順 4: 別のプロバイダーのモデルを比較する

モデル サービングは、Open AI、Anthropic、Cohere、Amazon Bedrock、Google Cloud Vertex AI など、多くの外部モデル プロバイダーをサポートします。 プロバイダー間で LLM を比較すると、AI プレイグラウンドを使用して、アプリケーションの正確性、速度、コストを最適化するのに役立ちます。

次の例では、Anthropic claude-2 のエンドポイントを作成し、OpenAI gpt-3.5-turbo-instruct を使用した質問に対する回答を比較します。 どちらの回答も同じ標準形式であるため、簡単に比較できます。

Anthropic claude-2 のエンドポイントを作成する

import mlflow.deployments

client = mlflow.deployments.get_deploy_client("databricks")

client.create_endpoint(
    name="anthropic-completions-endpoint",
    config={
        "served_entities": [
            {
                "name": "claude-completions",
                "external_model": {
                    "name": "claude-2",
                    "provider": "anthropic",
                    "task": "llm/v1/completions",
                    "anthropic_config": {
                        "anthropic_api_key": "{{secrets/my_anthropic_secret_scope/anthropic_api_key}}"
                    },
                },
            }
        ],
    },
)

各エンドポイントからの回答を比較する


openai_response = client.predict(
    endpoint="openai-completions-endpoint",
    inputs={
        "prompt": "How is Pi calculated? Be very concise."
    }
)
anthropic_response = client.predict(
    endpoint="anthropic-completions-endpoint",
    inputs={
        "prompt": "How is Pi calculated? Be very concise."
    }
)
openai_response["choices"] == [
    {
        "text": "Pi is calculated by dividing the circumference of a circle by its diameter."
                " This constant ratio of 3.14159... is then used to represent the relationship"
                " between a circle's circumference and its diameter, regardless of the size of the"
                " circle.",
        "index": 0,
        "finish_reason": "stop",
        "logprobs": None
    }
]
anthropic_response["choices"] == [
    {
        "text": "Pi is calculated by approximating the ratio of a circle's circumference to"
                " its diameter. Common approximation methods include infinite series, infinite"
                " products, and computing the perimeters of polygons with more and more sides"
                " inscribed in or around a circle.",
        "index": 0,
        "finish_reason": "stop",
        "logprobs": None
    }
]

その他のリソース

Mosaic AI Model Serving の外部モデル