From 135f4b896ced0dd5d09d4f37b4304025d189e820 Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Mon, 13 Jan 2025 17:40:12 +0100 Subject: [PATCH] Bump Anthropic Claude to v3 and use Messages API instead of now outdated Text Completions API --- .../bedrock-runtime/BedrockRuntimeService.php | 14 ++++++++------ .../GettingStartedWithBedrockRuntime.php | 8 ++++---- php/example_code/bedrock-runtime/README.md | 8 ++++---- php/example_code/bedrock-runtime/composer.json | 2 +- php/example_code/bedrock-runtime/composer.lock | 18 +++++++++--------- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/php/example_code/bedrock-runtime/BedrockRuntimeService.php b/php/example_code/bedrock-runtime/BedrockRuntimeService.php index db0439b8b2c..f311880fc2c 100644 --- a/php/example_code/bedrock-runtime/BedrockRuntimeService.php +++ b/php/example_code/bedrock-runtime/BedrockRuntimeService.php @@ -37,14 +37,16 @@ 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', @@ -52,7 +54,7 @@ public function invokeClaude($prompt) '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"; } diff --git a/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php b/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php index 50006863ae4..271698bc5e8 100644 --- a/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php +++ b/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php @@ -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'; @@ -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 diff --git a/php/example_code/bedrock-runtime/README.md b/php/example_code/bedrock-runtime/README.md index 524789fa3fd..402bf239b40 100644 --- a/php/example_code/bedrock-runtime/README.md +++ b/php/example_code/bedrock-runtime/README.md @@ -44,11 +44,11 @@ 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 @@ -56,7 +56,7 @@ functions within the same service. ### Stable Diffusion -- [InvokeModel](BedrockRuntimeService.php#L94) +- [InvokeModel](BedrockRuntimeService.php#L96) @@ -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 \ No newline at end of file +SPDX-License-Identifier: Apache-2.0 diff --git a/php/example_code/bedrock-runtime/composer.json b/php/example_code/bedrock-runtime/composer.json index e7cc40b048a..877018afa42 100644 --- a/php/example_code/bedrock-runtime/composer.json +++ b/php/example_code/bedrock-runtime/composer.json @@ -1,6 +1,6 @@ { "require": { - "aws/aws-sdk-php": "^3.324.10", + "aws/aws-sdk-php": "^3.336", "guzzlehttp/guzzle": "^7.9.2" }, "autoload": { diff --git a/php/example_code/bedrock-runtime/composer.lock b/php/example_code/bedrock-runtime/composer.lock index fb4fc596c65..3483421752b 100644 --- a/php/example_code/bedrock-runtime/composer.lock +++ b/php/example_code/bedrock-runtime/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f5911af905339ceaef8c3ec21747f45a", + "content-hash": "d230eb9467db7bff94de3a670e473719", "packages": [ { "name": "aws/aws-crt-php", @@ -62,16 +62,16 @@ }, { "name": "aws/aws-sdk-php", - "version": "3.324.10", + "version": "3.336.13", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "702da8ee8a9dbae919c3559b8f4dba61cd8355b0" + "reference": "dcb43c029ca74c52fa03a739341cc77086296a83" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/702da8ee8a9dbae919c3559b8f4dba61cd8355b0", - "reference": "702da8ee8a9dbae919c3559b8f4dba61cd8355b0", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/dcb43c029ca74c52fa03a739341cc77086296a83", + "reference": "dcb43c029ca74c52fa03a739341cc77086296a83", "shasum": "" }, "require": { @@ -100,8 +100,8 @@ "nette/neon": "^2.3", "paragonie/random_compat": ">= 2", "phpunit/phpunit": "^5.6.3 || ^8.5 || ^9.5", - "psr/cache": "^1.0", - "psr/simple-cache": "^1.0", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0", "sebastian/comparator": "^1.2.3 || ^4.0", "yoast/phpunit-polyfills": "^1.0" }, @@ -154,9 +154,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "issues": "https://github.com/aws/aws-sdk-php/issues", - "source": "https://github.com/aws/aws-sdk-php/tree/3.324.10" + "source": "https://github.com/aws/aws-sdk-php/tree/3.336.13" }, - "time": "2024-10-24T18:08:37+00:00" + "time": "2025-01-10T19:04:25+00:00" }, { "name": "guzzlehttp/guzzle",