Skip to content

Commit 0f5999a

Browse files
authored
First example invoking Claude v2
Co-authored-by: Dennis Traub <traubd@amazon.de>
1 parent 4e951d1 commit 0f5999a

File tree

4 files changed

+169
-7
lines changed

4 files changed

+169
-7
lines changed

.gitignore

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
# Compiled class file
7+
*.class
8+
9+
# Log file
10+
*.log
11+
12+
# BlueJ files
13+
*.ctxt
14+
15+
# Mobile Tools for Java (J2ME)
16+
.mtj.tmp/
17+
18+
# Package Files #
19+
*.jar
20+
*.war
21+
*.nar
22+
*.ear
23+
*.zip
24+
*.tar.gz
25+
*.rar
26+
27+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
28+
hs_err_pid*
29+
replay_pid*
30+
31+
### IntelliJ IDEA ###
32+
.idea/modules.xml
33+
.idea/jarRepositories.xml
34+
.idea/compiler.xml
35+
.idea/libraries/
36+
*.iws
37+
*.iml
38+
*.ipr
39+
40+
### Eclipse ###
41+
.apt_generated
42+
.classpath
43+
.factorypath
44+
.project
45+
.settings
46+
.springBeans
47+
.sts4-cache
48+
49+
### NetBeans ###
50+
/nbproject/private/
51+
/nbbuild/
52+
/dist/
53+
/nbdist/
54+
/.nb-gradle/
55+
build/
56+
!**/src/main/**/build/
57+
!**/src/test/**/build/
58+
59+
### VS Code ###
60+
.vscode/*
61+
!.vscode/settings.json
62+
!.vscode/tasks.json
63+
!.vscode/launch.json
64+
!.vscode/extensions.json
65+
!.vscode/*.code-snippets
66+
.history/
67+
*.vsix
68+
69+
### Mac OS ###
70+
.DS_Store

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
## My Project
1+
# Amazon Bedrock Java Examples
22

3-
TODO: Fill this README out!
3+
This repository contains examples using the AWS SDK for Java to help you get started with the Amazon Bedrock service.
44

5-
Be sure to:
5+
## Contributing
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
We welcome community contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
98

109
## Security
1110

1211
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
1312

1413
## License
1514

16-
This library is licensed under the MIT-0 License. See the LICENSE file.
17-
15+
This library is licensed under the MIT-0 License. See the [LICENSE](LICENSE) file.

pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>aws.community.examples</groupId>
8+
<artifactId>Bedrock</artifactId>
9+
<version>0.1</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<groupId>software.amazon.awssdk</groupId>
20+
<artifactId>bom</artifactId>
21+
<version>2.20.157</version>
22+
<type>pom</type>
23+
<scope>import</scope>
24+
</dependency>
25+
</dependencies>
26+
</dependencyManagement>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>software.amazon.awssdk</groupId>
31+
<artifactId>bedrockruntime</artifactId>
32+
<version>2.20.157</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>software.amazon.awssdk</groupId>
36+
<artifactId>sts</artifactId>
37+
<version>2.20.157</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.json</groupId>
41+
<artifactId>json</artifactId>
42+
<version>20230618</version>
43+
</dependency>
44+
</dependencies>
45+
46+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package aws.community.examples;
2+
3+
import java.nio.charset.StandardCharsets;
4+
5+
import org.json.JSONObject;
6+
7+
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
8+
import software.amazon.awssdk.core.SdkBytes;
9+
import software.amazon.awssdk.regions.Region;
10+
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
11+
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest;
12+
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse;
13+
14+
public class InvokeBedrock {
15+
public static void main(String[] args) {
16+
BedrockRuntimeClient runtime = BedrockRuntimeClient.builder()
17+
.region(Region.US_EAST_1)
18+
.credentialsProvider(ProfileCredentialsProvider.create())
19+
.build();
20+
21+
String prompt = "Generate a list of names for a fun brand of chili sauce";
22+
23+
JSONObject jsonBody = new JSONObject()
24+
.put("prompt", "Human: " + prompt + " Assistant:")
25+
.put("temperature", 0.8)
26+
.put("max_tokens_to_sample", 1024);
27+
28+
SdkBytes body = SdkBytes.fromUtf8String(
29+
jsonBody.toString()
30+
);
31+
32+
InvokeModelRequest request = InvokeModelRequest.builder()
33+
.modelId("anthropic.claude-v1")
34+
.body(body)
35+
.build();
36+
37+
InvokeModelResponse response = runtime.invokeModel(request);
38+
39+
JSONObject jsonObject = new JSONObject(
40+
response.body().asString(StandardCharsets.UTF_8)
41+
);
42+
43+
String completion = jsonObject.getString("completion");
44+
45+
System.out.println("-------------------------------------");
46+
System.out.println(completion);
47+
}
48+
}

0 commit comments

Comments
 (0)