Batchvorhersagen mit Anthropic Claude-Modellen

Mit Batchvorhersagen können Sie mehrere Prompts, die nicht latenzempfindlich sind, an ein Anthropic Claude-Modell senden. Im Gegensatz zu Onlinevorhersagen, bei denen Sie für jede Anfrage einen Eingabeprompt senden, können Sie eine große Anzahl von Eingabeprompts in einer einzigen Anfrage zusammenfassen.

Unterstützte Anthropic Claude-Modelle

Die Gemini Enterprise Agent Platform unterstützt Batch-Vorhersagen für die folgenden Anthropic Claude-Modelle:

Kontingente

Standardmäßig können Sie in einem einzelnen Projekt vier gleichzeitige Batchanfragen stellen.

Eingabe vorbereiten

Bereiten Sie zuerst Ihr Eingabe-Dataset in einer BigQuery-Tabelle oder als JSONL-Datei in Cloud Storage vor. Die Eingabe für beide Quellen muss dem JSON-Format des Anthropic Claude API-Schemas entsprechen, wie im folgenden Beispiel gezeigt:

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

BigQuery

Ihre BigQuery-Eingabetabelle muss dem folgenden Schema entsprechen:

Spaltenname Beschreibung
custom_id Eine ID für jede Anfrage, um die Eingabe mit der Ausgabe abzugleichen.
Anfrage Der Anfragetext, der Ihren Eingabeprompt enthält und dem Anthropic Claude API-Schema entsprechen muss
  • Ihre Eingabetabelle kann weitere Spalten enthalten, die vom Batchjob ignoriert werden.
  • Bei Batchvorhersagejobs werden zwei Spaltennamen für die Batchvorhersageausgabe reserviert: response(JSON) und status. Verwenden Sie diese Spalten nicht in der Eingabetabelle.

Cloud Storage

Für Cloud Storage muss die Eingabedatei eine JSONL-Datei sein, die sich in einem Cloud Storage-Bucket befindet.

Batchvorhersage anfordern

Erstellen Sie eine Batchvorhersage für ein Claude-Modell, indem Sie Eingaben aus BigQuery oder Cloud Storage verwenden. Sie können Vorhersagen entweder in eine BigQuery-Tabelle oder in eine JSONL-Datei in einem Cloud Storage-Bucket ausgeben.

BigQuery

Geben Sie die BigQuery-Eingabetabelle, das Modell und den Ausgabespeicherort an. Der Batchvorhersagejob und Ihre Tabelle müssen sich in derselben Region befinden.

Python

Informationen zur Installation des Vertex AI SDK for Python finden Sie unter Vertex AI SDK for Python installieren. Weitere Informationen finden Sie in der API-Referenzdokumentation zu 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.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

Ersetzen Sie diese Werte in den folgenden Anfragedaten:

  • LOCATION: Eine Region, die das ausgewählte Anthropic Claude-Modell unterstützt (siehe Claude-Regionen).
  • PROJECT_ID: Ihre Projekt-ID. .
  • MODEL: Der Name des Modells.
  • INPUT_URI: Die BigQuery-Tabelle, in der sich die Eingabe für die Batchvorhersage befindet, z. B. bq://myproject.mydataset.input_table.
  • OUTPUT_FORMAT: Geben Sie bigquery an, um die Ausgabe in eine BigQuery-Tabelle zu schreiben. Geben Sie jsonl an, um die Ausgabe in einen Cloud Storage-Bucket zu schreiben.
  • DESTINATION: Geben Sie für BigQuery bigqueryDestination an. Geben Sie für Cloud Storage gcsDestination an.
  • OUTPUT_URI_FIELD_NAME: Geben Sie für BigQuery outputUri an. Geben Sie für Cloud Storage outputUriPrefix an.
  • OUTPUT_URI: Geben Sie für BigQuery den Tabellenspeicherort an, z. B. bq://myproject.mydataset.output_result. Geben Sie für Cloud Storage den Bucket- und Ordnerspeicherort an, z. B. gs://mybucket/path/to/outputfile.

HTTP-Methode und URL:

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

JSON-Text der Anfrage:

'{
  "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"
    }
  }
}'

Wenn Sie die Anfrage senden möchten, wählen Sie eine der folgenden Optionen aus:

curl

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

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

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

$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

Sie sollten eine JSON-Antwort ähnlich wie diese erhalten:

Cloud Storage

Geben Sie den Cloud Storage-Speicherort, das Modell und den Ausgabespeicherort Ihrer JSONL-Datei an.

Python

Informationen zur Installation des Vertex AI SDK for Python finden Sie unter Vertex AI SDK for Python installieren. Weitere Informationen finden Sie in der API-Referenzdokumentation zu 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

Ersetzen Sie diese Werte in den folgenden Anfragedaten:

  • LOCATION: Eine Region, die das ausgewählte Anthropic Claude-Modell unterstützt (siehe Claude-Regionen).
  • PROJECT_ID: .
  • MODEL: Der Name des Modells.
  • INPUT_URIS: Eine durch Kommas getrennte Liste der Cloud Storage-Speicherorte Ihrer JSONL-Batchvorhersageeingabe, z. B. gs://bucketname/path/to/jsonl.
  • OUTPUT_FORMAT: Geben Sie bigquery an, um die Ausgabe in eine BigQuery-Tabelle zu schreiben. Geben Sie jsonl an, um die Ausgabe in einen Cloud Storage-Bucket zu schreiben.
  • DESTINATION: Geben Sie für BigQuery bigqueryDestination an. Geben Sie für Cloud Storage gcsDestination an.
  • OUTPUT_URI_FIELD_NAME: Geben Sie für BigQuery outputUri an. Geben Sie für Cloud Storage outputUriPrefix an.
  • OUTPUT_URI: Geben Sie für BigQuery den Tabellenspeicherort an, z. B. bq://myproject.mydataset.output_result. Geben Sie für Cloud Storage den Bucket- und Ordnerspeicherort an, z. B. gs://mybucket/path/to/outputfile.

HTTP-Methode und URL:

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

JSON-Text der Anfrage:

'{
  "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"
    }
  }
}'

Wenn Sie die Anfrage senden möchten, wählen Sie eine der folgenden Optionen aus:

curl

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

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

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

$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

Sie sollten eine JSON-Antwort ähnlich wie diese erhalten:

Status eines Batchvorhersagejobs abrufen

Rufen Sie den Status Ihres Batchvorhersagejobs ab, um zu prüfen, ob er erfolgreich abgeschlossen wurde.

REST

Ersetzen Sie diese Werte in den folgenden Anfragedaten:

  • PROJECT_ID: .
  • LOCATION: Die Region, in der sich Ihr Batchjob befindet.
  • JOB_ID: Die Batchjob-ID, die beim Erstellen des Jobs zurückgegeben wurde.

HTTP-Methode und URL:

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

Senden Sie die Anfrage mithilfe einer der folgenden Optionen:

curl

Führen Sie folgenden Befehl aus:

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

Führen Sie diesen Befehl aus:

$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

Sie sollten eine JSON-Antwort ähnlich wie diese erhalten:

Batchvorhersageausgabe abrufen

Wenn ein Batchvorhersagejob abgeschlossen ist, rufen Sie die Ausgabe von dem von Ihnen angegebenen Speicherort ab. In BigQuery befindet sich die Ausgabe in der Spalte response(JSON) der BigQuery-Zieltabelle. Bei Cloud Storage wird die Ausgabe als JSONL-Datei am Cloud Storage-Ausgabespeicherort gespeichert.

Sie können auf die vollständigen Ergebnisse der Batch-Vorhersage zugreifen, nachdem alle Zeilen abgeschlossen sind oder nach 24 Stunden, je nachdem, was zuerst eintritt.