|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// snippet-start:[javascript.v3.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration] |
| 5 | + |
| 6 | +import { |
| 7 | + BedrockRuntimeClient, |
| 8 | + InvokeModelCommand, |
| 9 | +} from "@aws-sdk/client-bedrock-runtime"; |
| 10 | +import { saveImage } from "../../utils/image-creation.js"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | + |
| 13 | +/** |
| 14 | + * This example demonstrates how to use Amazon Nova Canvas to generate images. |
| 15 | + * It shows how to: |
| 16 | + * - Set up the Amazon Bedrock runtime client |
| 17 | + * - Configure the image generation parameters |
| 18 | + * - Send a request to generate an image |
| 19 | + * - Process the response and handle the generated image |
| 20 | + * |
| 21 | + * @returns {Promise<string>} Base64-encoded image data |
| 22 | + */ |
| 23 | +export const invokeModel = async () => { |
| 24 | + // Step 1: Create the Amazon Bedrock runtime client |
| 25 | + // Credentials will be automatically loaded from the environment |
| 26 | + const client = new BedrockRuntimeClient({ region: "us-east-1" }); |
| 27 | + |
| 28 | + // Step 2: Specify which model to use |
| 29 | + // For the latest available models, see: |
| 30 | + // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html |
| 31 | + const modelId = "amazon.nova-canvas-v1:0"; |
| 32 | + |
| 33 | + // Step 3: Configure the request payload |
| 34 | + // First, set the main parameters: |
| 35 | + // - prompt: Text description of the image to generate |
| 36 | + // - seed: Random number for reproducible generation (0 to 858,993,459) |
| 37 | + const prompt = "A stylized picture of a cute old steampunk robot"; |
| 38 | + const seed = Math.floor(Math.random() * 858993460); |
| 39 | + |
| 40 | + // Then, create the payload using the following structure: |
| 41 | + // - taskType: TEXT_IMAGE (specifies text-to-image generation) |
| 42 | + // - textToImageParams: Contains the text prompt |
| 43 | + // - imageGenerationConfig: Contains optional generation settings (seed, quality, etc.) |
| 44 | + // For a list of available request parameters, see: |
| 45 | + // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html |
| 46 | + const payload = { |
| 47 | + taskType: "TEXT_IMAGE", |
| 48 | + textToImageParams: { |
| 49 | + text: prompt, |
| 50 | + }, |
| 51 | + imageGenerationConfig: { |
| 52 | + seed, |
| 53 | + quality: "standard", |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + // Step 4: Send and process the request |
| 58 | + // - Embed the payload in a request object |
| 59 | + // - Send the request to the model |
| 60 | + // - Extract and return the generated image data from the response |
| 61 | + try { |
| 62 | + const request = { |
| 63 | + modelId, |
| 64 | + body: JSON.stringify(payload), |
| 65 | + }; |
| 66 | + const response = await client.send(new InvokeModelCommand(request)); |
| 67 | + |
| 68 | + const decodedResponseBody = new TextDecoder().decode(response.body); |
| 69 | + // The response includes an array of base64-encoded PNG images |
| 70 | + /** @type {{images: string[]}} */ |
| 71 | + const responseBody = JSON.parse(decodedResponseBody); |
| 72 | + return responseBody.images[0]; // Base64-encoded image data |
| 73 | + } catch (error) { |
| 74 | + console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`); |
| 75 | + throw error; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +// If run directly, execute the example and save the generated image |
| 80 | +if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 81 | + console.log("Generating image. This may take a few seconds..."); |
| 82 | + invokeModel() |
| 83 | + .then(async (imageData) => { |
| 84 | + const imagePath = await saveImage(imageData, "nova-canvas"); |
| 85 | + // Example path: javascriptv3/example_code/bedrock-runtime/output/nova-canvas/image-01.png |
| 86 | + console.log(`Image saved to: ${imagePath}`); |
| 87 | + }) |
| 88 | + .catch((error) => { |
| 89 | + console.error("Execution failed:", error); |
| 90 | + process.exitCode = 1; |
| 91 | + }); |
| 92 | +} |
| 93 | +// snippet-end:[javascript.v3.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration] |
0 commit comments