Skip to content

Commit 4494f37

Browse files
DennisTraubDennis Traub
andauthored
PHP: Upgrade Anthropic Claude to v3 and use Messages API (#7191)
Bump Anthropic Claude to v3 and use Messages API instead of now outdated Text Completions API Co-authored-by: Dennis Traub <[email protected]>
1 parent a9eebaa commit 4494f37

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

php/example_code/bedrock-runtime/BedrockRuntimeService.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ public function invokeClaude($prompt)
3737

3838
$completion = "";
3939
try {
40-
$modelId = 'anthropic.claude-v2';
40+
$modelId = 'anthropic.claude-3-haiku-20240307-v1:0';
4141
// Claude requires you to enclose the prompt as follows:
42-
$prompt = "\n\nHuman: {$prompt}\n\nAssistant:";
4342
$body = [
44-
'prompt' => $prompt,
45-
'max_tokens_to_sample' => 200,
43+
'anthropic_version' => 'bedrock-2023-05-31',
44+
'max_tokens' => 512,
4645
'temperature' => 0.5,
47-
'stop_sequences' => ["\n\nHuman:"],
46+
'messages' => [[
47+
'role' => 'user',
48+
'content' => $prompt
49+
]]
4850
];
4951
$result = $this->bedrockRuntimeClient->invokeModel([
5052
'contentType' => 'application/json',
5153
'body' => json_encode($body),
5254
'modelId' => $modelId,
5355
]);
5456
$response_body = json_decode($result['body']);
55-
$completion = $response_body->completion;
57+
$completion = $response_body->content[0]->text;
5658
} catch (Exception $e) {
5759
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
5860
}

php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function runExample()
2323
$bedrockRuntimeService = new BedrockRuntimeService();
2424
$prompt = 'In one paragraph, who are you?';
2525
echo "\nPrompt: " . $prompt;
26-
echo "\n\nAnthropic Claude:";
26+
echo "\n\nAnthropic Claude:\n";
2727
echo $bedrockRuntimeService->invokeClaude($prompt);
28-
echo "\n\nAI21 Labs Jurassic-2: ";
28+
echo "\n\nAI21 Labs Jurassic-2:\n";
2929
echo $bedrockRuntimeService->invokeJurassic2($prompt);
3030
echo "\n---------------------------------------------------------------------\n";
3131
$image_prompt = 'stylized picture of a cute old steampunk robot';
@@ -35,12 +35,12 @@ public function runExample()
3535
$style_preset = 'photographic';
3636
$base64 = $bedrockRuntimeService->invokeStableDiffusion($image_prompt, $diffusionSeed, $style_preset);
3737
$image_path = $this->saveImage($base64, 'stability.stable-diffusion-xl');
38-
echo "The generated images have been saved to $image_path";
38+
echo "The generated image has been saved to $image_path";
3939
echo "\n\nAmazon Titan Image Generation:\n";
4040
$titanSeed = rand(0, 2147483647);
4141
$base64 = $bedrockRuntimeService->invokeTitanImage($image_prompt, $titanSeed);
4242
$image_path = $this->saveImage($base64, 'amazon.titan-image-generator-v1');
43-
echo "The generated images have been saved to $image_path";
43+
echo "The generated image has been saved to $image_path";
4444
}
4545

4646
private function saveImage($base64_image_data, $model_id): string

php/example_code/bedrock-runtime/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ functions within the same service.
4444

4545
### AI21 Labs Jurassic-2
4646

47-
- [InvokeModel](BedrockRuntimeService.php#L64)
47+
- [InvokeModel](BedrockRuntimeService.php#L66)
4848

4949
### Amazon Titan Image Generator
5050

51-
- [InvokeModel](BedrockRuntimeService.php#L131)
51+
- [InvokeModel](BedrockRuntimeService.php#L133)
5252

5353
### Anthropic Claude
5454

5555
- [InvokeModel](BedrockRuntimeService.php#L31)
5656

5757
### Stable Diffusion
5858

59-
- [InvokeModel](BedrockRuntimeService.php#L94)
59+
- [InvokeModel](BedrockRuntimeService.php#L96)
6060

6161

6262
<!--custom.examples.start-->
@@ -122,4 +122,4 @@ Run the tests with the following command:
122122

123123
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
124124

125-
SPDX-License-Identifier: Apache-2.0
125+
SPDX-License-Identifier: Apache-2.0

php/example_code/bedrock-runtime/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"aws/aws-sdk-php": "^3.324.10",
3+
"aws/aws-sdk-php": "^3.336",
44
"guzzlehttp/guzzle": "^7.9.2"
55
},
66
"autoload": {

php/example_code/bedrock-runtime/composer.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)