InvokeModel, InvokeModelWithBidirectionalStream). This aligns well with architectures requiring serverless AI inference, real-time streaming responses, or guardrail-enforced content moderation.ReactPHP) enables non-blocking I/O, critical for latency-sensitive applications.InvokeModelWithBidirectionalStream).use AsyncAws\BedrockRuntime\BedrockRuntimeClient;
use AsyncAws\BedrockRuntime\ValueObject\InvokeModelRequest;
$client = new BedrockRuntimeClient();
$request = new InvokeModelRequest('anthropic.claude-v2', '{"prompt": "..."}');
$response = $client->invokeModel($request);
InvokeModelWithBidirectionalStream API mandates HTTP/2, which may require:
react/http v2.x).mod_php).ReactPHP or promise-based concurrency may face ramp-up time.harmfulContentHandling) require understanding of Bedrock’s content moderation policies.AsyncAws\BedrockRuntime\Exception). Laravel’s exception handler must be extended to log/translate these gracefully.| Component | Compatibility | Notes |
|---|---|---|
| Laravel Version | 9.x+ (PHP 8.2+) | PHP 8.1 or lower: Blocked. Use aws/aws-sdk-php as fallback. |
| PHP Extensions | ext-curl, ext-json, ext-mbstring (required by AsyncAws) |
Standard in Laravel. |
| Web Server | HTTP/2 support required (e.g., Nginx, Caddy, or Laravel with Swoole/RoadRunner) | Apache may need mod_http2. |
| Queue System | Laravel Queues (Redis, Database, SQS) | Async inference can be offloaded to workers. |
| Async Runtime | Swoole, ReactPHP, or RoadRunner (for HTTP/2 streaming) | Required for InvokeModelWithBidirectionalStream. |
| Monitoring | Laravel Scout, Prometheus, or custom logging | Track Bedrock API calls, latency, and costs. |
fruitcake/laravel-aws) for simplicity.InvokeModel) in a Laravel queue worker.fruitcake/laravel-aws, this is a raw SDK.then(), await, or ReactPHP event loops.composer require async-aws/bedrock-runtime reactphp/http-client
.env or AsyncAws’s AwsClientBuilder:
use AsyncAws\Core\Credentials\Credentials;
use AsyncAws\Core\Credentials\EnvironmentCredentialsProvider;
$credentials = new Credentials(
env('AWS_ACCESS_KEY_ID'),
env('AWS_SECRET_ACCESS_KEY'),
env('AWS_DEFAULT_REGION', 'us-east-1')
);
app/Services/BedrockService.php).public function invokeModel(string $modelId, string $prompt): string {
$client = new BedrockRuntimeClient();
$request = new InvokeModelRequest($modelId, json_encode(['prompt' => $prompt]));
$response = $client->invokeModel($request)->wait();
return $response->getBody()->getContent();
}
InvokeModelWithBidirectionalStream:
$stream = $client->invokeModelWithBidirectionalStream($request);
$stream->on('data', function ($chunk) {
// Process chunk (e.g., emit to WebSocket)
});
harmfulContentHandling option.UNKNOWN_TO_SDK).Handler to log AsyncAws-specific exceptions.AsyncAws\Core\Retry\RetryStrategy).How can I help you explore Laravel packages today?