AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-05-31 · Documentation low

File: code-library/latest/ug/php_3_bedrock-runtime_code_examples.md

Summary

Updated code examples to replace AI21 Labs Jurassic-2 with Amazon Nova model and migrate from InvokeModel API to Converse API

Security assessment

The changes involve updating model references and switching API methods without any explicit mention of security vulnerabilities or security feature enhancements. The migration to Converse API appears to be a standard API version update rather than addressing security concerns.

Diff

diff --git a/code-library/latest/ug/php_3_bedrock-runtime_code_examples.md b/code-library/latest/ug/php_3_bedrock-runtime_code_examples.md
index 44db1b050..ca6f7bd74 100644
--- a//code-library/latest/ug/php_3_bedrock-runtime_code_examples.md
+++ b//code-library/latest/ug/php_3_bedrock-runtime_code_examples.md
@@ -5 +5 @@
-ScenariosAI21 Labs Jurassic-2Amazon Titan Image GeneratorAnthropic ClaudeStable Diffusion
+ScenariosAmazon NovaAmazon Titan Image GeneratorAnthropic ClaudeStable Diffusion
@@ -21 +21 @@ Each example includes a link to the complete source code, where you can find ins
-  * AI21 Labs Jurassic-2
+  * Amazon Nova
@@ -62,2 +61,0 @@ Invoke multiple LLMs on Amazon Bedrock.
-            echo "\n\nAI21 Labs Jurassic-2:\n";
-            echo $bedrockRuntimeService->invokeJurassic2($prompt);
@@ -112 +110 @@ Invoke multiple LLMs on Amazon Bedrock.
-## AI21 Labs Jurassic-2
+## Amazon Nova
@@ -114 +112 @@ Invoke multiple LLMs on Amazon Bedrock.
-The following code example shows how to send a text message to AI21 Labs Jurassic-2, using the Invoke Model API.
+The following code example shows how to send a text message to Amazon Nova, using Bedrock's Converse API.
@@ -123 +121 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-Use the Invoke Model API to send a text message.
+Send a text message to Amazon Nova, using Bedrock's Converse API.
@@ -126 +124,7 @@ Use the Invoke Model API to send a text message.
-        public function invokeJurassic2($prompt)
+    // Use the Conversation API to send a text message to Amazon Nova.
+    
+    use Aws\BedrockRuntime\BedrockRuntimeClient;
+    use Aws\Exception\AwsException;
+    use RuntimeException;
+    
+    class Converse
@@ -128,3 +132,7 @@ Use the Invoke Model API to send a text message.
-            # The different model providers have individual request and response formats.
-            # For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to:
-            # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html
+        public function converse(): string
+        {
+            // Create a Bedrock Runtime client in the AWS Region you want to use.
+            $client = new BedrockRuntimeClient([
+                'region' => 'us-east-1',
+                'profile' => 'default'
+            ]);
@@ -132,7 +140,10 @@ Use the Invoke Model API to send a text message.
-            $completion = "";
-            try {
-                $modelId = 'ai21.j2-mid-v1';
-                $body = [
-                    'prompt' => $prompt,
-                    'temperature' => 0.5,
-                    'maxTokens' => 200,
+            // Set the model ID, e.g., Amazon Nova Lite.
+            $modelId = 'amazon.nova-lite-v1:0';
+    
+            // Start a conversation with the user message.
+            $userMessage = "Describe the purpose of a 'hello world' program in one line.";
+            $conversation = [
+                [
+                    "role" => "user",
+                    "content" => [["text" => $userMessage]]
+                ]
@@ -140,3 +151,4 @@ Use the Invoke Model API to send a text message.
-                $result = $this->bedrockRuntimeClient->invokeModel([
-                    'contentType' => 'application/json',
-                    'body' => json_encode($body),
+    
+            try {
+                // Send the message to the model, using a basic inference configuration.
+                $response = $client->converse([
@@ -143,0 +156,5 @@ Use the Invoke Model API to send a text message.
+                    'messages' => $conversation,
+                    'inferenceConfig' => [
+                        'maxTokens' => 512,
+                        'temperature' => 0.5
+                    ]
@@ -145,5 +161,0 @@ Use the Invoke Model API to send a text message.
-                $response_body = json_decode($result['body']);
-                $completion = $response_body->completions[0]->data->text;
-            } catch (Exception $e) {
-                echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
-            }
@@ -151 +163,7 @@ Use the Invoke Model API to send a text message.
-            return $completion;
+                // Extract and return the response text.
+                $responseText = $response['output']['message']['content'][0]['text'];
+                return $responseText;
+            } catch (AwsException $e) {
+                echo "ERROR: Can't invoke {$modelId}. Reason: {$e->getAwsErrorMessage()}";
+                throw new RuntimeException("Failed to invoke model: " . $e->getAwsErrorMessage(), 0, $e);
+            }
@@ -152,0 +171,5 @@ Use the Invoke Model API to send a text message.
+    }
+    
+    $demo = new Converse();
+    echo $demo->converse();
+    
@@ -156 +179 @@ Use the Invoke Model API to send a text message.
-  * For API details, see [InvokeModel](https://docs.aws.amazon.com/goto/SdkForPHPV3/bedrock-runtime-2023-09-30/InvokeModel) in _AWS SDK for PHP API Reference_. 
+  * For API details, see [Converse](https://docs.aws.amazon.com/goto/SdkForPHPV3/bedrock-runtime-2023-09-30/Converse) in _AWS SDK for PHP API Reference_.