ChatCompletionBatch
langbatch.ChatCompletionBatch.ChatCompletionBatch
Bases: Batch
ChatCompletionBatch is a base class for chat completion batch classes. Utilizes OpenAI Chat Completion API format as the standard request format.
Source code in langbatch\ChatCompletionBatch.py
__init__
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"
})
Source code in langbatch\ChatCompletionBatch.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"])