diff --git a/.doc_gen/metadata/bedrock_metadata.yaml b/.doc_gen/metadata/bedrock_metadata.yaml index b4f88b6c746..972bd52f425 100644 --- a/.doc_gen/metadata/bedrock_metadata.yaml +++ b/.doc_gen/metadata/bedrock_metadata.yaml @@ -39,6 +39,14 @@ bedrock_ListFoundationModels: - description: List the available &BRlong; foundation models. snippet_tags: - bedrock.java2.list_foundation_models.main + Kotlin: + versions: + - sdk_version: 1 + github: kotlin/services/bedrock + excerpts: + - description: List the available &BRlong; foundation models. + snippet_tags: + - bedrock.kotlin.list_foundation_models.main PHP: versions: - sdk_version: 3 diff --git a/kotlin/services/bedrock/.gitignore b/kotlin/services/bedrock/.gitignore new file mode 100644 index 00000000000..3d6c834dd9e --- /dev/null +++ b/kotlin/services/bedrock/.gitignore @@ -0,0 +1,60 @@ +.DS_Store +.idea/shelf +/confluence/target +/dependencies/repo +/android.tests.dependencies +/dependencies/android.tests.dependencies +/dist +/local +/gh-pages +/ideaSDK +/clionSDK +/android-studio/sdk +out/ +/tmp +/intellij +workspace.xml +*.versionsBackup +/idea/testData/debugger/tinyApp/classes* +/jps-plugin/testData/kannotator +/js/js.translator/testData/out/ +/js/js.translator/testData/out-min/ +/js/js.translator/testData/out-pir/ +.gradle/ +build/ +!**/src/**/build +!**/test/**/build +*.iml +!**/testData/**/*.iml +.idea/artifacts +.idea/remote-targets.xml +.idea/libraries/Gradle*.xml +.idea/libraries/Maven*.xml +.idea/modules +.idea/runConfigurations/JPS_*.xml +.idea/runConfigurations/_JPS_*.xml +.idea/runConfigurations/PILL_*.xml +.idea/runConfigurations/_FP_*.xml +.idea/runConfigurations/_MT_*.xml +.idea/libraries +.idea/modules.xml +.idea/gradle.xml +.idea/compiler.xml +.idea/inspectionProfiles/profiles_settings.xml +.idea/.name +.idea/jarRepositories.xml +.idea/csv-plugin.xml +.idea/libraries-with-intellij-classes.xml +.idea/misc.xml +.idea/protoeditor.xml +node_modules/ +.rpt2_cache/ +libraries/tools/kotlin-test-js-runner/lib/ +local.properties +buildSrcTmp/ +distTmp/ +outTmp/ +/test.output +/kotlin-native/dist +kotlin-ide/ +.kotlin/ \ No newline at end of file diff --git a/kotlin/services/bedrock/README.md b/kotlin/services/bedrock/README.md new file mode 100644 index 00000000000..24adcaef01e --- /dev/null +++ b/kotlin/services/bedrock/README.md @@ -0,0 +1,77 @@ + +# Amazon Bedrock code examples for the SDK for Kotlin + +## Overview + +Shows how to use the AWS SDK for Kotlin to work with Amazon Bedrock. + + + + +*Amazon Bedrock enables you to build and scale generative AI applications with foundation models.* + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). +* Running the tests might result in charges to your AWS account. +* 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). +* 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). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `kotlin` folder. + + + +> ⚠ 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). + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +* [List available Amazon Bedrock foundation models](src/main/kotlin/com/example/bedrock/ListFoundationModels.kt#L31) (`ListFoundationModels`) + +## Run the examples + +### Instructions + + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `kotlin` folder. + + + + + + +## Additional resources + +* [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) +* [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [SDK for Kotlin Amazon Bedrock reference](https://sdk.amazonaws.com/kotlin/api/latest/bedrock/index.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/kotlin/services/bedrock/build.gradle.kts b/kotlin/services/bedrock/build.gradle.kts new file mode 100644 index 00000000000..a5bb49ad270 --- /dev/null +++ b/kotlin/services/bedrock/build.gradle.kts @@ -0,0 +1,41 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + kotlin("jvm") version "1.9.21" + application +} + +group = "com.example.bedrock" +version = "1.0-SNAPSHOT" + +buildscript { + repositories { + maven("https://plugins.gradle.org/m2/") + } + dependencies { + classpath("org.jlleitschuh.gradle:ktlint-gradle:11.3.1") + } +} + +repositories { + mavenCentral() +} +apply(plugin = "org.jlleitschuh.gradle.ktlint") +dependencies { + implementation("aws.sdk.kotlin:bedrock:0.33.1-beta") + implementation("aws.sdk.kotlin:sts:0.33.1-beta") + testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") +} +tasks.withType() { + kotlinOptions.jvmTarget = "17" +} +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") +} diff --git a/kotlin/services/bedrock/src/main/kotlin/com/example/bedrock/ListFoundationModels.kt b/kotlin/services/bedrock/src/main/kotlin/com/example/bedrock/ListFoundationModels.kt new file mode 100644 index 00000000000..d3977dd08d5 --- /dev/null +++ b/kotlin/services/bedrock/src/main/kotlin/com/example/bedrock/ListFoundationModels.kt @@ -0,0 +1,50 @@ +// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] +// snippet-sourcedescription:[ListFoundationModels.kt demonstrates how to list the available foundation models.] +// snippet-keyword:[AWS SDK for Kotlin] +// snippet-service:[Amazon Bedrock] + +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +package com.example.bedrock + +// snippet-start:[bedrock.kotlin.list_foundation_models.import] +import aws.sdk.kotlin.services.bedrock.BedrockClient +import aws.sdk.kotlin.services.bedrock.model.FoundationModelSummary +import aws.sdk.kotlin.services.bedrock.model.ListFoundationModelsRequest +// snippet-end:[bedrock.kotlin.list_foundation_models.import] + +/** + * Before running this Kotlin code example, set up your development environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main() { + listFoundationModels() +} + +// snippet-start:[bedrock.kotlin.list_foundation_models.main] +suspend fun listFoundationModels(): List? { + BedrockClient { region = "us-east-1" }.use { bedrockClient -> + val response = bedrockClient.listFoundationModels(ListFoundationModelsRequest {}) + response.modelSummaries?.forEach { model -> + println("==========================================") + println(" Model ID: ${model.modelId}") + println("------------------------------------------") + println(" Name: ${model.modelName}") + println(" Provider: ${model.providerName}") + println(" Input modalities: ${model.inputModalities}") + println(" Output modalities: ${model.outputModalities}") + println(" Supported customizations: ${model.customizationsSupported}") + println(" Supported inference types: ${model.inferenceTypesSupported}") + println("------------------------------------------\n") + } + return response.modelSummaries + } +} +// snippet-end:[bedrock.kotlin.list_foundation_models.main] diff --git a/kotlin/services/bedrock/src/test/kotlin/BedrockTest.kt b/kotlin/services/bedrock/src/test/kotlin/BedrockTest.kt new file mode 100644 index 00000000000..0742cf78e3c --- /dev/null +++ b/kotlin/services/bedrock/src/test/kotlin/BedrockTest.kt @@ -0,0 +1,25 @@ +/* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 +*/ + +import com.example.bedrock.listFoundationModels +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation +import org.junit.jupiter.api.Order +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.TestMethodOrder + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestMethodOrder(OrderAnnotation::class) +class BedrockTest { + + @Test + @Order(1) + fun ListFoundationModels() = runBlocking { + val modelSummaries = listFoundationModels() + assertFalse(modelSummaries!!.isEmpty()) + } +}