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

チュートリアル: OpenAIモデルをクエリするための外部モデルエンドポイントを作成する

このページでは、補完、チャット、および埋め込み用の OpenAI モデルを提供する外部モデルエンドポイントを構成およびクエリするための詳細な手順を説明します。MLflow Deployments SDKを使用してエンドポイントを作成し、OpenAIクライアントでクエリを実行します。情報については、外部モデルを参照してください。

エンドポイントを作成した後、Databricks はそのエンドポイントで Unity AI Gateway を構成し、使用状況の追跡、ペイロードのログ記録、ガードレール、およびレート制限などのガバナンス機能を追加することを推奨します。Model Serving を通じて提供されるすべての外部モデルは OpenAI 互換の API を使用してクエリされるため、プロバイダー間で単一のクライアントを使用できます。Unity AI GatewayでのAIガバナンスを参照してください。

プロンプト

Genie Code (エージェントモード) にこれを行うよう指示してください。

Create a new notebook that uses the MLflow Deployments SDK to create an external model endpoint named openai-completions-endpoint that serves OpenAI's gpt-3.5-turbo-instruct model for completions, reading my OpenAI API key from a Databricks secret scope. Then send a test completion request to the endpoint and display the response.

このタスクをサービングUIを使用して実行したい場合は、外部モデルサービングエンドポイントを作成するを参照してください。

要件

  • Databricks Runtime 13.0 ML以降 。
  • MLflow 2.9以上。
  • OpenAI APIキー。
  • Databricks CLIバージョン0.205以降をインストールしてください。

(オプション)ステップ0: Databricks Secrets CLIを使用してOpenAI APIキーを保存する

API キーは、ステップ 3 でプレーンテキスト文字列として、または Databricks Secrets を使用して提供できます。

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

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

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

ステップ1: 外部モデルをサポートするMLflowをインストールします。

外部モデルをサポートする MLflow バージョンをインストールするには、以下を使用してください:

sh
%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を入力してください。

Python
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キーをプレーンテキスト文字列として提供する方法を示しています。

Python
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 バージョンも指定できます。

Python
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"
},
},
}
],
},
)

エンドポイントでレート制限、使用状況の追跡、ペイロードロギング、またはガードレールを設定するには、Unity AI Gateway を使用します。Unity AI Gateway を介したレート制限の構成は、クエリベース (QPM) とトークンベース (TPM) の両方の制限をサポートし、ユーザーごと、グループごと、およびエンドポイント全体の制限を設定できます。

レート制限やその他のUnity AI Gateway機能を追加するためにエンドポイントを更新するプログラム例については、モデルサービングエンドポイントでのAI Gatewayの構成を参照してください。

注記

以前に文書化された最上位のrate_limitsフィールドを持つclient.update_endpoint()パターンは非推奨です。代わりにエンドポイントでUnity AI Gateway構成を使用してください。

ステップ 3:外部モデルエンドポイントにリクエストを送信する

Databricksは、OpenAIクライアントを使用して外部モデルエンドポイントをクエリーすることを推奨しています。Model Servingは、プロバイダー全体で統合されたOpenAI互換のAPIを公開しているため、基盤となるモデルがOpenAI、Anthropic、Cohere、Amazon Bedrock、Google クラウド Vertex AI、またはカスタムプロバイダーのいずれであっても、同じクライアントコードが機能します。

OpenAI クライアントをコンピュートにインストールしてください。

sh
%pip install openai

以下では、OpenAIチャットモデルを提供するエンドポイントにチャット完了リクエストを送信します。base_urlの値をDatabricksワークスペースURLに置き換え、api_keyDatabricks個人用アクセストークンを提供します。modelパラメーターをモデルサービングエンドポイントの名前に設定します。

Python
import os
from openai import OpenAI

client = OpenAI(
api_key=os.environ.get("DATABRICKS_TOKEN"),
base_url="https://<workspace-name>.cloud.databricks.com/serving-endpoints"
)

response = client.chat.completions.create(
model="openai-chat-endpoint",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
max_tokens=128,
temperature=0.1,
)
print(response.choices[0].message.content)

llm/v1/completionsタスク用に構成されたエンドポイントに補完リクエストを送信するには、client.completions.create()を使用してください。

Python
response = client.completions.create(
model="openai-completions-endpoint",
prompt="What is the capital of France?",
max_tokens=10,
temperature=0.1,
n=2,
)
print(response)

llm/v1/embeddingsタスク用に構成されたエンドポイントにエンベディングリクエストを送信するには、client.embeddings.create()を使用します。

Python
response = client.embeddings.create(
model="openai-embeddings-endpoint",
input="Databricks is a unified analytics platform.",
)
print(response.data[0].embedding)

Databricks ノートブック内からOpenAI クライアントを実行する場合、認証とワークスペースのベースURLを自動的に構成するdatabricks-openaiヘルパーを使用できます。詳細については、基盤モデルの使用を参照してください。

ステップ 4: 別のプロバイダーのモデルを比較する

モデルサービングは、Open AI 、Anthropic、Cohere、 Amazon Bedrock、Google Cloud Vertex AIなどを含む多くの外部モデル プロバイダーをサポートしています。

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

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

Python
import mlflow.deployments

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

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

各エンドポイントからのレスポンスを比較します

すべての外部モデルエンドポイントは OpenAI 互換の API を公開しているため、model パラメーターを対応するエンドポイント名に切り替えることで、同じ OpenAI クライアントで両方のエンドポイントをクエリできます。

Python
import os
from openai import OpenAI

client = OpenAI(
api_key=os.environ.get("DATABRICKS_TOKEN"),
base_url="https://<workspace-name>.cloud.databricks.com/serving-endpoints"
)

prompt = "How is Pi calculated? Be very concise."

openai_response = client.completions.create(
model="openai-completions-endpoint",
prompt=prompt,
)
anthropic_response = client.completions.create(
model="anthropic-completions-endpoint",
prompt=prompt,
)

print("OpenAI:", openai_response.choices[0].text)
print("Anthropic:", anthropic_response.choices[0].text)

その他のリソース