Prediksi batch dengan model Claude Anthropic

Prediksi batch memungkinkan Anda mengirim beberapa perintah yang tidak sensitif terhadap latensi ke model Anthropic Claude. Dibandingkan dengan prediksi online, di mana Anda mengirim satu perintah input untuk setiap permintaan, Anda dapat mengelompokkan sejumlah besar perintah input dalam satu permintaan.

Model Anthropic Claude yang didukung

Vertex AI mendukung prediksi batch untuk model Anthropic Claude berikut:

Kuota

Secara default, jumlah permintaan batch serentak yang dapat Anda buat dalam satu project adalah 4.

Menyiapkan input

Sebelum memulai, siapkan set data input Anda dalam tabel BigQuery atau sebagai file JSONL di Cloud Storage. Input untuk kedua sumber harus mengikuti format JSON Skema Anthropic Claude API, seperti yang ditunjukkan dalam contoh berikut:

{
  "custom_id": "request-1",
  "request":  {
    "messages": [{"role": "user", "content": "Hello!"}],
    "anthropic_version": "vertex-2023-10-16",
    "max_tokens": 50
  }
}

BigQuery

Tabel input BigQuery Anda harus mematuhi skema berikut:

Nama kolom Deskripsi
custom_id ID untuk setiap permintaan agar input cocok dengan output.
permintaan Isi permintaan, yang merupakan perintah input Anda dan harus mengikuti Skema Anthropic Claude API
  • Tabel input Anda dapat memiliki kolom lain, yang diabaikan oleh tugas batch.
  • Tugas prediksi batch mencadangkan dua nama kolom untuk output prediksi batch: response(JSON) dan status. Jangan gunakan kolom ini dalam tabel input.

Cloud Storage

Untuk Cloud Storage, file input harus berupa file JSONL yang berada di bucket Cloud Storage.

Meminta prediksi batch

Buat prediksi batch terhadap model Claude menggunakan input dari BigQuery atau Cloud Storage. Anda dapat memilih secara terpisah untuk menghasilkan prediksi ke tabel BigQuery atau file JSONL di bucket Cloud Storage.

BigQuery

Tentukan tabel input BigQuery, model, dan lokasi output Anda. Tugas prediksi batch dan tabel Anda harus berada di region yang sama.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Python.

import time

from google import genai
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# TODO(developer): Update and un-comment below line
# output_uri = f"bq://your-project.your_dataset.your_table"

job = client.batches.create(
    # Check Anthropic Claude region availability in https://cloud.devsite.corp.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
    # More about Anthropic model: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-3-5-haiku
    model="publishers/anthropic/models/claude-3-5-haiku",
    # The source dataset needs to be created specifically in us-east5
    src="bq://python-docs-samples-tests.anthropic_bq_sample.test_data",
    config=CreateBatchJobConfig(dest=output_uri),
)
print(f"Job name: {job.name}")
print(f"Job state: {job.state}")
# Example response:
# Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
# Job state: JOB_STATE_PENDING

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
completed_states = {
    JobState.JOB_STATE_SUCCEEDED,
    JobState.JOB_STATE_FAILED,
    JobState.JOB_STATE_CANCELLED,
    JobState.JOB_STATE_PAUSED,
}

while job.state not in completed_states:
    time.sleep(30)
    job = client.batches.get(name=job.name)
    print(f"Job state: {job.state}")
# Example response:
# Job state: JOB_STATE_PENDING
# Job state: JOB_STATE_RUNNING
# Job state: JOB_STATE_RUNNING
# ...
# Job state: JOB_STATE_SUCCEEDED

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • LOCATION: Region yang mendukung model Anthropic Claude yang dipilih (lihat Region Claude).
  • PROJECT_ID: Project ID Anda.
  • MODEL: Nama model.
  • INPUT_URI: Tabel BigQuery tempat input prediksi batch Anda berada seperti bq://myproject.mydataset.input_table.
  • OUTPUT_FORMAT: Untuk menghasilkan output ke tabel BigQuery, tentukan bigquery. Untuk menghasilkan output ke bucket Cloud Storage, tentukan jsonl.
  • DESTINATION: Untuk BigQuery, tentukan bigqueryDestination. Untuk Cloud Storage, tentukan gcsDestination.
  • OUTPUT_URI_FIELD_NAME: Untuk BigQuery, tentukan outputUri. Untuk Cloud Storage, tentukan outputUriPrefix.
  • OUTPUT_URI: Untuk BigQuery, tentukan lokasi tabel seperti bq://myproject.mydataset.output_result. Untuk Cloud Storage, tentukan lokasi bucket dan folder seperti gs://mybucket/path/to/outputfile.

Metode HTTP dan URL:

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

Isi JSON permintaan:

'{
  "displayName": "JOB_NAME",
  "model": "publishers/anthropic/models/MODEL",
  "inputConfig": {
    "instancesFormat":"bigquery",
    "bigquerySource":{
      "inputUri" : "INPUT_URI"
    }
  },
  "outputConfig": {
    "predictionsFormat":"OUTPUT_FORMAT",
    "DESTINATION":{
      "OUTPUT_URI_FIELD_NAME": "OUTPUT_URI"
    }
  }
}'

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

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

PowerShell

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs" | Select-Object -Expand Content

Anda akan melihat respons JSON yang mirip seperti berikut:

Cloud Storage

Tentukan lokasi Cloud Storage, model, dan lokasi output file JSONL Anda.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Python.

import time

from google import genai
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))
# TODO(developer): Update and un-comment below line
# output_uri = "gs://your-bucket/your-prefix"

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
job = client.batches.create(
    # More about Anthropic model: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-3-5-haiku
    model="publishers/anthropic/models/claude-3-5-haiku",
    # Source link: https://storage.cloud.google.com/cloud-samples-data/batch/anthropic-test-data-gcs.jsonl
    src="gs://cloud-samples-data/anthropic-test-data-gcs.jsonl",
    config=CreateBatchJobConfig(dest=output_uri),
)
print(f"Job name: {job.name}")
print(f"Job state: {job.state}")
# Example response:
# Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
# Job state: JOB_STATE_PENDING

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
completed_states = {
    JobState.JOB_STATE_SUCCEEDED,
    JobState.JOB_STATE_FAILED,
    JobState.JOB_STATE_CANCELLED,
    JobState.JOB_STATE_PAUSED,
}

while job.state not in completed_states:
    time.sleep(30)
    job = client.batches.get(name=job.name)
    print(f"Job state: {job.state}")
# Example response:
# Job state: JOB_STATE_PENDING
# Job state: JOB_STATE_RUNNING
# Job state: JOB_STATE_RUNNING
# ...
# Job state: JOB_STATE_SUCCEEDED

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • LOCATION: Region yang mendukung model Anthropic Claude yang dipilih (lihat Region Claude).
  • PROJECT_ID: Project ID Anda.
  • MODEL: Nama model.
  • INPUT_URIS: Daftar lokasi Cloud Storage yang dipisahkan koma dari input prediksi batch JSONL Anda, seperti gs://bucketname/path/to/jsonl.
  • OUTPUT_FORMAT: Untuk menghasilkan output ke tabel BigQuery, tentukan bigquery. Untuk menghasilkan output ke bucket Cloud Storage, tentukan jsonl.
  • DESTINATION: Untuk BigQuery, tentukan bigqueryDestination. Untuk Cloud Storage, tentukan gcsDestination.
  • OUTPUT_URI_FIELD_NAME: Untuk BigQuery, tentukan outputUri. Untuk Cloud Storage, tentukan outputUriPrefix.
  • OUTPUT_URI: Untuk BigQuery, tentukan lokasi tabel seperti bq://myproject.mydataset.output_result. Untuk Cloud Storage, tentukan lokasi bucket dan folder seperti gs://mybucket/path/to/outputfile.

Metode HTTP dan URL:

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

Isi JSON permintaan:

'{
  "displayName": "JOB_NAME",
  "model": "publishers/anthropic/models/MODEL",
  "inputConfig": {
    "instancesFormat":"jsonl",
    "gcsSource":{
      "uris" : "INPUT_URIS"
    }
  },
  "outputConfig": {
    "predictionsFormat":"OUTPUT_FORMAT",
    "DESTINATION":{
      "OUTPUT_URI_FIELD_NAME": "OUTPUT_URI"
    }
  }
}'

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

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

PowerShell

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs" | Select-Object -Expand Content

Anda akan melihat respons JSON yang mirip seperti berikut:

Mendapatkan status tugas prediksi batch

Dapatkan status tugas prediksi batch Anda untuk memeriksa apakah tugas telah berhasil diselesaikan.

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region tempat tugas batch Anda berada.
  • JOB_ID: ID tugas batch yang ditampilkan saat Anda membuat tugas.

Metode HTTP dan URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID"

PowerShell

Jalankan perintah berikut:

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

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID" | Select-Object -Expand Content

Anda akan menerima respons JSON yang mirip dengan berikut ini.

Mengambil output prediksi batch

Setelah tugas prediksi batch selesai, ambil output dari lokasi yang Anda tentukan. Untuk BigQuery, outputnya ada di kolom response(JSON) pada tabel BigQuery tujuan Anda. Untuk Cloud Storage, output disimpan sebagai file JSONL di lokasi Cloud Storage output.

Anda dapat mengakses hasil prediksi batch lengkap setelah semua baris selesai atau setelah 24 jam, mana saja yang lebih dulu.