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

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

重要

外部モデルのEndpointはレガシーです

Creating external model endpoints on Model Serving is a legacy approach.For new workloads, use model provider サービス in Unity Gateway to connect to external providers such as OpenAI and govern credentials, access, usage, and cost with Unity Catalog.See Create and manage providers with Model Provider サービス.

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

After you create an Endpoint, you can configure AI Gateway (legacy) on it to add governance features like usage tracking, payload logging, guardrails, and rate limits. Model Serving を通じて提供されるすべての外部モデルは OpenAI 互換 API を使用してクエリーされるため、プロバイダー間で単一のクライアントを使用できます。モデルサービング Endpointのガバナンスの構成(レガシー)を参照してください。

プロンプト

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の補完エンドポイントを作成します。エンドポイントには、各フィールドの一意の値でnameとopenai_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"
},
},
}
],
},
)

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

See Configure governance on モデルサービング endpoints (legacy) for a programmatic example that updates an endpoint to add rate limits and other AI Gateway (legacy) features.

注記

The previously documented client.update_endpoint() pattern with a top-level rate_limits field is deprecated.Use the AI Gateway (legacy) configuration on the endpoint instead.

ステップ 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_keyにDatabricks個人用アクセストークンを提供します。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={
"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 互換の 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)

その他のリソース​