revolution/laravel-amazon-bedrock

Tiny Amazon Bedrock wrapper for Laravel

Fund package maintenance!
invokable

Installs: 2 540

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

pkg:composer/revolution/laravel-amazon-bedrock

0.2.6 2026-02-16 01:17 UTC

This package is auto-updated.

Last update: 2026-02-17 00:24:30 UTC


README

Maintainability Code Coverage Ask DeepWiki

Overview

A lightweight Laravel package to easily interact with Amazon Bedrock, specifically for generating text.

  • Features: Text Generation only.
  • Supported Model: Anthropic Claude Haiku/Sonnet/Opus 4 and later.(Default: Sonnet 4.5)
  • Authentication: Bedrock API Key only.
  • Cache Control: Always enabled ephemeral cache at system prompt.
  • Minimal Dependencies: No extra dependencies except Laravel framework.

We created our own package because prism-php/bedrock often doesn't support breaking changes in prism-php/prism. If you need more functionality than this package, please use Prism.

Requirements

  • PHP >= 8.4
  • Laravel >= 12.x

Installation

composer require revolution/laravel-amazon-bedrock

Configuration

Publishing the config file is optional. Everything can be set in .env.

AWS_BEDROCK_API_KEY=your_api_key
AWS_BEDROCK_MODEL=global.anthropic.claude-sonnet-4-5-20250929-v1:0
AWS_DEFAULT_REGION=us-east-1

Bedrock API key is obtained from the AWS Management Console.

Usage

Usage is almost the same, making it easy to return to Prism, but it doesn't have any other features.

use Revolution\Amazon\Bedrock\Facades\Bedrock;

$response = Bedrock::text()
                   ->using(Bedrock::KEY, config('bedrock.model'))
                   ->withSystemPrompt('You are a helpful assistant.')
                   ->withPrompt('Tell me a joke about programming.')
                   ->asText();

echo $response->text;

Conversation History

For multi-turn conversations, use withMessages() to pass previous messages.

use Revolution\Amazon\Bedrock\Facades\Bedrock;
use Revolution\Amazon\Bedrock\ValueObjects\Messages\UserMessage;
use Revolution\Amazon\Bedrock\ValueObjects\Messages\AssistantMessage;

$response = Bedrock::text()
                   ->withSystemPrompt('You are a helpful assistant.')
                   ->withMessages([
                       new UserMessage('What is JSON?'),
                       new AssistantMessage('JSON is a lightweight data format...'),
                   ])
                   ->withPrompt('Can you show me an example?')
                   ->asText();

echo $response->text;

Example with Eloquent conversation history

use App\Models\Message;
use Revolution\Amazon\Bedrock\Facades\Bedrock;
use Revolution\Amazon\Bedrock\ValueObjects\Messages\UserMessage;
use Revolution\Amazon\Bedrock\ValueObjects\Messages\AssistantMessage;

$messages = Message::query()
    ->where('conversation_id', $conversationId)
    ->orderBy('created_at')
    ->get()
    ->map(fn (Message $message) => match ($message->role) {
        'user' => UserMessage::make($message->content),
        'assistant' => AssistantMessage::make($message->content),
    })
    ->all();

$response = Bedrock::text()
                   ->withSystemPrompt('You are a helpful assistant.')
                   ->withMessages($messages)
                   ->withPrompt($newUserMessage)
                   ->asText();

Streaming

use Revolution\Amazon\Bedrock\Facades\Bedrock;

$stream = Bedrock::text()
                 ->using(Bedrock::KEY, config('bedrock.model'))
                 ->withSystemPrompt('You are a helpful assistant.')
                 ->withPrompt('Tell me a joke about programming.')
                 ->asStream();

foreach ($stream as $event) {
    if (data_get($event, 'type') === 'content_block_delta') {
        echo data_get($event, 'delta.text');
    }
}

Testing

use Revolution\Amazon\Bedrock\Facades\Bedrock;
use Revolution\Amazon\Bedrock\ValueObjects\Usage;
use Revolution\Amazon\Bedrock\Testing\TextResponseFake;

it('can generate text', function () {
    $fakeResponse = TextResponseFake::make()
        ->withText('Hello, I am Claude!')
        ->withUsage(new Usage(10, 20));

    // Set up the fake
    $fake = Bedrock::fake([$fakeResponse]);

    // Run your code
    $response = Bedrock::text()
        ->using(Bedrock::KEY, 'global.anthropic.claude-sonnet-4-5-20250929-v1:0')
        ->withPrompt('Who are you?')
        ->asText();

    // Make assertions
    expect($response->text)->toBe('Hello, I am Claude!');
});

Streaming Testing

   Bedrock::fake(streamResponses: [
       StreamResponseFake::make('Hello!'),
   ]);
   foreach (Bedrock::text()->withPrompt('Hi')->asStream() as $event) {
       //
   }

   // multiple chunks
   StreamResponseFake::make()->withChunks(['Hello', ' World']);

Laravel AI SDK Integration

  • Experimental implementation.
  • Support only text generation. No other features are supported.

This is an opt-in feature only enabled when the Laravel AI SDK is installed.

composer require laravel/ai
php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"

Add the following configuration to config/ai.php.

// config/ai.php
    'default' => 'bedrock-anthropic',

    'providers' => [
        'bedrock-anthropic' => [
            'driver' => 'bedrock-anthropic',
            'key' => '',
        ],
    ],

Usage with agent helper.

use function Laravel\Ai\agent;

$response = agent(
    instructions: 'You are an expert at software development.',
)->prompt('Tell me about Laravel');

echo $response->text;

Streaming

use Laravel\Ai\Streaming\Events\TextDelta;

use function Laravel\Ai\agent;

$stream = agent(
    instructions: 'You are an expert at software development.',
)->stream('Tell me about Laravel');

foreach ($stream as $event) {
    if ($event instanceof TextDelta) {
        echo $event->delta;
    }
}

License

MIT