prism-php/bedrock
Composer 安装命令:
composer require prism-php/bedrock
包简介
A provider for Prism adding support for AWS Bedrock.
README 文档
README
Prism Bedrock
Unlock the power of AWS Bedrock services in your Laravel applications with Prism Bedrock. This package provides a standalone Bedrock provider for the Prism PHP framework.
Installation
composer require prism-php/bedrock
Configuration
Add the following to your Prism configuration (config/prism.php):
'bedrock' => [ // Key should match Bedrock::KEY 'region' => env('AWS_REGION', 'us-east-1'), // Set to true to ignore other auth configuration and use the AWS SDK default credential chain // read more at https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_default_chain.html 'use_default_credential_provider' => env('AWS_USE_DEFAULT_CREDENTIAL_PROVIDER', false), 'api_key' => env('AWS_ACCESS_KEY_ID'), // Ignored with `use_default_credential_provider` === true 'api_secret' => env('AWS_SECRET_ACCESS_KEY'), // Ignored with `use_default_credential_provider` === true 'session_token' => env('AWS_SESSION_TOKEN'), // Only required for temporary credentials. Ignored with `use_default_credential_provider` === true ],
Usage
Text Generation
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; $response = Prism::text() ->using(Bedrock::KEY, 'anthropic.claude-3-sonnet-20240229-v1:0') ->withPrompt('Explain quantum computing in simple terms') ->asText(); echo $response->text;
Structured Output (JSON)
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; use Prism\Prism\Schema\ObjectSchema; use Prism\Prism\Schema\StringSchema; use Prism\Prism\Schema\ArraySchema; $schema = new ObjectSchema( name: 'languages', description: 'Top programming languages', properties: [ new ArraySchema( 'languages', 'List of programming languages', items: new ObjectSchema( name: 'language', description: 'Programming language details', properties: [ new StringSchema('name', 'The language name'), new StringSchema('popularity', 'Popularity description'), ] ) ) ] ); $response = Prism::structured() ->using(Bedrock::KEY, 'anthropic.claude-3-sonnet-20240229-v1:0') ->withSchema($schema) ->withPrompt('List the top 3 programming languages') ->asStructured(); // Access your structured data $data = $response->structured;
Embeddings (with Cohere models)
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; $response = Prism::embeddings() ->using(Bedrock::KEY, 'cohere.embed-english-v3') ->fromArray(['The sky is blue', 'Water is wet']) ->asEmbeddings(); // Access the embeddings $embeddings = $response->embeddings;
Model names
Important
When using inference profiles via an ARN, you should urlencode the model name.
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; $response = Prism::text() ->using(Bedrock::KEY, urlencode('arn:aws:bedrock:us-east-1:999999999999:inference-profile/us.anthropic.claude-3-7-sonnet-20250219-v1:0')) ->withPrompt('Explain quantum computing in simple terms') ->asText();
Supported API Schemas
AWS Bedrock supports several API schemas - some LLM vendor specific, some generic.
Prism Bedrock supports three of those API schemas:
- Converse: AWS's native interface for chat (default)*
- Anthropic: For Claude models**
- Cohere: For Cohere embeddings models
Each schema supports different capabilities:
| Schema | Text | Structured | Embeddings |
|---|---|---|---|
| Converse | ✅ | ✅ | ❌ |
| Anthropic | ✅ | ✅ | ❌ |
| Cohere | ❌ | ❌ | ✅ |
* A unified interface for multiple providers. See AWS documentation for a list of supported models.
** The Converse schema does not support Anthropic's native features (e.g. PDF vision analysis). This schema uses Anthropic's native schema and therefore allows use of Anthropic native features. Please note however that Bedrock's Anthropic schema does not yet have feature parity with Anthropic. Notably it does not support documents or citations, and only supports prompt caching for Claude Sonnet 3.7 and Claude Haiku 3.5. To use documents with Anthropic's models, use them via the Converse schema (though note that this not the same as using Anthropic's PDF vision directly).
Auto-resolution of API schemas
Prism Bedrock resolves the most appropriate API schema from the model string. If it is unable to resolve a specific schema (e.g. anthropic for Anthropic), it will default to Converse.
Therefore if you use a model that is not supported by AWS Bedrock Converse, and does not have a specific Prism Bedrock implementation, your request will fail.
If you wish to force Prism Bedrock to use Converse instead of a vendor specific interface, you can do so with withProviderOptions():
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; use Prism\Bedrock\Enums\BedrockSchema; $response = Prism::text() ->using(Bedrock::KEY, 'anthropic.claude-3-sonnet-20240229-v1:0') ->withProviderOptions(['apiSchema' => BedrockSchema::Converse]) ->withPrompt('Explain quantum computing in simple terms') ->asText();
Cache-Optimized Prompts
For supported models, you can enable prompt caching to reduce latency and costs:
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; use Prism\Prism\ValueObjects\Messages\UserMessage; $response = Prism::text() ->using(Bedrock::KEY, 'anthropic.claude-3-sonnet-20240229-v1:0') ->withMessages([ (new UserMessage('Message with cache breakpoint'))->withProviderOptions(['cacheType' => 'ephemeral']), (new UserMessage('Message with another cache breakpoint'))->withProviderOptions(['cacheType' => 'ephemeral']), new UserMessage('Compare the last two messages.') ]) ->asText();
Tip
Anthropic currently supports a cacheType of "ephemeral". Converse currently supports a cacheType of "default". It is possible that Anthropic and/or AWS may add additional types in the future.
Structured Adapted Support
Both Anthropic and Converse Schemas do not support a native structured format.
Prism Bedrock has adapted support by appending a prompt asking the model to a response conforming to the schema you provide.
The performance of that prompt may vary by model. You can override it using withProviderOptions():
use Prism\Prism\Prism; use Prism\Bedrock\Bedrock; use Prism\Prism\ValueObjects\Messages\UserMessage; Prism::structured() ->withSchema($schema) ->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0') ->withProviderOptions([ // Override the default message of "Respond with ONLY JSON (i.e. not in backticks or a code block, with NO CONTENT outside the JSON) that matches the following schema:" 'jsonModeMessage' => 'My custom message', ]) ->withPrompt('My prompt') ->asStructured();
License
The MIT License (MIT). Please see License File for more information.
Authors
This library is created by TJ Miller with contributions from the Open Source Community.
prism-php/bedrock 适用场景与选型建议
prism-php/bedrock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 184.32k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2025 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 prism-php/bedrock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prism-php/bedrock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 184.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 34
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-20
