diff --git a/demos/springboot-bedrock-demo/.gitignore b/demos/springboot-bedrock-demo/.gitignore new file mode 100644 index 0000000..7904485 --- /dev/null +++ b/demos/springboot-bedrock-demo/.gitignore @@ -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 \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/README.md b/demos/springboot-bedrock-demo/README.md new file mode 100644 index 0000000..2fc2b52 --- /dev/null +++ b/demos/springboot-bedrock-demo/README.md @@ -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. + + + + diff --git a/demos/springboot-bedrock-demo/pom.xml b/demos/springboot-bedrock-demo/pom.xml new file mode 100644 index 0000000..b63fa27 --- /dev/null +++ b/demos/springboot-bedrock-demo/pom.xml @@ -0,0 +1,147 @@ + + + 4.0.0 + org.acme + springboot-bedrock + 1.0.0-SNAPSHOT + + + 3.11.0 + 17 + UTF-8 + UTF-8 + true + + + + org.springframework.boot + spring-boot-starter-parent + 3.1.5 + + + + + + + + software.amazon.awssdk + bom + 2.21.10 + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + 2022.0.4 + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.projectlombok + lombok + provided + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.2.0 + + + + + software.amazon.awssdk + sts + + + commons-logging + commons-logging + + + + + + software.amazon.awssdk + bedrockruntime + 2.20.162 + + + software.amazon.awssdk + bedrock + 2.20.162 + + + org.json + json + 20230618 + + + org.testng + testng + RELEASE + compile + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.graalvm.buildtools + native-maven-plugin + + + maven-compiler-plugin + ${compiler-plugin.version} + + + -parameters + + + + + + + + native + + + native + + + + false + + + + diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/SpringBootBedrockApplication.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/SpringBootBedrockApplication.java new file mode 100644 index 0000000..33d8b23 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/SpringBootBedrockApplication.java @@ -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("*"); + } + }; + } + +} \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/config/AWSCredentialsConfig.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/config/AWSCredentialsConfig.java new file mode 100644 index 0000000..a064aa4 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/config/AWSCredentialsConfig.java @@ -0,0 +1,62 @@ +package org.acme.config; + +/* +* 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 lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; + +/** + * @author Angel Conde + * + */ +@Slf4j +@Configuration +public class AWSCredentialsConfig { + + @Value("${aws.iam.role}") + private String assumeRoleARN=""; + + @Value("${aws.bedrock.region}") + private String region; + + @Bean + public AwsCredentialsProvider customPermissionProvider() { + log.info("Assuming role "+ assumeRoleARN); + StsClient sts=StsClient.builder() + .region(Region.of(region)) + .build(); + StsAssumeRoleCredentialsProvider stsRoleCredentials= + StsAssumeRoleCredentialsProvider.builder() + .stsClient(sts) + .refreshRequest(() -> AssumeRoleRequest + .builder() + .roleArn(assumeRoleARN) + .roleSessionName("bootful") + .build()) + + .build(); + return stsRoleCredentials; + } +} diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/config/BedrockClientConfiguration.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/config/BedrockClientConfiguration.java new file mode 100644 index 0000000..5405e8e --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/config/BedrockClientConfiguration.java @@ -0,0 +1,61 @@ +package org.acme.config; + +/* + * 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 lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; + +/** + * @author Angel Conde + * + */ +@Configuration +@Slf4j +public class BedrockClientConfiguration +{ + + @Value("${aws.bedrock.endpoint}") + private String endpoint = ""; + + @Value("${aws.bedrock.region}") + private String region = ""; + + private AwsCredentialsProvider credentialsProvider = null; + + @Autowired + public BedrockClientConfiguration(AwsCredentialsProvider provider){ + credentialsProvider=provider; + } + + + @Bean + public BedrockRuntimeClient bedrockClient() { + if(endpoint.equals("endpoint")){ + return BedrockRuntimeClient.builder().region(Region.of(region)).build(); + }else { + return BedrockRuntimeClient.builder().region(Region.of(region)).credentialsProvider(credentialsProvider).build(); + } + } + +} diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/BedrockController.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/BedrockController.java new file mode 100644 index 0000000..595d2f3 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/BedrockController.java @@ -0,0 +1,67 @@ +package org.acme.controller; + +/* + * 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 java.util.Date; +import java.util.List; + +import lombok.extern.slf4j.Slf4j; +import org.acme.model.BedrockAPI; +import org.acme.service.BedrockAPIService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + + +/** + * @author Angel Conde + * + */ +@RestController +@RequestMapping("/") +@Slf4j +public class BedrockController { + private static String llm = "anthropic.claude-v2"; + private final BedrockAPIService bedrockService; + + + @Autowired + public BedrockController(BedrockAPIService bedrockService) { + this.bedrockService = bedrockService; + } + + @GetMapping("/llm") + public String getLlm() { + return "Current Model: " + llm; + } + + @GetMapping("/llm/set") + public String setLlm(@RequestParam(value = "model", required = true) String new_llm) { + llm = new_llm; + return "New model: " + llm; + } + + @RequestMapping(method = RequestMethod.GET, value ="/bedrock") + private String bedrockCall(@RequestParam(value = "prompt", required = true) String prompt, @RequestParam(value = "temperature", required=false, defaultValue = "0.8") String temperature){ + String res=bedrockService.queryBedrock(prompt,Float.parseFloat(temperature)); + log.info("inside CRUD controller uiCall " + new Date()); + return res; + } + + + +} \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/UIController.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/UIController.java new file mode 100644 index 0000000..a1b9af3 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/controller/UIController.java @@ -0,0 +1,65 @@ +package org.acme.controller; + +/* + * 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 java.net.InetAddress; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * @author Angel Conde + * + */ +@Controller +@Slf4j +public class UIController { + + public UIController() { + } + + @GetMapping("/bedrockui") + public String home(Model model) { + // obtain a hostname. First try to get the host name from docker container (from the "HOSTNAME" environment variable) + String hostName = System.getenv("HOSTNAME"); + + // get the os name + String os = System.getProperty("os.name"); + + // if the application is not running in a docker container, we can to obtain the hostname using the "java.net.InetAddress" class + if(hostName == null || hostName.isEmpty()) { + try { + InetAddress addr = InetAddress.getLocalHost(); + hostName = addr.getHostName(); + } catch (Exception e) { + log.error(e.getLocalizedMessage()); + hostName = "Unknow"; + } + } + model.addAttribute("data","Bootful Bedrock app: "+ hostName+" running on: "+ os); + return "bedrockui"; + + } + +} \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/model/BedrockAPI.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/model/BedrockAPI.java new file mode 100644 index 0000000..750eaf1 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/model/BedrockAPI.java @@ -0,0 +1,40 @@ +package org.acme.model; + +/* + * 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 lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author Angel Conde + * + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor + +public class BedrockAPI { + + private String prompt; + private String temperature; + private String maxTokensToSample; + +} diff --git a/demos/springboot-bedrock-demo/src/main/java/org/acme/service/BedrockAPIService.java b/demos/springboot-bedrock-demo/src/main/java/org/acme/service/BedrockAPIService.java new file mode 100644 index 0000000..0541ad9 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/java/org/acme/service/BedrockAPIService.java @@ -0,0 +1,82 @@ + package org.acme.service; + +/* + * 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 lombok.extern.slf4j.Slf4j; +import org.acme.model.BedrockAPI; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest; +import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * This service uses Amazon Bedrock Libraries from AWS Java SDK to interact with it. + * + * @author Angel Conde + */ +@Service +@Slf4j +public class BedrockAPIService { + + private static String llm = "anthropic.claude-v2"; + + private final BedrockRuntimeClient bedrockClient; + + @Autowired + public BedrockAPIService(final BedrockRuntimeClient bedrockClient) { + super(); + this.bedrockClient = bedrockClient; + } + + + public String createProductIndex(BedrockAPI product) { + return null; + } + + public void findByLastName(final String lastName) { + + } + + public String queryBedrock(String prompt, float temperature) { + JSONObject jsonBody = new JSONObject() + .put("prompt", "Human: " + prompt + " Assistant:") + .put("temperature", temperature) + .put("max_tokens_to_sample", 1024); + + SdkBytes body = SdkBytes.fromUtf8String( + jsonBody.toString() + ); + + InvokeModelRequest request = InvokeModelRequest.builder() + .modelId(llm) + .body(body) + .build(); + + InvokeModelResponse invocationResponse = bedrockClient.invokeModel(request); + + return invocationResponse.body().asUtf8String(); + + } +} \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/src/main/resources/application.yml b/demos/springboot-bedrock-demo/src/main/resources/application.yml new file mode 100644 index 0000000..67366b9 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/resources/application.yml @@ -0,0 +1,15 @@ +spring: + application: + name: "bootful bedrock" +aws: + bedrock: + region: "region" + endpoint: "endpoint" #you can customize the endpoint if needed + iam: + role: "roleARN" + +management: + endpoints: + web: + exposure: + include: health,info,prometheus \ No newline at end of file diff --git a/demos/springboot-bedrock-demo/src/main/resources/logback.xml b/demos/springboot-bedrock-demo/src/main/resources/logback.xml new file mode 100644 index 0000000..22e376a --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/resources/logback.xml @@ -0,0 +1,18 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + diff --git a/demos/springboot-bedrock-demo/src/main/resources/templates/bedrockui.html b/demos/springboot-bedrock-demo/src/main/resources/templates/bedrockui.html new file mode 100644 index 0000000..2bdb5a9 --- /dev/null +++ b/demos/springboot-bedrock-demo/src/main/resources/templates/bedrockui.html @@ -0,0 +1,64 @@ + + + + Amazon Bedrock on SpringBoot + + + + + + + + + +
+

+

Insert prompt

+ +
+ +
+ +
+
+
+
+ +
+ + + + + + + + + \ No newline at end of file