Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java v2: Implementation and tests for synchronous and asynchronous invocations of Amazon Bedrock models #5630

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2380ef3
Invoke model
DennisTraub Nov 9, 2023
245da6d
Add Readme
DennisTraub Nov 9, 2023
ab8f432
Add InvokeModelWithResponseStream example
DennisTraub Nov 9, 2023
cc76c57
Merge branch 'awsdocs:main' into bedrock-runtime-invoke-model
DennisTraub Nov 11, 2023
63f4f48
Invoke model
DennisTraub Nov 9, 2023
e0d3601
Add Readme
DennisTraub Nov 9, 2023
331eb64
Add InvokeModelWithResponseStream example
DennisTraub Nov 9, 2023
6571886
Upgrade to Java 17
DennisTraub Nov 11, 2023
af75cf7
Configure readme generation
DennisTraub Nov 11, 2023
43a4747
Add generated readme
DennisTraub Nov 11, 2023
39dcd10
Add custom prerequisites
DennisTraub Nov 11, 2023
e943c51
Merge branch 'bedrock-runtime-invoke-model' of github.com:DennisTraub…
DennisTraub Nov 11, 2023
a0d58c3
Delete javav2/example_code/bedrock/BedrockJ2Project.iml
DennisTraub Nov 11, 2023
54a12ce
House-cleaning after moving the module
DennisTraub Nov 11, 2023
11b7e02
Add a code sample for AI21 Labs Jurassic-2
DennisTraub Nov 12, 2023
6653ad2
Add a code sample for Stability.ai Stable Diffusion
DennisTraub Nov 12, 2023
1fd0edf
Merge branch 'awsdocs:main' into bedrock-runtime-invoke-model
DennisTraub Nov 13, 2023
639c143
Update metadata
DennisTraub Nov 13, 2023
7ecf5cc
Invoke model
DennisTraub Nov 9, 2023
7f191f4
Add Readme
DennisTraub Nov 9, 2023
ea1d4a9
Add InvokeModelWithResponseStream example
DennisTraub Nov 9, 2023
90533b2
Upgrade to Java 17
DennisTraub Nov 11, 2023
4b7a82c
Configure readme generation
DennisTraub Nov 11, 2023
7d2c091
Add generated readme
DennisTraub Nov 11, 2023
b5261d3
Add custom prerequisites
DennisTraub Nov 11, 2023
ffc1792
Invoke model
DennisTraub Nov 9, 2023
d99aa6b
Add Readme
DennisTraub Nov 9, 2023
f91fd83
Add InvokeModelWithResponseStream example
DennisTraub Nov 9, 2023
38a14c3
Delete javav2/example_code/bedrock/BedrockJ2Project.iml
DennisTraub Nov 11, 2023
6e32977
House-cleaning after moving the module
DennisTraub Nov 11, 2023
9f91149
Add a code sample for AI21 Labs Jurassic-2
DennisTraub Nov 12, 2023
376bac9
Add a code sample for Stability.ai Stable Diffusion
DennisTraub Nov 12, 2023
f253d0d
Update metadata
DennisTraub Nov 13, 2023
d74f463
Merge branch 'bedrock-runtime-invoke-model' of github.com:DennisTraub…
DennisTraub Nov 13, 2023
bdcbe13
Regenerate readme
DennisTraub Nov 13, 2023
5bdadac
Fix typo in bedrock-runtime_metadata.yaml
DennisTraub Nov 13, 2023
27580ae
Fix Readme.md
DennisTraub Nov 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,71 @@ bedrock-runtime_InvokeModel:
- bdr.abapv1.invokemodel_stable_diffusion
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_InvokeClaude:
title: Invoke the Anthropic Claude model on &BR; to run an inference
title_abbrev: Invoke Anthropic Claude 2 on &BR;
synopsis: invoke the anthropic claude model on &BR; to run inference.
category:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke the Anthropic Claude 2 foundation model
snippet_tags:
- bedrock-runtime.java2.invoke_claude.main
services:
bedrock-runtime: { InvokeModel }

bedrock-runtime_InvokeJurassic2:
title: Invoke the AI21 Labs Jurassic-2 model on &BR; to run an inference
title_abbrev: Invoke AI21 Labs Jurassic-2 on &BR;
synopsis: invoke the ai21 labs jurassic-2 model on &BR; to run inference.
category:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke the AI21 Labs Jurassic-2 foundation model
snippet_tags:
- bedrock-runtime.java2.invoke_jurassic2.main
services:
bedrock-runtime: { InvokeModel }

bedrock-runtime_InvokeStableDiffusion:
title: Invoke the Stability.ai Stable Diffusion XL model on &BR; to run an inference
title_abbrev: Invoke Stability.ai Stable Diffusion XL on &BR;
synopsis: invoke the stability.ai stable diffusion xl on &BR; model to run inference.
category:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke the Stability.ai Stable Diffusion XL foundation model
snippet_tags:
- bedrock-runtime.java2.invoke_stable_diffusion.main
services:
bedrock-runtime: { InvokeModel }

bedrock-runtime_InvokeModelWithResponseStream:
title: Invoke the specified &BR; model on &BR; to run an inference with a response stream
title_abbrev: Invoke a model on &BR; with a response stream
synopsis: invoke an model on &BR; to run an inference with a response stream
category:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke a model on &BR; and process the response stream
snippet_tags:
- bedrock-runtime.java2.invoke_model_with_response_stream.main
services:
bedrock-runtime: {InvokeModelWithResponseStream}
2 changes: 2 additions & 0 deletions javav2/example_code/bedrock-runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.iml
output
80 changes: 80 additions & 0 deletions javav2/example_code/bedrock-runtime/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--Generated by WRITEME on 2023-11-13 16:01:11.875851 (UTC)-->
# Amazon Bedrock Runtime code examples for the SDK for Java 2.x

## Overview

Shows how to use the AWS SDK for Java 2.x to work with Amazon Bedrock Runtime.

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

*Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon.*

## ⚠ 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 `javav2` 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.

* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L101) (`InvokeModel`)
* [Invoke Anthropic Claude 2 on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L51) (`InvokeModel`)
* [Invoke Stability.ai Stable Diffusion XL on Amazon Bedrock](src/main/java/com/example/bedrockruntime/InvokeModel.java#L151) (`InvokeModel`)
* [Invoke a model on Amazon Bedrock with a response stream](src/main/java/com/example/bedrockruntime/InvokeModelWithResponseStream.java#L49) (`InvokeModelWithResponseStream`)

## 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 `javav2` folder.



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

## Additional resources

* [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
* [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
* [SDK for Java 2.x Amazon Bedrock Runtime reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrock-runtime/package-summary.html)

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

---

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

SPDX-License-Identifier: Apache-2.0
60 changes: 60 additions & 0 deletions javav2/example_code/bedrock-runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BedrockJ2Project</groupId>
<artifactId>BedrockJ2Project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.21.17</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading
Loading