[Platform] Add batch processing support#1802
[Platform] Add batch processing support#1802camilleislasse wants to merge 5 commits intosymfony:mainfrom
Conversation
|
friendly ping @tacman |
Introduces BatchPlatformInterface / BatchClientInterface mirroring the existing Platform / ModelClientInterface split for synchronous calls. - BatchPlatform: normalizes inputs via Contract, delegates HTTP to BatchClientInterface - BatchJob: immutable snapshot of a batch job status - BatchResult: result of a single request within a completed batch - BatchInput: single request within a batch submission - BatchStatus: canonical status enum (PROCESSING, COMPLETED, FAILED, CANCELLED, EXPIRED) - Capability::BATCH: new capability flag for models supporting batch processing
Implements BatchClientInterface for OpenAI using the Files API + Batch API: uploads a JSONL file, creates the batch job, polls status, streams results. Adds Capability::BATCH to all GPT models in the ModelCatalog and a PlatformFactory::createBatch() factory method.
Implements BatchClientInterface for Anthropic using the Message Batches API. Streams the request body as a JSON array to avoid loading all requests in memory. Adds Capability::BATCH to all Claude models in the ModelCatalog and a PlatformFactory::createBatch() factory method.
Registers BatchPlatform as a service for OpenAI and Anthropic providers. Automatically aliases BatchPlatformInterface when a single batch platform is configured.
Demonstrates submitting a batch of questions, polling until completion, and streaming results with token usage.
4661d1f to
5a8e019
Compare
|
Hi there, by the first look I don't really get why we need separate infrastructure here and cannot reuse the Can we do something like this instead? // regular platform setup via factory
$platform = PlatformFactory::create(env('OPENAI_API_KEY'));
$inputs = [
new BatchInput('req-1', new MessageBag(Message::ofUser('What is the capital of France?'))),
new BatchInput('req-2', new MessageBag(Message::ofUser('What is the capital of Germany?'))),
];
$result = $platform->invoke('gpt-4o-mini', $inputs, [
'max_tokens' => 50,
'batch' => true, // if needed?
]);
$job = $result->asBatchJob();
// Poll until complete
while (!$job->isTerminal()) {
sleep(5);
}
// Stream results
foreach ($job->getResults() as $result) {
if ($result->isSuccess()) {
echo $result->getId().': '.$result->getContent();
}
} |
Hi! thanks for taking the time to look at this.
The part we weren't sure about is the output: unlike all existing clients where results come from the same HTTP response as the request, batch results arrive from a completely separate endpoint, much later The HTTP response only contains job metadata (id, status, counts), a if you have something in mind for that part? |
|
@tacman review :
|
We have a similar issue with the However, I agree that there needs to be a second part of infrastructure after the initial platform call - but I would like to see the initial call as integrated in the Platform as possible |
Batch Processing
Summary
Adds asynchronous batch processing support to the Platform component, inspired by issue #1776.
The implementation mirrors the existing
Platform/ModelClientInterfacesplit:BatchPlatformInterfacepublic API for submitting and managing batch jobsBatchClientInterfaceHTTP-level contract implemented by each providerBatchPlatformnormalizes inputs viaContract, delegates toBatchClientInterfaceNew classes
BatchInput- a single request within a batch submissionBatchJob- immutable snapshot of a batch job statusBatchResult- result of a single request within a completed batchBatchStatus- canonical status enum (PROCESSING, COMPLETED, FAILED, CANCELLED, EXPIRED)Capability::BATCH- new capability flag for models supporting batch processingProvider support
OpenAI - uploads a JSONL file via the Files API, creates the batch job, streams results line by line.
Anthropic - streams the request body as a JSON array to avoid loading all requests in memory, uses the Message Batches API.
AiBundle integration
Registers
BatchPlatformas a service for OpenAI and Anthropic providers. Automatically aliasesBatchPlatformInterfacewhen a single batch platform is configured.Usage
Commits
[Platform]Add Batch core abstractions[Platform][OpenAI]Add Batch support[Platform][Anthropic]Add Batch support[AIBundle]Add Batch platform registration[Examples]Add OpenAI batch processing example