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

feat: add converse nova api multimodel by java #490

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
17 changes: 17 additions & 0 deletions multi-modal/Nova/java/nova-converse/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Converse Sample

## environment
* java 17
* maven
aws aws_secret_access_key, aws_access_key_id and region in ~/.aws/credentials or environment variables

## complie
* install maven and java
```bash
mvn clean package
```

## run`
```bash
java -jar target/demo-1.0-SNAPSHOT.jar
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<aws.sdk.version>2.30.21</aws.sdk.version>
</properties>
</project>
55 changes: 55 additions & 0 deletions multi-modal/Nova/java/nova-converse/demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<aws.sdk.version>2.30.21</aws.sdk.version>
</properties>

<dependencies>
<!-- AWS SDK -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example;

import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.*;

import java.util.Arrays;

public class Main {

public static String converse(String s3Uri) throws Exception {
var client = BedrockRuntimeClient.builder()
.credentialsProvider(DefaultCredentialsProvider.create())
.region(Region.US_EAST_1)
.build();

var modelId = "amazon.nova-lite-v1:0";

// Create video content block
var videoContent = ContentBlock.builder()
.video(video -> video
.format("mp4")
.source(source -> source
.s3Location(s3 -> s3
.uri(s3Uri)
)))
.build();

// Create text content block
var prompt = "What is the main idea of the video?";
var textContent = ContentBlock.fromText(prompt);

// Create message with both video and text content
var message = Message.builder()
.content(Arrays.asList(videoContent, textContent))
.role(ConversationRole.USER)
.build();

try {
ConverseResponse response = client.converse(request -> request
.modelId(modelId)
.messages(message)
.inferenceConfig(config -> config
.maxTokens(4096)
.temperature(0.5F)
.topP(0.9F)));

var responseText = response.output().message().content().get(0).text();
System.out.println(responseText);
return responseText;

} catch (SdkClientException e) {
System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage());
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
String s3Uri = "s3://bedrock-video-generation-us-east-1-pi8hu9/video-class/cb4198e065f64149b7ccdf7f9b78f1b9.mp4";
//String bucketOwner = "767828766472";
try {
converse(s3Uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}