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

Adding SpringBoot Demo for Amazon Bedrock using Java #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions demos/springboot-bedrock-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
target/
!**/src/main/**/target/
!**/src/test/**/target/

.aws-sam/*

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

!.mvn/wrapper/maven-wrapper.jar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

### IntelliJ IDEA ###
.idea/*
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### VS Code ###
.vscode/*
.history/
*.vsix

### Mac OS ###
.DS_Store
86 changes: 86 additions & 0 deletions demos/springboot-bedrock-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Amazon Bedrock Demo using SpringBoot

This is a sample project, that demonstrates how to access Amazon Bedrock from a SpringBoot application that could deployed on AWS Fargate or on AWS Lambda (via native compilation step for avoid those cold start times).

## Prerequisites

To deploy this demo you need:
- Access to an [AWS account](https://aws.amazon.com/free/)
- [Apache Maven](https://maven.apache.org/)

This demo sent rest calls have signed with AWS credentials that will be mapped to a IAMRole with permissions to access Amazon Bedrock.

This sample code does not secure the Rest API in any way.

In a production environment you could also employ Amazon Cognito or other mechanisms to secure the API and dynamically attach the role to the API depending the logged user following best practices. The project could be integrated with Spring Security in order to gain such features.

## Build the application
To build the application, execute the following command in the `springboot-bedrock-demo` directory:

```bash
mvn package
```

## Access the Application

*Note: Replace `URL_PREFIX.execute-api.us-east-1.amazonaws.com` with the actual URL that has been returned during the deployment.*

### Send a prompt

To send a prompt to the currently active model, use the following endpoint:

```
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock?prompt=YOUR_PROMPT
```

Example:

```
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/bedrock?promp=Hello, how are you?
```

### Check active model

To check which model is currently active, use the following endpoint:

```
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/llm
```

### Change active model

To change the active model, use the following endpoint:

```
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/llm/set?model=NEW_MODEL
```

Example:

```
https://URL_PREFIX.execute-api.us-east-1.amazonaws.com/llm/set?model=anthropic.claude-instant-v1
```


The following models are currently available as part of this app:
- `anthropic.claude-v1`
- `anthropic.claude-instant-v1`
- `anthropic.claude-v2`

Provide the following configuration in the application.yml file

aws.bedrock.region=aws-region-you-are-using
aws.iamrole=iam-master-role-ARN

This project uses an IAM Role. The project is using STS to get credentials for that Role, remember to adapt it for your needs. In the blogpost, the app is launched via Fargate and the Task Role is used for the permission chain.


You can use Postman to interact with the API. The API docs can be seen at http://localhost:8080/swagger-ui/index.html

Live demo with URL: http://localhost:8080/bedrockui
Start to input some characters in the search box, which will open an auto-complete box of maximum 5 suggestions.
Complete the search text and click search button to see the search results.




147 changes: 147 additions & 0 deletions demos/springboot-bedrock-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>springboot-bedrock</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<compiler-plugin.version>3.11.0</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipITs>true</skipITs>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.21.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- lombok annotation processing -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>


<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>2.20.162</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrock</artifactId>
<version>2.20.162</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.acme;

/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootApplication
public class SpringBootBedrockApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootBedrockApplication.class, args);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
//enable CORS for all domains, remember to adapt this on production scenarios
registry.addMapping("/**").allowedOrigins("*");
}
};
}

}
Loading