AWS code-library documentation change
Summary
Added documentation for Amazon Nova and Amazon Nova Canvas models, including code examples for using Bedrock's Converse API and image generation capabilities
Security assessment
The changes are focused on adding new model documentation and code examples for interacting with Amazon Nova and Amazon Nova Canvas. There is no mention of security vulnerabilities, security features, or security-related configurations in the changes.
Diff
diff --git a/code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md index cdbf6a6cd..c5c18151d 100644 --- a/code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md +++ b/code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md @@ -5 +5 @@ -ScenariosAI21 Labs Jurassic-2Amazon Titan TextAnthropic ClaudeCohere CommandMeta LlamaMistral AI +ScenariosAI21 Labs Jurassic-2Amazon NovaAmazon Nova CanvasAmazon Titan TextAnthropic ClaudeCohere CommandMeta LlamaMistral AI @@ -112,0 +113,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + * Amazon Nova + * Amazon Nova Canvas @@ -370,0 +373,235 @@ Use the Invoke Model API to send a text message. +## Amazon Nova + +The following code example shows how to send a text message to Amazon Nova, using Bedrock's Converse API. + +**SDK for JavaScript (v3)** + + +###### 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/javascriptv3/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 Titan Text. + + import { + BedrockRuntimeClient, + ConverseCommand, + } from "@aws-sdk/client-bedrock-runtime"; + + // Create a Bedrock Runtime client in the AWS Region you want to use. + const client = new BedrockRuntimeClient({ region: "us-east-1" }); + + // Set the model ID, e.g., Titan Text Premier. + const modelId = "amazon.titan-text-premier-v1:0"; + + // Start a conversation with the user message. + const userMessage = + "Describe the purpose of a 'hello world' program in one line."; + const conversation = [ + { + role: "user", + content: [{ text: userMessage }], + }, + ]; + + // Create a command with the model ID, the message, and a basic configuration. + const command = new ConverseCommand({ + modelId, + messages: conversation, + inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 }, + }); + + try { + // Send the command to the model and wait for the response + const response = await client.send(command); + + // Extract and print the response text. + const responseText = response.output.message.content[0].text; + console.log(responseText); + } catch (err) { + console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); + process.exit(1); + } + + + + + * For API details, see [Converse](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseCommand) in _AWS SDK for JavaScript API Reference_. + + + + +The following code example shows how to send a text message to Amazon Nova, using Bedrock's Converse API and process the response stream in real-time. + +**SDK for JavaScript (v3)** + + +###### 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/javascriptv3/example_code/bedrock-runtime#code-examples). + +Send a text message to Amazon Nova using Bedrock's Converse API and process the response stream in real-time. + + + // Use the Conversation API to send a text message to Mistral. + + import { + BedrockRuntimeClient, + ConverseCommand, + } from "@aws-sdk/client-bedrock-runtime"; + + // Create a Bedrock Runtime client in the AWS Region you want to use. + const client = new BedrockRuntimeClient({ region: "us-east-1" }); + + // Set the model ID, e.g., Mistral Large. + const modelId = "mistral.mistral-large-2402-v1:0"; + + // Start a conversation with the user message. + const userMessage = + "Describe the purpose of a 'hello world' program in one line."; + const conversation = [ + { + role: "user", + content: [{ text: userMessage }], + }, + ]; + + // Create a command with the model ID, the message, and a basic configuration. + const command = new ConverseCommand({ + modelId, + messages: conversation, + inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 }, + }); + + try { + // Send the command to the model and wait for the response + const response = await client.send(command); + + // Extract and print the response text. + const responseText = response.output.message.content[0].text; + console.log(responseText); + } catch (err) { + console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); + process.exit(1); + } + + + + + * For API details, see [ConverseStream](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseStreamCommand) in _AWS SDK for JavaScript API Reference_. + + + + +## Amazon Nova Canvas + +The following code example shows how to invoke Amazon Nova Canvas on Amazon Bedrock to generate an image. + +**SDK for JavaScript (v3)** + + +###### 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/javascriptv3/example_code/bedrock-runtime#code-examples). + +Create an image with Amazon Nova Canvas. + + + import { + BedrockRuntimeClient, + InvokeModelCommand, + } from "@aws-sdk/client-bedrock-runtime"; + import { saveImage } from "../../utils/image-creation.js"; + import { fileURLToPath } from "node:url"; + + /** + * This example demonstrates how to use Amazon Nova Canvas to generate images. + * It shows how to: + * - Set up the Amazon Bedrock runtime client + * - Configure the image generation parameters + * - Send a request to generate an image + * - Process the response and handle the generated image + * + * @returns {Promise<string>} Base64-encoded image data + */ + export const invokeModel = async () => { + // Step 1: Create the Amazon Bedrock runtime client + // Credentials will be automatically loaded from the environment + const client = new BedrockRuntimeClient({ region: "us-east-1" }); + + // Step 2: Specify which model to use + // For the latest available models, see: + // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html + const modelId = "amazon.nova-canvas-v1:0"; + + // Step 3: Configure the request payload + // First, set the main parameters: + // - prompt: Text description of the image to generate + // - seed: Random number for reproducible generation (0 to 858,993,459) + const prompt = "A stylized picture of a cute old steampunk robot"; + const seed = Math.floor(Math.random() * 858993460); + + // Then, create the payload using the following structure: + // - taskType: TEXT_IMAGE (specifies text-to-image generation) + // - textToImageParams: Contains the text prompt + // - imageGenerationConfig: Contains optional generation settings (seed, quality, etc.) + // For a list of available request parameters, see: + // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html + const payload = { + taskType: "TEXT_IMAGE", + textToImageParams: { + text: prompt, + }, + imageGenerationConfig: { + seed, + quality: "standard", + },