-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ImageToTextAsyncTest.java (#6271)
Unit Test to test the Image to Text functionality in InvokeModelWithResponseStream:invokeClaude3Sonnet method.
- Loading branch information
1 parent
6aadb9b
commit 2bdeeff
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
javav2/example_code/bedrock-runtime/src/test/java/ImageToTextAsyncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import com.example.bedrockruntime.InvokeModelWithResponseStream; | ||
|
||
|
||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Base64; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_METHOD) | ||
class ImageToTextAsyncTest extends TestBase { | ||
|
||
@Test | ||
@Tag("IntegrationTest") | ||
void InvokeClaude3WithResponseStream() { | ||
var silent = false; | ||
String sampleImageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/PD-US_table_updated.svg/431px-PD-US_table_updated.svg.png"; | ||
var generatedText = InvokeModelWithResponseStream.invokeClaude3Sonnet(encodeImage(sampleImageURL), silent); | ||
; | ||
assertNotNullOrEmpty(generatedText); | ||
System.out.println("Test async invoke Claude with response stream passed."); | ||
} | ||
public static String encodeImage(String imageUrl) { | ||
byte[] imageBytes = new byte[8]; | ||
try (var imageResponse = new URL(imageUrl).openStream()) { | ||
imageBytes = org.apache.commons.io.IOUtils.toByteArray(imageResponse); // Use Apache Commons IO for efficient handling | ||
} catch (IOException ioException) { | ||
System.err.println("Error: " + ioException.getMessage()); | ||
} | ||
// Base64 encode the image bytes | ||
return Base64.getEncoder().encodeToString(imageBytes); | ||
} | ||
} |