-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Kotlin: Amazon Nova text and image generation examples #7257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6d6ac7
Refactor InvokeModel and add Converse
DennisTraub dfe64a7
Add ConverseStream
DennisTraub 8d758ee
Add example for Nova Canvas
DennisTraub 7cea744
Add Nova examples to Readme
DennisTraub c332ffe
Add Kdoc to the image utility
DennisTraub 536defc
Fix formatting
DennisTraub bc962d9
Fix gradle config
DennisTraub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,41 @@ | ||
| plugins { | ||
| kotlin("jvm") version "2.1.10" | ||
| id("org.jetbrains.kotlin.plugin.serialization") version "2.1.10" | ||
| id("org.jlleitschuh.gradle.ktlint") version "11.3.1" apply true | ||
| id("org.jlleitschuh.gradle.ktlint") version "12.1.1" | ||
| application | ||
| } | ||
|
|
||
| group = "com.example.bedrockruntime" | ||
| version = "1.0-SNAPSHOT" | ||
|
|
||
| val awsSdkVersion = "1.4.27" | ||
| val junitVersion = "5.12.0" | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| buildscript { | ||
| repositories { | ||
| maven("https://plugins.gradle.org/m2/") | ||
| } | ||
| dependencies { | ||
| classpath("org.jlleitschuh.gradle:ktlint-gradle:11.3.1") | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("aws.sdk.kotlin:bedrockruntime:1.4.11") | ||
| implementation("aws.sdk.kotlin:bedrockruntime:$awsSdkVersion") | ||
| implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.8.0") | ||
| testImplementation("org.junit.jupiter:junit-jupiter:5.11.4") | ||
| } | ||
|
|
||
| application { | ||
| mainClass.set("com.example.bedrockruntime.InvokeModelKt") | ||
| testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion") | ||
| testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1") | ||
| testImplementation("org.jetbrains.kotlin:kotlin-reflect") | ||
| testRuntimeOnly("org.junit.platform:junit-platform-launcher") | ||
| } | ||
|
|
||
| // Java and Kotlin configuration | ||
| kotlin { | ||
| jvmToolchain(21) | ||
| } | ||
|
|
||
| java { | ||
| toolchain { | ||
| languageVersion = JavaLanguageVersion.of(21) | ||
| } | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| testLogging { | ||
| events("passed", "skipped", "failed") | ||
| } | ||
| } | ||
|
|
||
| // Define the test source set | ||
| testClassesDirs += files("build/classes/kotlin/test") | ||
| classpath += files("build/classes/kotlin/main", "build/resources/main") | ||
| application { | ||
| mainClass.set("com.example.bedrockruntime.InvokeModelKt") | ||
| } | ||
67 changes: 0 additions & 67 deletions
67
kotlin/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/InvokeModel.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
...in/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/libs/ImageTools.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package com.example.bedrockruntime.libs | ||
|
|
||
| import java.io.ByteArrayInputStream | ||
| import java.io.IOException | ||
| import javax.imageio.ImageIO | ||
| import javax.swing.ImageIcon | ||
| import javax.swing.JFrame | ||
| import javax.swing.JLabel | ||
|
|
||
| /** | ||
| * Utility object for handling image-related operations. | ||
| */ | ||
| object ImageTools { | ||
| /** | ||
| * Displays a byte array as an image in a new window. | ||
| * | ||
| * Creates a new JFrame window that displays the image represented by the provided byte array. | ||
| * The window will close the application when closed (EXIT_ON_CLOSE). | ||
| * | ||
| * @param imageData The image data as a byte array | ||
| * @throws RuntimeException if there is an error reading the image data | ||
| */ | ||
| fun displayImage(imageData: ByteArray) { | ||
| try { | ||
| val image = ImageIO.read(ByteArrayInputStream(imageData)) | ||
| JFrame("Image").apply { | ||
| defaultCloseOperation = JFrame.EXIT_ON_CLOSE | ||
| contentPane.add(JLabel(ImageIcon(image))) | ||
| pack() | ||
| isVisible = true | ||
| } | ||
| } catch (e: IOException) { | ||
| throw RuntimeException(e) | ||
| } | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
...ntime/src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/canvas/InvokeModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package com.example.bedrockruntime.models.amazon.nova.canvas | ||
|
|
||
| // snippet-start:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration] | ||
|
|
||
| import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient | ||
| import aws.sdk.kotlin.services.bedrockruntime.model.InvokeModelRequest | ||
| import com.example.bedrockruntime.libs.ImageTools.displayImage | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.Json | ||
| import java.util.* | ||
|
|
||
| /** | ||
| * 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 display the generated image | ||
| */ | ||
| suspend fun main() { | ||
| println("Generating image. This may take a few seconds...") | ||
| val imageData = invokeModel() | ||
| displayImage(imageData) | ||
| } | ||
|
|
||
| // Data class for parsing the model's response | ||
| @Serializable | ||
| private data class Response(val images: List<String>) | ||
|
|
||
| // Configure JSON parser to ignore unknown fields in the response | ||
| private val json = Json { ignoreUnknownKeys = true } | ||
|
|
||
| suspend fun invokeModel(): ByteArray { | ||
| // Create and configure the Bedrock runtime client | ||
| BedrockRuntimeClient { region = "us-east-1" }.use { client -> | ||
|
|
||
| // Specify the model ID. For the latest available models, see: | ||
| // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html | ||
| val modelId = "amazon.nova-canvas-v1:0" | ||
|
|
||
| // Configure the generation parameters and create the request | ||
| // First, set the main parameters: | ||
| // - prompt: Text description of the image to generate | ||
| // - seed: Random number for reproducible generation (0 to 858,993,459) | ||
| val prompt = "A stylized picture of a cute old steampunk robot" | ||
| val seed = (0..858_993_459).random() | ||
|
|
||
| // Then, create the request using a template with 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 | ||
| val request = """ | ||
| { | ||
| "taskType": "TEXT_IMAGE", | ||
| "textToImageParams": { | ||
| "text": "$prompt" | ||
| }, | ||
| "imageGenerationConfig": { | ||
| "seed": $seed, | ||
| "quality": "standard" | ||
| } | ||
| } | ||
| """.trimIndent() | ||
|
|
||
| // Send the request and process the model's response | ||
| runCatching { | ||
| // Send the request to the model | ||
| val response = client.invokeModel( | ||
| InvokeModelRequest { | ||
| this.modelId = modelId | ||
| body = request.toByteArray() | ||
| }, | ||
| ) | ||
|
|
||
| // Parse the response and extract the generated image | ||
| val jsonResponse = response.body.toString(Charsets.UTF_8) | ||
| val parsedResponse = json.decodeFromString<Response>(jsonResponse) | ||
|
|
||
| // Extract the generated image and return it as a byte array for better handling | ||
| val base64Image = parsedResponse.images.first() | ||
| return Base64.getDecoder().decode(base64Image) | ||
| }.getOrElse { error -> | ||
| System.err.println("ERROR: Can't invoke '$modelId'. Reason: ${error.message}") | ||
| throw RuntimeException("Failed to generate image with model $modelId", error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // snippet-end:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration] |
62 changes: 62 additions & 0 deletions
62
...ck-runtime/src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/text/Converse.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package com.example.bedrockruntime.models.amazon.nova.text | ||
|
|
||
| // snippet-start:[bedrock-runtime.kotlin.Converse_AmazonNovaText] | ||
|
|
||
| import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient | ||
| import aws.sdk.kotlin.services.bedrockruntime.model.ContentBlock | ||
| import aws.sdk.kotlin.services.bedrockruntime.model.ConversationRole | ||
| import aws.sdk.kotlin.services.bedrockruntime.model.ConverseRequest | ||
| import aws.sdk.kotlin.services.bedrockruntime.model.Message | ||
|
|
||
| /** | ||
| * This example demonstrates how to use the Amazon Nova foundation models to generate text. | ||
| * It shows how to: | ||
| * - Set up the Amazon Bedrock runtime client | ||
| * - Create a message | ||
| * - Configure and send a request | ||
| * - Process the response | ||
| */ | ||
| suspend fun main() { | ||
| converse().also { println(it) } | ||
| } | ||
|
|
||
| suspend fun converse(): String { | ||
| // Create and configure the Bedrock runtime client | ||
| BedrockRuntimeClient { region = "us-east-1" }.use { client -> | ||
|
|
||
| // Specify the model ID. For the latest available models, see: | ||
| // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html | ||
| val modelId = "amazon.nova-lite-v1:0" | ||
|
|
||
| // Create the message with the user's prompt | ||
| val prompt = "Describe the purpose of a 'hello world' program in one line." | ||
| val message = Message { | ||
| role = ConversationRole.User | ||
| content = listOf(ContentBlock.Text(prompt)) | ||
| } | ||
|
|
||
| // Configure the request with optional model parameters | ||
| val request = ConverseRequest { | ||
| this.modelId = modelId | ||
| messages = listOf(message) | ||
| inferenceConfig { | ||
| maxTokens = 500 // Maximum response length | ||
| temperature = 0.5F // Lower values: more focused output | ||
| // topP = 0.8F // Alternative to temperature | ||
| } | ||
| } | ||
|
|
||
| // Send the request and process the model's response | ||
| runCatching { | ||
| val response = client.converse(request) | ||
| return response.output!!.asMessage().content.first().asText() | ||
| }.getOrElse { error -> | ||
| error.message?.let { e -> System.err.println("ERROR: Can't invoke '$modelId'. Reason: $e") } | ||
| throw RuntimeException("Failed to generate text with model $modelId", error) | ||
| } | ||
| } | ||
| } | ||
| // snippet-end:[bedrock-runtime.kotlin.Converse_AmazonNovaText] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.