-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathinvokeModel.js
More file actions
93 lines (84 loc) · 3.48 KB
/
invokeModel.js
File metadata and controls
93 lines (84 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[javascript.v3.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration]
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",
},
};
// Step 4: Send and process the request
// - Embed the payload in a request object
// - Send the request to the model
// - Extract and return the generated image data from the response
try {
const request = {
modelId,
body: JSON.stringify(payload),
};
const response = await client.send(new InvokeModelCommand(request));
const decodedResponseBody = new TextDecoder().decode(response.body);
// The response includes an array of base64-encoded PNG images
/** @type {{images: string[]}} */
const responseBody = JSON.parse(decodedResponseBody);
return responseBody.images[0]; // Base64-encoded image data
} catch (error) {
console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`);
throw error;
}
};
// If run directly, execute the example and save the generated image
if (process.argv[1] === fileURLToPath(import.meta.url)) {
console.log("Generating image. This may take a few seconds...");
invokeModel()
.then(async (imageData) => {
const imagePath = await saveImage(imageData, "nova-canvas");
// Example path: javascriptv3/example_code/bedrock-runtime/output/nova-canvas/image-01.png
console.log(`Image saved to: ${imagePath}`);
})
.catch((error) => {
console.error("Execution failed:", error);
process.exitCode = 1;
});
}
// snippet-end:[javascript.v3.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration]