Amazon Bedrock-Beispiele für die Verwendung von Java SDK 2.x - AWS SDK for Java 2.x

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Amazon Bedrock-Beispiele für die Verwendung von Java SDK 2.x

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von Amazon Bedrock Aktionen ausführen und allgemeine Szenarien implementieren. AWS SDK for Java 2.x

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Während Aktionen Ihnen zeigen, wie Sie einzelne Servicefunktionen aufrufen, können Sie Aktionen im Kontext der zugehörigen Szenarien und serviceübergreifenden Beispiele sehen.

Szenarien sind Codebeispiele, die Ihnen zeigen, wie Sie eine bestimmte Aufgabe ausführen können, indem Sie mehrere Funktionen innerhalb desselben Services aufrufen.

Jedes Beispiel enthält einen Link zu GitHub, wo Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.

Themen

Aktionen

Das folgende Codebeispiel zeigt, wie Sie es verwendenGetFoundationModel.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Erfahren Sie mehr über ein Foundation-Modell mithilfe des synchronen Amazon Bedrock-Clients.

/** * 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); } }

Erfahren Sie mehr über ein Foundation-Modell mithilfe des asynchronen Amazon Bedrock-Clients.

/** * 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); } }

Das folgende Codebeispiel zeigt die VerwendungListFoundationModels.

SDKfür Java 2.x
Anmerkung

Es gibt noch mehr dazu. GitHub Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listet die verfügbaren Amazon Bedrock Foundation-Modelle mit dem synchronen Amazon Bedrock-Client auf.

/** * 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); } }

Listet die verfügbaren Amazon Bedrock Foundation-Modelle auf, die den asynchronen Amazon Bedrock-Client verwenden.

/** * 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); } }