Skip to content

Commit

Permalink
Kotlin: Add Example for Listing Amazon Bedrock Foundation Models (#5705)
Browse files Browse the repository at this point in the history
* Add .gitignore
  • Loading branch information
DennisTraub authored Nov 24, 2023
1 parent 8f22d4d commit d143327
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .doc_gen/metadata/bedrock_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions kotlin/services/bedrock/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
77 changes: 77 additions & 0 deletions kotlin/services/bedrock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!--Generated by WRITEME on 2023-11-23 17:01:49.458171 (UTC)-->
# Amazon Bedrock code examples for the SDK for Kotlin

## Overview

Shows how to use the AWS SDK for Kotlin to work with Amazon Bedrock.

<!--custom.overview.start-->
<!--custom.overview.end-->

*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).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `kotlin` folder.


<!--custom.prerequisites.start-->
> ⚠ 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).
<!--custom.prerequisites.end-->
### 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



<!--custom.instructions.start-->
<!--custom.instructions.end-->



### 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.



<!--custom.tests.start-->
<!--custom.tests.end-->

## 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)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
41 changes: 41 additions & 0 deletions kotlin/services/bedrock/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<KotlinCompile>() {
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")
}
Original file line number Diff line number Diff line change
@@ -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<FoundationModelSummary>? {
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]
25 changes: 25 additions & 0 deletions kotlin/services/bedrock/src/test/kotlin/BedrockTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit d143327

Please sign in to comment.