定制 revolution/laravel-amazon-bedrock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

revolution/laravel-amazon-bedrock

最新稳定版本:0.1.8

Composer 安装命令:

composer require revolution/laravel-amazon-bedrock

包简介

Tiny Amazon Bedrock wrapper for Laravel

README 文档

README

Maintainability Code Coverage

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.3
  • 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();

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!');
});

License

MIT

统计信息

  • 总下载量: 1.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-26