Invoquez les intégrations de texte Amazon Titan sur Amazon Bedrock - Amazon Bedrock

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Invoquez les intégrations de texte Amazon Titan sur Amazon Bedrock

Les exemples de code suivants montrent comment :

  • Commencez à créer votre première intégration.

  • Créez des intégrations configurant le nombre de dimensions et la normalisation (V2 uniquement).

Java
SDK pour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Créez votre première intégration avec Titan Text Embeddings V2.

// Generate and print an embedding with Amazon Titan Text Embeddings. public class TextEmbeddingsQuickstart { 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 V2. var modelId = "amazon.titan-embed-text-v2:0"; // 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); } }

Appelez Titan Text Embeddings V2 pour configurer le nombre de dimensions et la normalisation.

/** * Invoke Amazon Titan Text Embeddings V2 with additional inference parameters. * * @param inputText - The text to convert to an embedding. * @param dimensions - The number of dimensions the output embeddings should have. * Values accepted by the model: 256, 512, 1024. * @param normalize - A flag indicating whether or not to normalize the output embeddings. * @return The {@link JSONObject} representing the model's response. */ public static JSONObject invokeModel(String inputText, int dimensions, boolean normalize) { // 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 Embed Text v2.0. var modelId = "amazon.titan-embed-text-v2:0"; // Create the request for the model. var nativeRequest = """ { "inputText": "%s", "dimensions": %d, "normalize": %b } """.formatted(inputText, dimensions, normalize); // 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; }
  • Pour plus de détails sur l'API, voir InvokeModella section Référence des AWS SDK for Java 2.x API.

Python
SDK pour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Créez votre première intégration avec Amazon Titan Text Embeddings.

# Generate and print an embedding with Amazon Titan Text Embeddings V2. import boto3 import json # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Titan Text Embeddings V2. model_id = "amazon.titan-embed-text-v2:0" # The text to convert to an embedding. input_text = "Please recommend books with a theme similar to the movie 'Inception'." # Create the request for the model. native_request = {"inputText": input_text} # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) # Decode the model's native response body. model_response = json.loads(response["body"].read()) # Extract and print the generated embedding and the input text token count. embedding = model_response["embedding"] input_token_count = model_response["inputTextTokenCount"] print("\nYour input:") print(input_text) print(f"Number of input tokens: {input_token_count}") print(f"Size of the generated embedding: {len(embedding)}") print("Embedding:") print(embedding)
  • Pour plus de détails sur l'API, consultez InvokeModelle AWS manuel de référence de l'API SDK for Python (Boto3).

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de ce service avec un AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.