자바 SDK 2.x를 사용하는 아마존 베드락 예제 - AWS SDK for Java 2.x

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

자바 SDK 2.x를 사용하는 아마존 베드락 예제

다음 코드 예제는 Amazon Bedrock과 AWS SDK for Java 2.x 함께 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 호출하는 방법을 보여 주며 관련 시나리오와 교차 서비스 예시에서 컨텍스트에 맞는 작업을 볼 수 있습니다.

시나리오는 동일한 서비스 내에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예시입니다.

각 예제에는 GitHub 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 링크가 포함되어 있습니다.

주제

작업

다음 코드 예시에서는 GetFoundationModel을 사용하는 방법을 보여 줍니다.

SDKJava 2.x의 경우
참고

더 많은 내용이 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

동기식 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); } }

비동기식 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); } }
  • API자세한 내용은 참조를 참조하십시오. GetFoundationModelAWS SDK for Java 2.x API

다음 코드 예시에서는 ListFoundationModels을 사용하는 방법을 보여 줍니다.

SDK자바 2.x의 경우
참고

더 많은 내용이 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

동기식 Amazon Bedrock 클라이언트를 사용하여 사용 가능한 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); } }

비동기식 Amazon Bedrock 클라이언트를 사용하여 사용 가능한 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); } }
  • 자세한 내용은 참조를 참조하십시오. API ListFoundationModelsAWS SDK for Java 2.x API