Download the PHP package digitaldreams/local-batch-api without Composer
On this page you can find all versions of the php package digitaldreams/local-batch-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download digitaldreams/local-batch-api
More information about digitaldreams/local-batch-api
Files in digitaldreams/local-batch-api
Package local-batch-api
Short Description Drop-in replacement for Anthropic and OpenAI batch APIs, self-hosted against Ollama or LM Studio.
License MIT
Informations about the package local-batch-api
local-batch-api
Drop-in replacement for the Anthropic and OpenAI batch APIs — self-hosted, running against local Ollama or LM Studio. No API keys, no cloud costs, no rate limits.
Requirements
Installation
Step 1 — Install the package
Step 2 — Run migrations
This creates two tables: batches and batch_files.
Step 3 — Configure your inference backend
Add to your .env:
Step 4 — Start a queue worker
Batch jobs run asynchronously. The worker must be running:
Two Ways to Use This Package
This package supports two independent usage patterns:
| Event-based | REST API | |
|---|---|---|
| Who calls it | Your own Laravel code | Any HTTP client (SDK, curl, external app) |
| Auth | Laravel's existing auth | Sanctum token (or your middleware) |
| Routes needed | No | Yes |
| Best for | Internal pipelines, jobs, commands | Replacing Anthropic/OpenAI SDK endpoints |
Approach 1 — Event-based (Internal Usage)
Use this when your own Laravel application needs to submit and process batches. No HTTP routes required.
Submitting an Anthropic-format batch
Fire a SubmitAnthropicBatchEvent event. The package listener picks it up and dispatches the processing job automatically.
Submitting an OpenAI-format batch
The OpenAI flow requires a file ID. Upload first using the BatchService, then fire the event.
Listening for results
Listen to BatchCompletedEvent to act on results when processing finishes:
Register it in AppServiceProvider::boot():
All available events
| Event | Properties | Fired when |
|---|---|---|
BatchCreatedEvent |
$batch, $items, $provider |
Batch record saved, job dispatched |
BatchProcessingEvent |
$batch |
Queue worker picks up the job |
BatchItemStartedEvent |
$batch, $dto |
Single request about to fire |
BatchItemCompletedEvent |
$batch, $result |
Single request finished |
BatchCompletedEvent |
$batch, $results |
All requests done |
BatchFailedEvent |
$batch, $exception |
Job threw an unrecoverable error |
BatchCancelledEvent |
$batch |
Batch cancelled |
Approach 2 — REST API (External HTTP Clients)
Use this when you want to point an existing Anthropic or OpenAI SDK at your local server instead of the cloud. The API surface is identical to the real APIs.
Step 1 — Register routes with authentication
Do not set BATCH_API_EXPOSE_ROUTES=true. Instead, register routes manually inside a protected middleware group so you control authentication.
Install Sanctum if you haven't already:
In your routes/api.php (or a service provider), wrap BatchApi::routes() with Sanctum middleware:
This registers all 11 endpoints, each requiring a valid Sanctum token.
Note:
BatchApi::routes()also applies theapimiddleware internally. Wrapping it withauth:sanctumstacks both, so your routes haveapi+auth:sanctum.
Step 2 — Issue a token
Step 3 — Call the API
All requests need the token in the Authorization header:
Anthropic Batch API — Step by Step
1. Submit a batch
Response 202 Accepted:
2. Poll until done
Keep polling until processing_status is "ended".
3. Fetch results (NDJSON)
Returns 204 No Content if still processing. When ready, streams one JSON object per line:
Other Anthropic endpoints
OpenAI Batch API — Step by Step
1. Upload a JSONL file
Create a .jsonl file (one request per line):
Upload it:
Response 201 Created:
2. Submit the batch
Response 201 Created:
3. Poll until completed
Poll until status is "completed". Note the output_file_id in the response.
4. Download results
Returns JSONL, one result per line:
Other OpenAI endpoints
Pointing an existing SDK at this server
Python (Anthropic SDK):
Python (OpenAI SDK):
Batch Status Lifecycle
| Internal | Anthropic processing_status |
OpenAI status |
|---|---|---|
pending |
in_progress |
validating |
processing |
in_progress |
in_progress |
completed |
ended |
completed |
failed |
ended |
failed |
cancelling |
canceling |
cancelling |
cancelled |
ended |
cancelled |
Batches expire after 24 hours.
Switching to LM Studio
- Open LM Studio → start the local server (default port
1234) - Load a model
- Update
.env:
No other changes needed.
Concurrency Tuning
INFERENCE_CONCURRENCY controls parallel requests per batch chunk.
| Hardware | Value |
|---|---|
| CPU-only | 1 |
| GPU with spare VRAM | 3–5 |
Postman Collection
Import Local-Batch-API.postman_collection.json. Set the baseUrl variable to your server URL. The collection auto-saves batch IDs and file IDs between requests so you can run folders top-to-bottom without manually copying values.
Troubleshooting
Batches stay pending forever — Queue worker not running. Run php artisan queue:work.
Ollama timeout in results — Model is slow or INFERENCE_TIMEOUT too low. Raise to 300.
Routes return 404 — Routes not registered. Either set BATCH_API_EXPOSE_ROUTES=true (no auth) or call BatchApi::routes() manually in a middleware group.
401 Unauthorized on API routes — Sanctum token missing or invalid. Pass Authorization: Bearer <token> header.
cannot chdir git error in submodule — Run git submodule update --init in the parent repo.
License
MIT