バッチテキスト生成を取得する

バッチ予測では、レイテンシの影響を受けない大量のテキスト プロンプト リクエストを効率的に送信できます。オンライン予測の場合は一度に 1 つの入力リクエストしか行えませんが、バッチで予測の場合は 1 つのバッチ リクエストで多くの LLM リクエストを送信できます。Vertex AI の表形式データに対してバッチ予測を行う場合と同様に、出力先を決めて入力プロンプトを追加すると、その出力先に非同期でレスポンスが返されます。

テキストモデルのバッチ リクエストを送信して結果を確認したら、モデルのチューニングを行うことができます。チューニング後、更新されたモデルを通常どおりにバッチ生成用に送信できます。モデルのチューニングの詳細については、基盤モデルをチューニングするをご覧ください。

バッチ予測をサポートするテキストモデル

  • text-bison

入力を準備する

バッチ リクエストの入力では、バッチ生成用のモデルに送信するアイテムを指定します。モデルでテキスト分類を使用する場合は、JSON Lines ファイルまたは BigQuery テーブルを使用して入力のリストを指定できます。BigQuery テーブルは BigQuery に保存し、JSON Lines ファイルは Cloud Storage に保存します。

テキストモデルのバッチ リクエストは、BigQuery ストレージ ソースと Cloud Storage のみを受け入れます。リクエストには最大 30,000 件のプロンプトを含めることができます。

形式の詳細については、以下をご覧ください。

JSONL の例

JSONL の入力形式

{"prompt":"Give a short description of a machine learning model:"}
{"prompt":"Best recipe for banana bread:"}

JSONL の出力

{"instance":{"prompt":"Give..."},"predictions": [{"content":"A machine","safetyAttributes":{...}}],"status":""}
{"instance":{"prompt":"Best..."},"predictions": [{"content":"Sure", "safetyAttributes":{...}}],"status":""}

BigQuery の例

BigQuery の入力形式

この例は、単一列の BigQuery テーブルを示しています。

プロンプト
「ML モデルについて簡単に説明してください」
「バナナブレッドのベストレシピ」

BigQuery の出力

プロンプト 予測 ステータス
「ML モデルについて簡単に説明してください」

'[{
   "content": "A machine learning model is a
               statistical method",
   "safetyAttributes": {
     "blocked": false,
     "scores": [
       0.10000000149011612
     ],
     "categories": [
       "Violent"
     ]
   }
 }]'
 
「バナナブレッドのベストレシピ」

'[{"content": "Sure, here is a recipe for banana
               bread:\n\nIngredients:\n\n*",
   "safetyAttributes": {
     "scores": [
       0.10000000149011612
     ],
     "blocked": false,
     "categories": [
       "Violent"
     ]
   }
}]'

バッチ レスポンスをリクエストする

送信した入力アイテム数によっては、バッチ生成タスクが完了するまでに時間がかかることがあります。

REST

Vertex AI API を使用してテキスト プロンプトをテストするには、パブリッシャー モデル エンドポイントに POST リクエストを送信します。

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: Google Cloud Platform プロジェクトの名前です。
  • BP_JOB_NAME: ジョブ名。
  • MODEL_PARAM: モデル パラメータのマップ。使用可能なパラメータには、maxOutputTokens、topK、topP、temperature などがあります。
  • INPUT_URI: 入力ソース URI。これは、BigQuery テーブル URI または Cloud Storage の JSONL ファイルの URI です。
  • OUTPUT_URI: 出力ターゲット URI。

HTTP メソッドと URL:

POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs

リクエストの本文(JSON):

{
    "name": "BP_JOB_NAME",
    "displayName": "BP_JOB_NAME",
    "model": "publishers/google/models/text-bison",
    "model_parameters": "MODEL_PARAM"
    "inputConfig": {
      "instancesFormat":"bigquery",
      "bigquerySource":{
        "inputUri" : "INPUT_URI"
      }
    },
    "outputConfig": {
      "predictionsFormat":"bigquery",
      "bigqueryDestination":{
        "outputUri": "OUTPUT_URI"
        }
    }
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs"

PowerShell

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs" | Select-Object -Expand Content

次のような JSON レスポンスが返されます。

{
  "name": "projects/{PROJECT_ID}/locations/us-central1/batchPredictionJobs/{BATCH_JOB_ID}",
  "displayName": "BP_sample_publisher_BQ_20230712_134650",
  "model": "projects/{PROJECT_ID}/locations/us-central1/models/text-bison",
  "inputConfig": {
    "instancesFormat": "bigquery",
    "bigquerySource": {
      "inputUri": "bq://sample.text_input"
    }
  },
  "modelParameters": {},
  "outputConfig": {
    "predictionsFormat": "bigquery",
    "bigqueryDestination": {
      "outputUri": "bq://sample.llm_dataset.embedding_out_BP_sample_publisher_BQ_20230712_134650"
    }
  },
  "state": "JOB_STATE_PENDING",
  "createTime": "2023-07-12T20:46:52.148717Z",
  "updateTime": "2023-07-12T20:46:52.148717Z",
  "labels": {
    "owner": "sample_owner",
    "product": "llm"
  },
  "modelVersionId": "1",
  "modelMonitoringStatus": {}
}

レスポンスには、バッチジョブの固有識別子が含まれます。ジョブ stateJOB_STATE_SUCCEEDED になるまで、BATCH_JOB_ID を使用してバッチジョブのステータスをポーリングできます。次に例を示します。

curl \
  -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs/BATCH_JOB_ID

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。詳細については、Python API リファレンス ドキュメントをご覧ください。

from vertexai.preview.language_models import TextGenerationModel
text_model = TextGenerationModel.from_pretrained("text-bison")
batch_prediction_job = text_model.batch_predict(
  source_uri=["gs://BUCKET_NAME/test_table.jsonl"],
  destination_uri_prefix="gs://BUCKET_NAME/tmp/2023-05-25-vertex-LLM-Batch-Prediction/result3",
  # Optional:
  model_parameters={
      "maxOutputTokens": "200",
      "temperature": "0.2",
      "topP": "0.95",
      "topK": "40",
  },
)
print(batch_prediction_job.display_name)
print(batch_prediction_job.resource_name)
print(batch_prediction_job.state)

バッチ出力を取得する

バッチ予測タスクが完了すると、リクエストで指定した Cloud Storage バケットまたは BigQuery テーブルに出力が保存されます。

次のステップ