スキップしてコンテンツを表示

PHP connecting to Bedrock model and having issues with InvokeModel

0

Hi there,

I am new to using Bedrock, and I am adding some AI connections to a PHP web application.

My user credentials are correct, and the IAM user has FULL permissions to Bedrock

I am wanting to invoke the Claude model which I have access to but getting issues with the code below.

Appreciate any help with this.

CODE: require 'vendor/autoload.php';

use Aws\S3\S3Client; use Aws\Bedrock\BedrockClient;

$s3 = new S3Client([ 'region' => 'ap-southeast-2', 'version' => 'latest', 'credentials' => [ 'key' => 'X', 'secret' => 'X', ], ]);

$bedrock = new BedrockClient([ 'region' => 'ap-southeast-2', 'version' => 'latest', 'credentials' => [ 'key' => 'X, 'secret' => 'X', ], ]);

$response = $bedrock->invokeModel([ 'modelId' => 'anthropic.claude-3-haiku-20240307-v1:0:48k', // Replace with the actual model ID 'body' => json_encode([ 'inputText' => 'Write me a Haiku' ]), ]);


The error I am getting is the following: Fatal error: Uncaught InvalidArgumentException: Operation not found: InvokeModel in /home/indusai/webapps/indusAI/vendor/aws/aws-sdk-php/src/AwsClient.php:328 Stack trace: #0 /home/indusai/webapps/indusAI/vendor/aws/aws-sdk-php/src/AwsClientTrait.php(86): Aws\AwsClient->getCommand() #1 /home/indusai/webapps/indusAI/aitest.php(66): Aws\AwsClient->__call() #2 {main} thrown in /home/indusai/webapps/indusAI/vendor/aws/aws-sdk-php/src/AwsClient.php on line 328

質問済み 2年前639ビュー

1回答
0

There are a few issues with your code that are causing the error you're encountering:

  1. You're using the wrong client for invoking the model. Instead of BedrockClient, you should be using BedrockRuntimeClient. The BedrockClient is for managing Bedrock resources, while BedrockRuntimeClient is for invoking models.

  2. 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.

  3. 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

回答済み 2年前

エキスパート

レビュー済み 2年前

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

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

関連するコンテンツ