- Newest
- Most votes
- Most comments
There are a few issues with your code that are causing the error you're encountering:
-
You're using the wrong client for invoking the model. Instead of
BedrockClient, you should be usingBedrockRuntimeClient. TheBedrockClientis for managing Bedrock resources, whileBedrockRuntimeClientis for invoking models. -
The region you're using (ap-southeast-2) may not support the Claude 3 Haiku model. Make sure to check which regions support the model you're trying to use.
-
The format of your request body is incorrect for the Claude 3 model. Claude 3 uses the Messages API, which requires a different structure.
Here's a corrected version of your code:
require 'vendor/autoload.php'; use Aws\BedrockRuntime\BedrockRuntimeClient; $bedrockRuntime = new BedrockRuntimeClient([ 'region' => 'us-east-1', // or another region that supports Claude 3 'version' => 'latest', 'credentials' => [ 'key' => 'X', 'secret' => 'X', ], ]); $response = $bedrockRuntime->invokeModel([ 'modelId' => 'anthropic.claude-3-haiku-20240307-v1:0', 'contentType' => 'application/json', 'accept' => 'application/json', 'body' => json_encode([ 'anthropic_version' => 'bedrock-2023-05-31', 'max_tokens' => 1000, 'messages' => [ [ 'role' => 'user', 'content' => 'Write me a Haiku' ] ] ]), ]); $result = json_decode($response['body']->getContents(), true); echo $result['content'][0]['text'];
Make sure you have the latest version of the AWS SDK for PHP installed. Also, double-check that you have been granted access to the Claude 3 model in your AWS account and that you're using a supported region for that model.
If you continue to face issues, verify your IAM permissions include the necessary actions to invoke models in Amazon Bedrock, specifically the "bedrock:InvokeModel" and "bedrock:InvokeModelWithResponseStream" actions.
Sources
Resolve InvokeModel API error in Amazon Bedrock | AWS re:Post
Bedrock API invocation error - on demand throughput isn's supported | AWS re:Post
Accessing Amazon Bedrock Claude-3-Haiku and Claude-3-Sonnet models via Lambda Functions | AWS re:Post
Relevant content
asked 2 years ago
asked 2 years ago

Hi, yes, BedrockRuntimeClient i is the client to use for Invoke.