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

Python + Java: Amazon Titan Text Embeddings G1 and V2 #6420

Merged
merged 7 commits into from
May 7, 2024
Merged
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
67 changes: 67 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,73 @@ bedrock-runtime_Hello:
services:
bedrock-runtime: {InvokeModel}

# Amazon Titan - Embeddings
bedrock-runtime_TitanTextEmbeddingsG1_InvokeModel:
title: Invoke Amazon Titan Text Embeddings G1 on &BR;
title_abbrev: "Amazon Titan Text Embeddings G1"
synopsis_list:
- Get started creating your first embedding.
- Create embeddings and process all response fields.
category: Invoke model examples
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Create your first embedding with Amazon Titan Text Embeddings G1.
snippet_tags:
- bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_Quickstart
- description: Invoke Amazon Titan Text Embeddings G1 and process all response fields.
snippet_tags:
- bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_AdditionalFields
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Create your first embedding with Amazon Titan Text Embeddings G1.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModel_TitanTextEmbeddingsG1_Quickstart
- description: Invoke Amazon Titan Text Embeddings G1 and process all response fields.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModel_TitanTextEmbeddingsG1_AdditionalFields
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_TitanTextEmbeddingsV2_InvokeModel:
title: Invoke Amazon Titan Text Embeddings V2 on &BR;
title_abbrev: "Amazon Titan Text Embeddings V2"
synopsis_list:
- Get started creating your first embedding.
- Create embeddings configuring the number of dimensions and normalization.
category: Invoke model examples
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Create your first embedding with Titan Text Embeddings V2.
snippet_tags:
- bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsV2_Quickstart
- description: Invoke Titan Text Embeddings V2 configuring the number of dimensions and normalization.
snippet_tags:
- bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsV2_AdditionalFields
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Create your first embedding with Titan Text Embeddings V2.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModel_TitanTextEmbeddingsG1_Quickstart
- description: Invoke Titan Text Embeddings V2 configuring the number of dimensions and normalization.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModel_TitanTextEmbeddingsG1_AdditionalFields
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_InvokeAmazonTitanImageGeneratorForImageGeneration:
title: Invoke Amazon Titan Image G1 on &BR; to generate an image
title_abbrev: 'Amazon Titan: Image generation'
Expand Down
10 changes: 6 additions & 4 deletions javav2/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ functions within the same service.

### Invoke model examples

- [AI21 Labs Jurassic-2: Text generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L205)
- [Amazon Titan: Image generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L338)
- [AI21 Labs Jurassic-2: Text generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L208)
- [Amazon Titan Text Embeddings G1](src/main/java/com/example/bedrockruntime/models/amazon/embeddings/text/G1InvokeModelQuickstart.java#L11)
- [Amazon Titan Text Embeddings V2](src/main/java/com/example/bedrockruntime/models/amazon/embeddings/text/V2InvokeModelQuickstart.java#L11)
- [Amazon Titan: Image generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L343)
- [Anthropic Claude 2: Real-time response stream processing](src/main/java/com/example/bedrockruntime/Claude2.java#L65)
- [Anthropic Claude 2: Text generation](src/main/java/com/example/bedrockruntime/InvokeModel.java#L112)
- [Anthropic Claude 3: Real-time response stream processing](src/main/java/com/example/bedrockruntime/Claude3.java#L49)
Expand All @@ -50,8 +52,8 @@ functions within the same service.
- [Meta Llama 3: Text generation](src/main/java/com/example/bedrockruntime/models/meta/llama3/InvokeModelQuickstart.java#L13)
- [Meta Llama 3: Text generation with response stream](src/main/java/com/example/bedrockruntime/models/meta/llama3/InvokeModelWithResponseStreamQuickstart.java#L14)
- [Mistral AI: Text generation with Mistral 7B Instruct](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L33)
- [Mistral AI: Text generation with Mixtral 8x7B Instruct](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L88)
- [Stable Diffusion: Image generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L268)
- [Mistral AI: Text generation with Mixtral 8x7B Instruct](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L89)
- [Stable Diffusion: Image generation](src/main/java/com/example/bedrockruntime/InvokeModelAsync.java#L272)


<!--custom.examples.start-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* <p>
* For more information, see the following documentation topic:
*
* <p>
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class InvokeModelAsync {

// snippet-start:[bedrock-runtime.java2.invoke_mistral_7b_async.main]

/**
* Asynchronously invokes the Mistral 7B model to run an inference based on the provided input.
*
Expand All @@ -56,15 +57,15 @@ public static List<String> invokeMistral7B(String prompt) {
.toString();

CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request -> request
.accept("application/json")
.contentType("application/json")
.body(SdkBytes.fromUtf8String(payload))
.modelId(modelId))
.whenComplete((response, exception) -> {
if (exception != null) {
System.out.println("Model invocation failed: " + exception);
}
});
.accept("application/json")
.contentType("application/json")
.body(SdkBytes.fromUtf8String(payload))
.modelId(modelId))
.whenComplete((response, exception) -> {
if (exception != null) {
System.out.println("Model invocation failed: " + exception);
}
});

try {
InvokeModelResponse response = completableFuture.get();
Expand All @@ -86,6 +87,7 @@ public static List<String> invokeMistral7B(String prompt) {
// snippet-end:[bedrock-runtime.java2.invoke_mistral_7b_async.main]

// snippet-start:[bedrock-runtime.java2.invoke_mixtral_8x7b_async.main]

/**
* Asynchronously invokes the Mixtral 8x7B model to run an inference based on the provided input.
*
Expand Down Expand Up @@ -141,6 +143,7 @@ public static List<String> invokeMixtral8x7B(String prompt) {
// snippet-end:[bedrock-runtime.java2.invoke_mixtral_8x7b_async.main]

// snippet-start:[bedrock-runtime.java2.invoke_claude_async.main]

/**
* Asynchronously invokes the Anthropic Claude 2 model to run an inference based
* on the provided input.
Expand Down Expand Up @@ -203,6 +206,7 @@ public static String invokeClaude(String prompt) {
// snippet-end:[bedrock-runtime.java2.invoke_claude_async.main]

// snippet-start:[bedrock-runtime.java2.invoke_jurassic-2_async.main]

/**
* Asynchronously invokes the AI21 Labs Jurassic-2 model to run an inference
* based on the provided input.
Expand Down Expand Up @@ -266,6 +270,7 @@ public static String invokeJurassic2(String prompt) {
// snippet-end:[bedrock-runtime.java2.invoke_jurassic-2_async.main]

// snippet-start:[bedrock-runtime.java2.invoke_stable_diffusion_async.main]

/**
* Asynchronously invokes the Stability.ai Stable Diffusion XL model to create
* an image based on the provided input.
Expand All @@ -285,7 +290,7 @@ public static String invokeStableDiffusion(String prompt, long seed, String styl
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html
*/

String stableDiffusionModelId = "stability.stable-diffusion-xl";
String stableDiffusionModelId = "stability.stable-diffusion-xl-v1";

BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder()
.region(Region.US_EAST_1)
Expand Down Expand Up @@ -336,6 +341,7 @@ public static String invokeStableDiffusion(String prompt, long seed, String styl
// snippet-end:[bedrock-runtime.java2.invoke_stable_diffusion_async.main]

// snippet-start:[bedrock-runtime.java2.invoke_titan_image_async.main]

/**
* Invokes the Amazon Titan image generation model to create an image using the
* input
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.bedrockruntime.libs;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ScenarioRunner {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private final List<String> scenarios = new ArrayList<>();

public void printHeader() {
System.out.println("*".repeat(80));
System.out.println("*" + " ".repeat(78) + "*");
System.out.println("* Welcome to the Amazon Bedrock demo!" + " ".repeat(41) + "*");
System.out.println("* " + "-".repeat(74) + " *");
System.out.println("*" + " ".repeat(78) + "*");
System.out.println("* This demo showcases the following scenarios:" + " ".repeat(32) + "*");

int scenarioNumber = 1;
scenarios.forEach(scenario -> {
System.out.println("*" + " ".repeat(78) + "*");
System.out.println("* " + scenarioNumber + ". " + scenario + "." + " ".repeat(20) + "*");
});

System.out.println("*" + " ".repeat(78) + "*");
System.out.println("*".repeat(80));
}

public void printScenarioHeader(String title) {
System.out.println("=".repeat(80));
System.out.println(title);
System.out.println("=".repeat(80));
}

public void printCurrentResponse(JSONObject response) throws IOException {
printCurrentResponse(response.toString(2));
}

public void printCurrentResponse(String text) throws IOException {
System.out.println("-".repeat(80));
promptUser("Press Enter to see the detailed response...");
System.out.println(text);
System.out.println("-".repeat(80));
}

public void printFooter() {
System.out.println("\n" + "*".repeat(80));
System.out.println("Thanks for running the Amazon Bedrock demo!");
System.out.println("=".repeat(80));
System.out.println("For more examples across different programming languages check out:");
System.out.println("https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html");
System.out.println("*".repeat(80));
}

public void promptUser(String text) throws IOException {
System.out.printf("\n%s%n", text);
this.reader.readLine();
}

public ScenarioRunner addScenario(String title) {
this.scenarios.add(title);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.bedrockruntime.models.amazon.embeddings.text;

import org.json.JSONObject;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;

// snippet-start:[bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_Quickstart]
// Generate and print an embedding with Amazon Titan Text Embeddings G1.
public class G1InvokeModelQuickstart {

public static void main(String[] args) {

// Create a Bedrock Runtime client in the AWS Region of your choice.
var client = BedrockRuntimeClient.builder()
.region(Region.US_WEST_2)
.build();

// Set the model ID, e.g., Titan Text Embeddings G1.
var modelId = "amazon.titan-embed-text-v1";

// The text to convert into an embedding.
var inputText = "Please recommend books with a theme similar to the movie 'Inception'.";

// Create a JSON payload using the model's native structure.
var request = new JSONObject().put("inputText", inputText);

// Encode and send the request.
var response = client.invokeModel(req -> req
.body(SdkBytes.fromUtf8String(request.toString()))
.modelId(modelId));

// Decode the model's native response body.
var nativeResponse = new JSONObject(response.body().asUtf8String());

// Extract and print the generated embedding.
var embedding = nativeResponse.getJSONArray("embedding");
System.out.println(embedding);
}
}
// snippet-end:[bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_Quickstart]
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.bedrockruntime.models.amazon.embeddings.text;

import com.example.bedrockruntime.libs.ScenarioRunner;
import org.json.JSONObject;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;

import java.io.IOException;

/**
* This program demonstrates how to use InvokeModel with Amazon Titan Text Embeddings G1 on Amazon Bedrock.
* <p>
* For more examples in different programming languages check out the Amazon Bedrock User Guide at:
* https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html
*/

public class G1InvokeModelScenarios {
// snippet-start:[bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_AdditionalFields]

/**
* Invoke Amazon Titan Text Embeddings G1 and print the response.
*
* @param inputText - The text to convert to an embedding.
* @return The {@link JSONObject} representing the model's response.
*/
public static JSONObject invokeModel(String inputText) {

// Create a Bedrock Runtime client in the AWS Region of your choice.
var client = BedrockRuntimeClient.builder()
.region(Region.US_WEST_2)
.build();

// Set the model ID, e.g., Titan Text Embeddings G1.
var modelId = "amazon.titan-embed-text-v1";

// Format the request payload using Anthropic's native structure.
var nativeRequest = "{\"inputText\": \"" + inputText + "\"}";

// Encode and send the request.
var response = client.invokeModel(request -> {
request.body(SdkBytes.fromUtf8String(nativeRequest));
request.modelId(modelId);
});

// Decode the model's response.
var modelResponse = new JSONObject(response.body().asUtf8String());

// Extract and print the generated embedding and the input text token count.
var embedding = modelResponse.getJSONArray("embedding");
var inputTokenCount = modelResponse.getBigInteger("inputTextTokenCount");
System.out.println("Embedding: " + embedding);
System.out.println("\nInput token count: " + inputTokenCount);

// Return the model's native response.
return modelResponse;
}
// snippet-end:[bedrock-runtime.java2.InvokeModel_TitanTextEmbeddingsG1_AdditionalFields]

public static void main(String[] args) throws IOException {
new Demo().run();
}

private static class Demo {
private final ScenarioRunner demo = new ScenarioRunner()
.addScenario("Create an embedding with custom inference parameters");

void run() throws IOException {
demo.printHeader();

var inputText = "Please recommend books with a theme similar to the movie 'Inception'.";
var response = runTextScenario(inputText);
demo.printCurrentResponse(response);

demo.printFooter();
}

private JSONObject runTextScenario(String inputText) {
demo.printScenarioHeader("Scenario - Create an embedding with custom inference parameters:");
System.out.printf("%nInput text: '%s'%n", inputText);
System.out.printf("Waiting for the response...%n");
return invokeModel(inputText);
}
}
}
Loading
Loading