Skip to content

Commit d93e928

Browse files
committed
feat: implement minimal bedrock-runtime example in kotlin
1 parent e2e5501 commit d93e928

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Amazon Bedrock Runtime code examples for the SDK for Kotlin
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Kotlin to work with Amazon Bedrock Runtime
6+
7+
<!--custom.overview.start-->
8+
This section provides examples that show how to invoke foundation models using the Amazon Bedrock Runtime API with the AWS SDK for Kotlin.
9+
<!--custom.overview.end-->
10+
11+
_Amazon Bedrock enables you to build and scale generative AI applications with foundation models._
12+
13+
## ⚠ Important
14+
15+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
16+
* Running the tests might result in charges to your AWS account.
17+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
18+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
19+
20+
<!--custom.important.start-->
21+
<!--custom.important.end-->
22+
23+
## Code examples
24+
25+
### Prerequisites
26+
27+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `kotlin` folder.
28+
29+
<!--custom.prerequisites.start-->
30+
> ⚠ 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).
31+
<!--custom.prerequisites.end-->
32+
33+
### Single actions
34+
35+
Code excerpts that show you how to call individual service functions.
36+
37+
- [InvokeModel](./src/main/kotlin/com/example/bedrockruntime/InvokeModel.kt) (Demonstrates how to invoke a foundation model to generate content)
38+
39+
<!--custom.examples.start-->
40+
<!--custom.examples.end-->
41+
42+
## Run the examples
43+
44+
### Instructions
45+
46+
<!--custom.instructions.start-->
47+
<!--custom.instructions.end-->
48+
49+
### Tests
50+
51+
⚠ Running tests might result in charges to your AWS account.
52+
53+
To find instructions for running these tests, see the [README](../../README.md#Tests)
54+
in the `kotlin` folder.
55+
56+
<!--custom.tests.start-->
57+
<!--custom.tests.end-->
58+
59+
## Additional resources
60+
61+
- [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
62+
- [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
63+
- [SDK for Kotlin Amazon Bedrock reference](https://sdk.amazonaws.com/kotlin/api/latest/bedrock/index.html)
64+
- [SDK for Kotlin Amazon Bedrock Runtime reference](https://sdk.amazonaws.com/kotlin/api/latest/bedrock-runtime/index.html)
65+
66+
<!--custom.resources.start-->
67+
<!--custom.resources.end-->
68+
69+
### Input and Output Format
70+
71+
**Input:**
72+
73+
```json
74+
{
75+
"inputText": "Your prompt here.",
76+
"textGenerationConfig": {
77+
"maxTokenCount": 2000,
78+
"stopSequences": [],
79+
"temperature": 1.0,
80+
"topP": 0.7
81+
}
82+
}
83+
```
84+
You may want to check the doc page [Inference Request Parameters](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html) on the AWS Bedrock documentation for more information on the parameters.
85+
The structure of the input varies depending on the model you are using.
86+
87+
**Output:**
88+
89+
```json
90+
/* Body only */
91+
{
92+
"inputTextTokenCount":7,
93+
"results":[
94+
{"tokenCount":9,
95+
"outputText":"Generated answer.",
96+
"completionReason":"FINISH"
97+
}
98+
]
99+
}
100+
```
101+
102+
---
103+
104+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
105+
106+
SPDX-License-Identifier: Apache-2.0
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
plugins {
2+
kotlin("jvm") version "2.1.10"
3+
id("org.jetbrains.kotlin.plugin.serialization") version "2.1.10"
4+
id("org.jlleitschuh.gradle.ktlint") version "11.3.1" apply true
5+
application
6+
}
7+
8+
group = "com.example.bedrockruntime"
9+
version = "1.0-SNAPSHOT"
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
buildscript {
16+
repositories {
17+
maven("https://plugins.gradle.org/m2/")
18+
}
19+
dependencies {
20+
classpath("org.jlleitschuh.gradle:ktlint-gradle:11.3.1")
21+
}
22+
}
23+
24+
dependencies {
25+
implementation("aws.sdk.kotlin:bedrockruntime:1.4.11")
26+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.8.0")
27+
testImplementation("org.junit.jupiter:junit-jupiter:5.11.4")
28+
}
29+
30+
application {
31+
mainClass.set("com.example.bedrockruntime.InvokeModelKt")
32+
}
33+
34+
// Java and Kotlin configuration
35+
kotlin {
36+
jvmToolchain(21)
37+
}
38+
39+
java {
40+
toolchain {
41+
languageVersion = JavaLanguageVersion.of(21)
42+
}
43+
}
44+
45+
tasks.test {
46+
useJUnitPlatform()
47+
testLogging {
48+
events("passed", "skipped", "failed")
49+
}
50+
51+
// Define the test source set
52+
testClassesDirs += files("build/classes/kotlin/test")
53+
classpath += files("build/classes/kotlin/main", "build/resources/main")
54+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.bedrockruntime
5+
6+
// snippet-start:[bedrock.kotlin.invoke_model.import]
7+
import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient
8+
import aws.sdk.kotlin.services.bedrockruntime.model.InvokeModelRequest
9+
import kotlinx.serialization.Serializable
10+
import kotlinx.serialization.json.Json
11+
// snippet-end:[bedrock.kotlin.invoke_model.import]
12+
13+
// snippet-start:[bedrock.kotlin.invoke_model.main]
14+
/**
15+
* Before running this Kotlin code example, set up your development environment, including your credentials.
16+
*
17+
* This example demonstrates how to invoke the Titan Text model (amazon.titan-text-lite-v1).
18+
* Remember that you must enable the model before you can use it. See notes in the README.md file.
19+
*
20+
* For more information, see the following documentation topic:
21+
* https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html
22+
*/
23+
suspend fun main() {
24+
val prompt = """
25+
Write a short, funny story about a time-traveling cat who
26+
ends up in ancient Egypt at the time of the pyramids.
27+
""".trimIndent()
28+
29+
val response = invokeModel(prompt, "amazon.titan-text-lite-v1")
30+
println("Generated story:\n$response")
31+
}
32+
33+
suspend fun invokeModel(prompt: String, modelId: String): String {
34+
BedrockRuntimeClient { region = "eu-central-1" }.use { client ->
35+
val request = InvokeModelRequest {
36+
this.modelId = modelId
37+
contentType = "application/json"
38+
accept = "application/json"
39+
body = """
40+
{
41+
"inputText": "${prompt.replace(Regex("\\s+"), " ").trim()}",
42+
"textGenerationConfig": {
43+
"maxTokenCount": 1000,
44+
"stopSequences": [],
45+
"temperature": 1,
46+
"topP": 0.7
47+
}
48+
}
49+
""".trimIndent().toByteArray()
50+
}
51+
52+
val response = client.invokeModel(request)
53+
val responseBody = response.body.toString(Charsets.UTF_8)
54+
55+
val jsonParser = Json { ignoreUnknownKeys = true }
56+
return jsonParser
57+
.decodeFromString<BedrockResponse>(responseBody)
58+
.results
59+
.first()
60+
.outputText
61+
}
62+
}
63+
64+
@Serializable
65+
private data class BedrockResponse(val results: List<Result>)
66+
67+
@Serializable
68+
private data class Result(val outputText: String)
69+
// snippet-end:[bedrock.kotlin.invoke_model.main]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import com.example.bedrockruntime.invokeModel
5+
import kotlinx.coroutines.runBlocking
6+
import org.junit.jupiter.api.Assertions.assertTrue
7+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
8+
import org.junit.jupiter.api.Order
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.TestInstance
11+
import org.junit.jupiter.api.TestMethodOrder
12+
13+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14+
@TestMethodOrder(OrderAnnotation::class)
15+
class InvokeModelTest {
16+
@Test
17+
@Order(1)
18+
fun listFoundationModels() = runBlocking {
19+
val prompt = "What is the capital of France?"
20+
21+
val answer = invokeModel(prompt, "amazon.titan-text-lite-v1")
22+
assertTrue(answer.isNotBlank())
23+
}
24+
}

0 commit comments

Comments
 (0)