Skip to content

Commit dcf15e4

Browse files
committed
Fix formatting
1 parent dc398c8 commit dcf15e4

File tree

10 files changed

+43
-46
lines changed

10 files changed

+43
-46
lines changed

kotlin/services/bedrock-runtime/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `kotli
3030
<!--custom.prerequisites.start-->
3131
> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).
3232
<!--custom.prerequisites.end-->
33-
3433
### Amazon Nova
3534

3635
- [Converse](src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/text/Converse.kt#L6)

kotlin/services/bedrock-runtime/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ tasks.test {
3838

3939
application {
4040
mainClass.set("com.example.bedrockruntime.InvokeModelKt")
41-
}
41+
}

kotlin/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/canvas/InvokeModel.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,17 @@ suspend fun invokeModel(): ByteArray {
6565
"quality": "standard"
6666
}
6767
}
68-
""".trimIndent()
68+
""".trimIndent()
6969

7070
// Send the request and process the model's response
7171
runCatching {
7272
// Send the request to the model
73-
val response = client.invokeModel(InvokeModelRequest {
74-
this.modelId = modelId
75-
body = request.toByteArray()
76-
})
73+
val response = client.invokeModel(
74+
InvokeModelRequest {
75+
this.modelId = modelId
76+
body = request.toByteArray()
77+
},
78+
)
7779

7880
// Parse the response and extract the generated image
7981
val jsonResponse = response.body.toString(Charsets.UTF_8)
@@ -82,12 +84,11 @@ suspend fun invokeModel(): ByteArray {
8284
// Extract the generated image and return it as a byte array for better handling
8385
val base64Image = parsedResponse.images.first()
8486
return Base64.getDecoder().decode(base64Image)
85-
8687
}.getOrElse { error ->
8788
System.err.println("ERROR: Can't invoke '$modelId'. Reason: ${error.message}")
8889
throw RuntimeException("Failed to generate image with model $modelId", error)
8990
}
9091
}
9192
}
9293

93-
// snippet-end:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration]
94+
// snippet-end:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration]

kotlin/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/text/Converse.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ suspend fun converse(): String {
4343
this.modelId = modelId
4444
messages = listOf(message)
4545
inferenceConfig {
46-
maxTokens = 500 // Maximum response length
47-
temperature = 0.5F // Lower values: more focused output
48-
// topP = 0.8F // Alternative to temperature
46+
maxTokens = 500 // Maximum response length
47+
temperature = 0.5F // Lower values: more focused output
48+
// topP = 0.8F // Alternative to temperature
4949
}
5050
}
5151

5252
// Send the request and process the model's response
5353
runCatching {
5454
val response = client.converse(request)
5555
return response.output!!.asMessage().content.first().asText()
56-
5756
}.getOrElse { error ->
5857
error.message?.let { e -> System.err.println("ERROR: Can't invoke '$modelId'. Reason: $e") }
5958
throw RuntimeException("Failed to generate text with model $modelId", error)

kotlin/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/models/amazon/nova/text/ConverseStream.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ package com.example.bedrockruntime.models.amazon.nova.text
66
// snippet-start:[bedrock-runtime.kotlin.ConverseStream_AmazonNovaText]
77

88
import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient
9-
import aws.sdk.kotlin.services.bedrockruntime.model.*
9+
import aws.sdk.kotlin.services.bedrockruntime.model.ContentBlock
10+
import aws.sdk.kotlin.services.bedrockruntime.model.ConversationRole
11+
import aws.sdk.kotlin.services.bedrockruntime.model.ConverseStreamOutput
12+
import aws.sdk.kotlin.services.bedrockruntime.model.ConverseStreamRequest
13+
import aws.sdk.kotlin.services.bedrockruntime.model.Message
1014

1115
/**
1216
* This example demonstrates how to use the Amazon Nova foundation models
@@ -44,9 +48,9 @@ suspend fun converseStream(): String {
4448
this.modelId = modelId
4549
messages = listOf(message)
4650
inferenceConfig {
47-
maxTokens = 500 // Maximum response length
48-
temperature = 0.5F // Lower values: more focused output
49-
// topP = 0.8F // Alternative to temperature
51+
maxTokens = 500 // Maximum response length
52+
temperature = 0.5F // Lower values: more focused output
53+
// topP = 0.8F // Alternative to temperature
5054
}
5155
}
5256

@@ -76,4 +80,4 @@ suspend fun converseStream(): String {
7680
return completeResponseBuffer.toString()
7781
}
7882

79-
// snippet-end:[bedrock-runtime.kotlin.ConverseStream_AmazonNovaText]
83+
// snippet-end:[bedrock-runtime.kotlin.ConverseStream_AmazonNovaText]

kotlin/services/bedrock-runtime/src/main/kotlin/com/example/bedrockruntime/models/amazon/titan/text/InvokeModel.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ suspend fun main() {
2727
private data class BedrockResponse(val results: List<Result>) {
2828
@Serializable
2929
data class Result(
30-
val outputText: String
30+
val outputText: String,
3131
)
3232
}
3333

34-
3534
// Initialize JSON parser with relaxed configuration
3635
private val json = Json { ignoreUnknownKeys = true }
3736

@@ -55,15 +54,17 @@ suspend fun invokeModel(): String {
5554
"temperature": 0.5
5655
}
5756
}
58-
""".trimIndent()
57+
""".trimIndent()
5958

6059
// Send the request and process the model's response
6160
runCatching {
6261
// Send the request to the model
63-
val response = client.invokeModel(InvokeModelRequest {
64-
this.modelId = modelId
65-
body = request.toByteArray()
66-
})
62+
val response = client.invokeModel(
63+
InvokeModelRequest {
64+
this.modelId = modelId
65+
body = request.toByteArray()
66+
},
67+
)
6768

6869
// Convert the response bytes to a JSON string
6970
val jsonResponse = response.body.toString(Charsets.UTF_8)
@@ -73,7 +74,6 @@ suspend fun invokeModel(): String {
7374

7475
// Extract and return the generated text
7576
return parsedResponse.results.firstOrNull()!!.outputText
76-
7777
}.getOrElse { error ->
7878
error.message?.let { msg ->
7979
System.err.println("ERROR: Can't invoke '$modelId'. Reason: $msg")

kotlin/services/bedrock-runtime/src/test/kotlin/models/AbstractModelTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ abstract class AbstractModelTest {
8181
*/
8282
data class ModelTest(
8383
val name: String,
84-
val function: suspend () -> Any
84+
val function: suspend () -> Any,
8585
)
8686
}

kotlin/services/bedrock-runtime/src/test/kotlin/models/TestConverse.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ class TestConverse : AbstractModelTest() {
1414
* Creates test cases that validate each model's ability to generate
1515
* and return text responses.
1616
*/
17-
override fun modelProvider(): Stream<ModelTest> {
18-
return listOf(
19-
ModelTest("Amazon Nova") { com.example.bedrockruntime.models.amazon.nova.text.converse() }
20-
).stream()
21-
}
22-
}
17+
override fun modelProvider(): Stream<ModelTest> = listOf(
18+
ModelTest("Amazon Nova") { com.example.bedrockruntime.models.amazon.nova.text.converse() },
19+
).stream()
20+
}

kotlin/services/bedrock-runtime/src/test/kotlin/models/TestConverseStream.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ class TestConverseStream : AbstractModelTest() {
1414
* Creates test cases that validate each model's ability to generate
1515
* and return streaming text responses.
1616
*/
17-
override fun modelProvider(): Stream<ModelTest> {
18-
return listOf(
19-
ModelTest("Amazon Nova") { com.example.bedrockruntime.models.amazon.nova.text.converseStream() }
20-
).stream()
21-
}
22-
}
17+
override fun modelProvider(): Stream<ModelTest> = listOf(
18+
ModelTest("Amazon Nova") { com.example.bedrockruntime.models.amazon.nova.text.converseStream() },
19+
).stream()
20+
}

kotlin/services/bedrock-runtime/src/test/kotlin/models/TestInvokeModel.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ class TestInvokeModel : AbstractModelTest() {
1414
* Creates test cases that validate each model's ability to generate
1515
* and return text or byte[] responses.
1616
*/
17-
override fun modelProvider(): Stream<ModelTest> {
18-
return listOf(
19-
ModelTest("Amazon Titan Text") { com.example.bedrockruntime.models.amazon.titan.text.invokeModel() },
20-
ModelTest("Amazon Nova Canvas") { com.example.bedrockruntime.models.amazon.nova.canvas.invokeModel() }
21-
).stream()
22-
}
23-
}
17+
override fun modelProvider(): Stream<ModelTest> = listOf(
18+
ModelTest("Amazon Titan Text") { com.example.bedrockruntime.models.amazon.titan.text.invokeModel() },
19+
ModelTest("Amazon Nova Canvas") { com.example.bedrockruntime.models.amazon.nova.canvas.invokeModel() },
20+
).stream()
21+
}

0 commit comments

Comments
 (0)