|
| 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 |
0 commit comments