VertexAIChatCompletionBatch
langbatch.vertexai.VertexAIChatCompletionBatch
Bases: VertexAIBatch
, ChatCompletionBatch
VertexAIChatCompletionBatch is a class for Vertex AI chat completion batches. Can be used for batch processing with Gemini 1.5 Flash and Gemini 1.5 Pro models
Usage:
Source code in langbatch\vertexai.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
__init__
__init__(file: str, model: str, gcp_project: str, bigquery_input_dataset: str, bigquery_output_dataset: str) -> None
Initialize the VertexAIBatch class.
Parameters:
-
file
(str
) –The path to the jsonl file in Vertex AI batch format.
-
model
(str
) –The name of the model to use for the batch prediction.
-
gcp_project
(str
) –The GCP project to use for the batch prediction.
-
bigquery_input_dataset
(str
) –The BigQuery dataset to use for the batch prediction input.
-
bigquery_output_dataset
(str
) –The BigQuery dataset to use for the batch prediction output.
Usage:
batch = VertexAIBatch(
"path/to/file.jsonl",
"model",
"gcp_project",
"bigquery_input_dataset",
"bigquery_output_dataset"
)
Source code in langbatch\vertexai.py
create_from_requests
classmethod
Creates a batch when given a list of requests. These requests should be in correct Batch API request format as per the Batch type. Ex. for OpenAIChatCompletionBatch, requests should be a Chat Completion request with custom_id.
Parameters:
-
requests
–A list of requests.
-
batch_kwargs
(Dict
, default:{}
) –Additional keyword arguments for the batch class. Ex. gcp_project, etc. for VertexAIChatCompletionBatch.
Returns:
-
–
An instance of the Batch class.
Raises:
-
BatchInitializationError
–If the input data is invalid.
Usage:
batch = OpenAIChatCompletionBatch.create_from_requests([
{ "custom_id": "request-1",
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Biryani Receipe, pls."}],
"max_tokens": 1000
}
},
{
"custom_id": "request-2",
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Write a short story about AI"}],
"max_tokens": 1000
}
}
]
Source code in langbatch\Batch.py
load
classmethod
load(id: str, storage: BatchStorage = FileBatchStorage(), batch_kwargs: Dict = {})
Load a batch from the storage and return a Batch object.
Parameters:
-
id
(str
) –The id of the batch.
-
storage
(BatchStorage
, default:FileBatchStorage()
) –The storage to load the batch from. Defaults to FileBatchStorage().
-
batch_kwargs
(Dict
, default:{}
) –Additional keyword arguments for the batch class. Ex. gcp_project, etc. for VertexAIChatCompletionBatch.
Returns:
-
Batch
–The batch object.
Usage:
Source code in langbatch\Batch.py
save
save(storage: BatchStorage = FileBatchStorage())
Save the batch to the storage.
Parameters:
-
storage
(BatchStorage
, default:FileBatchStorage()
) –The storage to save the batch to. Defaults to FileBatchStorage().
Usage:
batch = OpenAIChatCompletionBatch(file)
batch.save()
# save the batch to file storage
batch.save(storage=FileBatchStorage("./data"))
Source code in langbatch\Batch.py
start
Source code in langbatch\vertexai.py
get_status
get_results_file
Usage:
import jsonlines
# create a batch and start batch process
batch = OpenAIChatCompletionBatch(file)
batch.start()
if batch.get_status() == "completed":
# get the results file
results_file = batch.get_results_file()
with jsonlines.open(results_file) as reader:
for obj in reader:
print(obj)
Source code in langbatch\Batch.py
get_results
Retrieve the results of the chat completion batch.
Returns:
-
Tuple[List[Dict[str, Any]], List[Dict[str, Any]]] | Tuple[None, None]
–A tuple containing successful and unsuccessful results. Successful results: A list of dictionaries with "choices" and "custom_id" keys. Unsuccessful results: A list of dictionaries with "error" and "custom_id" keys.
Usage:
successful_results, unsuccessful_results = batch.get_results()
for result in successful_results:
print(result["choices"])
Source code in langbatch\ChatCompletionBatch.py
is_retryable_failure
Source code in langbatch\vertexai.py
retry
Source code in langbatch\vertexai.py
get_unsuccessful_requests
Retrieve the unsuccessful requests from the batch.
Returns:
-
List[Dict[str, Any]]
–A list of requests that failed.
Usage:
batch = OpenAIChatCompletionBatch(file)
batch.start()
if batch.get_status() == "completed":
# get the unsuccessful requests
unsuccessful_requests = batch.get_unsuccessful_requests()
for request in unsuccessful_requests:
print(request["custom_id"])
Source code in langbatch\Batch.py
get_requests_by_custom_ids
Retrieve the requests from the batch file by custom ids.
Parameters:
-
custom_ids
(List[str]
) –A list of custom ids.
Returns:
-
List[Dict[str, Any]]
–A list of requests.
Usage:
batch = OpenAIChatCompletionBatch(file)
batch.start()
if batch.get_status() == "completed":
# get the requests by custom ids
requests = batch.get_requests_by_custom_ids(["custom_id1", "custom_id2"])
for request in requests:
print(request["custom_id"])
Source code in langbatch\Batch.py
create
classmethod
create(data: List[Iterable[ChatCompletionMessageParam]], request_kwargs: Dict = {}, batch_kwargs: Dict = {}) -> ChatCompletionBatch
Create a chat completion batch when given a list of messages.
Parameters:
-
data
(List[Iterable[ChatCompletionMessageParam]]
) –A list of messages to be sent to the API.
-
request_kwargs
(Dict
, default:{}
) –Additional keyword arguments for the API call. Ex. model, messages, etc.
-
batch_kwargs
(Dict
, default:{}
) –Additional keyword arguments for the batch class. Ex. gcp_project, etc. for VertexAIChatCompletionBatch.
Returns:
-
ChatCompletionBatch
–An instance of the ChatCompletionBatch class.
Raises:
-
BatchInitializationError
–If the input data is invalid.
Usage:
batch = OpenAIChatCompletionBatch.create([
[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of France?"}],
[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of Germany?"}]
],
request_kwargs={"model": "gpt-4o"})
# For Vertex AI
batch = VertexAIChatCompletionBatch.create([
[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of France?"}],
[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of Germany?"}]
],
request_kwargs={"model": "gemini-2.0-flash-001"},
batch_kwargs={
"gcp_project": "your-gcp-project",
"bigquery_input_dataset": "your-bigquery-input-dataset",
"bigquery_output_dataset": "your-bigquery-output-dataset"
})