Skip to content

Commit

Permalink
Bump Anthropic Claude to v3 and use Messages API instead of now outda…
Browse files Browse the repository at this point in the history
…ted Text Completions API
  • Loading branch information
Dennis Traub authored and Laren-AWS committed Jan 13, 2025
1 parent a9eebaa commit 135f4b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
14 changes: 8 additions & 6 deletions php/example_code/bedrock-runtime/BedrockRuntimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ public function invokeClaude($prompt)

$completion = "";
try {
$modelId = 'anthropic.claude-v2';
$modelId = 'anthropic.claude-3-haiku-20240307-v1:0';
// Claude requires you to enclose the prompt as follows:
$prompt = "\n\nHuman: {$prompt}\n\nAssistant:";
$body = [
'prompt' => $prompt,
'max_tokens_to_sample' => 200,
'anthropic_version' => 'bedrock-2023-05-31',
'max_tokens' => 512,
'temperature' => 0.5,
'stop_sequences' => ["\n\nHuman:"],
'messages' => [[
'role' => 'user',
'content' => $prompt
]]
];
$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => json_encode($body),
'modelId' => $modelId,
]);
$response_body = json_decode($result['body']);
$completion = $response_body->completion;
$completion = $response_body->content[0]->text;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function runExample()
$bedrockRuntimeService = new BedrockRuntimeService();
$prompt = 'In one paragraph, who are you?';
echo "\nPrompt: " . $prompt;
echo "\n\nAnthropic Claude:";
echo "\n\nAnthropic Claude:\n";
echo $bedrockRuntimeService->invokeClaude($prompt);
echo "\n\nAI21 Labs Jurassic-2: ";
echo "\n\nAI21 Labs Jurassic-2:\n";
echo $bedrockRuntimeService->invokeJurassic2($prompt);
echo "\n---------------------------------------------------------------------\n";
$image_prompt = 'stylized picture of a cute old steampunk robot';
Expand All @@ -35,12 +35,12 @@ public function runExample()
$style_preset = 'photographic';
$base64 = $bedrockRuntimeService->invokeStableDiffusion($image_prompt, $diffusionSeed, $style_preset);
$image_path = $this->saveImage($base64, 'stability.stable-diffusion-xl');
echo "The generated images have been saved to $image_path";
echo "The generated image has been saved to $image_path";
echo "\n\nAmazon Titan Image Generation:\n";
$titanSeed = rand(0, 2147483647);
$base64 = $bedrockRuntimeService->invokeTitanImage($image_prompt, $titanSeed);
$image_path = $this->saveImage($base64, 'amazon.titan-image-generator-v1');
echo "The generated images have been saved to $image_path";
echo "The generated image has been saved to $image_path";
}

private function saveImage($base64_image_data, $model_id): string
Expand Down
8 changes: 4 additions & 4 deletions php/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ functions within the same service.

### AI21 Labs Jurassic-2

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

### Amazon Titan Image Generator

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

### Anthropic Claude

- [InvokeModel](BedrockRuntimeService.php#L31)

### Stable Diffusion

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


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

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

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
2 changes: 1 addition & 1 deletion php/example_code/bedrock-runtime/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"aws/aws-sdk-php": "^3.324.10",
"aws/aws-sdk-php": "^3.336",
"guzzlehttp/guzzle": "^7.9.2"
},
"autoload": {
Expand Down
18 changes: 9 additions & 9 deletions php/example_code/bedrock-runtime/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 135f4b8

Please sign in to comment.