Exemples d'utilisation d'Amazon Bedrock SDK pour Java 2.x - AWS SDK for Java 2.x

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.

Exemples d'utilisation d'Amazon Bedrock SDK pour Java 2.x

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l' AWS SDK for Java 2.x aide d'Amazon Bedrock.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous indiquent comment appeler des fonctions de service individuelles, vous pouvez les voir en contexte dans leurs scénarios associés et dans des exemples interservices.

Les Scénarios sont des exemples de code qui vous montrent comment accomplir une tâche spécifique en appelant plusieurs fonctions au sein d’un même service.

Chaque exemple inclut un lien vers GitHub, où vous pouvez trouver des instructions sur la façon de configurer et d'exécuter le code en contexte.

Rubriques

Actions

L'exemple de code suivant montre comment utiliserGetFoundationModel.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet 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.

Obtenez des informations sur un modèle de base à l'aide du client synchrone Amazon Bedrock.

/** * Get details about an Amazon Bedrock foundation model. * * @param bedrockClient The service client for accessing Amazon Bedrock. * @param modelIdentifier The model identifier. * @return An object containing the foundation model's details. */ public static FoundationModelDetails getFoundationModel(BedrockClient bedrockClient, String modelIdentifier) { try { GetFoundationModelResponse response = bedrockClient.getFoundationModel( r -> r.modelIdentifier(modelIdentifier) ); FoundationModelDetails model = response.modelDetails(); System.out.println(" Model ID: " + model.modelId()); System.out.println(" Model ARN: " + model.modelArn()); System.out.println(" Model Name: " + model.modelName()); System.out.println(" Provider Name: " + model.providerName()); System.out.println(" Lifecycle status: " + model.modelLifecycle().statusAsString()); System.out.println(" Input modalities: " + model.inputModalities()); System.out.println(" Output modalities: " + model.outputModalities()); System.out.println(" Supported customizations: " + model.customizationsSupported()); System.out.println(" Supported inference types: " + model.inferenceTypesSupported()); System.out.println(" Response streaming supported: " + model.responseStreamingSupported()); return model; } catch (ValidationException e) { throw new IllegalArgumentException(e.getMessage()); } catch (SdkException e) { System.err.println(e.getMessage()); throw new RuntimeException(e); } }

Obtenez des informations sur un modèle de base à l'aide du client asynchrone Amazon Bedrock.

/** * Get details about an Amazon Bedrock foundation model. * * @param bedrockClient The async service client for accessing Amazon Bedrock. * @param modelIdentifier The model identifier. * @return An object containing the foundation model's details. */ public static FoundationModelDetails getFoundationModel(BedrockAsyncClient bedrockClient, String modelIdentifier) { try { CompletableFuture<GetFoundationModelResponse> future = bedrockClient.getFoundationModel( r -> r.modelIdentifier(modelIdentifier) ); FoundationModelDetails model = future.get().modelDetails(); System.out.println(" Model ID: " + model.modelId()); System.out.println(" Model ARN: " + model.modelArn()); System.out.println(" Model Name: " + model.modelName()); System.out.println(" Provider Name: " + model.providerName()); System.out.println(" Lifecycle status: " + model.modelLifecycle().statusAsString()); System.out.println(" Input modalities: " + model.inputModalities()); System.out.println(" Output modalities: " + model.outputModalities()); System.out.println(" Supported customizations: " + model.customizationsSupported()); System.out.println(" Supported inference types: " + model.inferenceTypesSupported()); System.out.println(" Response streaming supported: " + model.responseStreamingSupported()); return model; } catch (ExecutionException e) { if (e.getMessage().contains("ValidationException")) { throw new IllegalArgumentException(e.getMessage()); } else { System.err.println(e.getMessage()); throw new RuntimeException(e); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); throw new RuntimeException(e); } }
  • Pour API plus de détails, voir GetFoundationModella section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserListFoundationModels.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet 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.

Répertoriez les modèles de fondation Amazon Bedrock disponibles à l'aide du client synchrone Amazon Bedrock.

/** * Lists Amazon Bedrock foundation models that you can use. * You can filter the results with the request parameters. * * @param bedrockClient The service client for accessing Amazon Bedrock. * @return A list of objects containing the foundation models' details */ public static List<FoundationModelSummary> listFoundationModels(BedrockClient bedrockClient) { try { ListFoundationModelsResponse response = bedrockClient.listFoundationModels(r -> {}); List<FoundationModelSummary> models = response.modelSummaries(); if (models.isEmpty()) { System.out.println("No available foundation models in " + region.toString()); } else { for (FoundationModelSummary model : models) { System.out.println("Model ID: " + model.modelId()); System.out.println("Provider: " + model.providerName()); System.out.println("Name: " + model.modelName()); System.out.println(); } } return models; } catch (SdkClientException e) { System.err.println(e.getMessage()); throw new RuntimeException(e); } }

Répertoriez les modèles de fondation Amazon Bedrock disponibles à l'aide du client asynchrone Amazon Bedrock.

/** * Lists Amazon Bedrock foundation models that you can use. * You can filter the results with the request parameters. * * @param bedrockClient The async service client for accessing Amazon Bedrock. * @return A list of objects containing the foundation models' details */ public static List<FoundationModelSummary> listFoundationModels(BedrockAsyncClient bedrockClient) { try { CompletableFuture<ListFoundationModelsResponse> future = bedrockClient.listFoundationModels(r -> {}); List<FoundationModelSummary> models = future.get().modelSummaries(); if (models.isEmpty()) { System.out.println("No available foundation models in " + region.toString()); } else { for (FoundationModelSummary model : models) { System.out.println("Model ID: " + model.modelId()); System.out.println("Provider: " + model.providerName()); System.out.println("Name: " + model.modelName()); System.out.println(); } } return models; } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); throw new RuntimeException(e); } catch (ExecutionException e) { System.err.println(e.getMessage()); throw new RuntimeException(e); } }
  • Pour API plus de détails, voir ListFoundationModelsla section AWS SDK for Java 2.x APIRéférence.