AWS code-library documentation change
Summary
Added PHP SDK code example for using Bedrock's Converse API with Amazon Nova text model
Security assessment
The change adds a standard code example for API usage without any security-specific configurations or warnings. It demonstrates normal usage of the Converse API with basic inference parameters.
Diff
diff --git a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Converse_AmazonNovaText_section.md b/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Converse_AmazonNovaText_section.md index f31134ded..8bb2857b0 100644 --- a//code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Converse_AmazonNovaText_section.md +++ b//code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_Converse_AmazonNovaText_section.md @@ -699,0 +700,73 @@ Send a text message to Amazon Nova, using Bedrock's Converse API. +PHP + + +**SDK for PHP** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/bedrock-runtime#code-examples). + +Send a text message to Amazon Nova, using Bedrock's Converse API. + + + // Use the Conversation API to send a text message to Amazon Nova. + + use Aws\BedrockRuntime\BedrockRuntimeClient; + use Aws\Exception\AwsException; + use RuntimeException; + + class Converse + { + 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' + ]); + + // 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]] + ] + ]; + + try { + // Send the message to the model, using a basic inference configuration. + $response = $client->converse([ + 'modelId' => $modelId, + 'messages' => $conversation, + 'inferenceConfig' => [ + 'maxTokens' => 512, + 'temperature' => 0.5 + ] + ]); + + // 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); + } + } + } + + $demo = new Converse(); + echo $demo->converse(); + + + + + * 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_. + + + +