チュートリアル: OpenAI モデルをクエリーする外部モデルエンドポイントの作成
この記事では、 MLflow Deployments SDK を使用して、補完、チャット、エンべディングの OpenAI モデルを提供する外部モデル エンドポイントを構成およびクエリするための詳細な手順について説明します。 詳しくは、外部モデルについての記事をご覧ください。
このタスクを実行するために Serving UI を使用する場合は、 「外部モデルサービング エンドポイントの作成」を参照してください。
要件
Databricks Runtime 13.0 ML以降 。
MLflow 2.9 以降。
OpenAI API キー。
Databricks CLIバージョン 0.205 以上をインストールします。
ステップ 1: Databricks Secrets CLI を使用して OpenAI API キーを格納する
OpenAI API キーは、Databricks Secrets 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
ステップ 2: 外部モデルをサポートする MLflow をインストールする
外部モデルをサポートする MLflow バージョンをインストールするには、以下を使用します。
%pip install mlflow[genai]>=2.9.0
ステップ 3: 外部モデルエンドポイントを作成して管理する
重要
このセクションのコード例では、 パブリック プレビューの MLflow デプロイ CRUD SDK の使用方法を示します。
大規模言語モデル (LLM) の外部モデル エンドポイントを作成するには、MLflow デプロイメント SDK のcreate_endpoint()
メソッドを使用します。 また、外部モデルエンドポイントは、サービング UI で作成することもできます。
次のコード スニペットは、構成の served_entities
セクションで指定されているように、OpenAI gpt-3.5-turbo-instruct
の入力候補エンドポイントを作成します。エンドポイントについては、 name
と openai_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}}"
}
}
}]
}
)
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()
を使用します。 次のコード スニペットは、エンドポイントのレート制限をユーザーあたり 20 コール/分に更新する方法を示しています。
client.update_endpoint(
endpoint="openai-completions-endpoint",
config={
"rate_limits": [
{
"key": "user",
"renewal_period": "minute",
"calls": 20
}
],
},
)
ステップ 4: 外部モデルエンドポイントにリクエストを送信する
重要
このセクションのコード例は、MLflow Deployments SDK のpredict()
メソッドの使用法を示しています。
チャット、補完、エンべディングのリクエストは、MLflow デプロイ 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
}
}
ステップ 5: 別のプロバイダーのモデルを比較する
モデルサービングは、Open AI 、Anthropic、Cohere、 Amazon Bedrock、Google Cloud Vertex AIなどを含む多くの外部モデル プロバイダーをサポートしています。 AI Playgroundを使用して、プロバイダー間で LLM を比較し、アプリケーションの精度、速度、コストを最適化できます。
次の例では、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
}
]