베드록의 컨버스를 사용하여 Amazon Bedrock에서 앤트로픽 클로드 호출하기 API - Amazon Bedrock

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

베드록의 컨버스를 사용하여 Amazon Bedrock에서 앤트로픽 클로드 호출하기 API

다음 코드 예제는 베드록의 컨버스를 사용하여 Anthropic Claude에게 문자 메시지를 보내는 방법을 보여줍니다. API

.NET
AWS SDK for .NET
참고

더 많은 정보가 있습니다 GitHub. 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

// Use the Converse API to send a text message to Anthropic Claude. using System; using System.Collections.Generic; using Amazon; using Amazon.BedrockRuntime; using Amazon.BedrockRuntime.Model; // Create a Bedrock Runtime client in the AWS Region you want to use. var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); // Set the model ID, e.g., Claude 3 Haiku. var modelId = "anthropic.claude-3-haiku-20240307-v1:0"; // Define the user message. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Create a request with the model ID, the user message, and an inference configuration. var request = new ConverseRequest { ModelId = modelId, Messages = new List<Message> { new Message { Role = ConversationRole.User, Content = new List<ContentBlock> { new ContentBlock { Text = userMessage } } } }, InferenceConfig = new InferenceConfiguration() { MaxTokens = 512, Temperature = 0.5F, TopP = 0.9F } }; try { // Send the request to the Bedrock Runtime and wait for the result. var response = await client.ConverseAsync(request); // Extract and print the response text. string responseText = response?.Output?.Message?.Content?[0]?.Text ?? ""; Console.WriteLine(responseText); } catch (AmazonBedrockRuntimeException e) { Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); throw; }
Java
SDK자바 2.x의 경우
참고

더 많은 내용이 있습니다. GitHub 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

// Use the Converse API to send a text message to Anthropic Claude. import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; import software.amazon.awssdk.services.bedrockruntime.model.Message; public class Converse { public static String converse() { // Create a Bedrock Runtime client in the AWS Region you want to use. // Replace the DefaultCredentialsProvider with your preferred credentials provider. var client = BedrockRuntimeClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()) .region(Region.US_EAST_1) .build(); // Set the model ID, e.g., Claude 3 Haiku. var modelId = "anthropic.claude-3-haiku-20240307-v1:0"; // Create the input text and embed it in a message object with the user role. var inputText = "Describe the purpose of a 'hello world' program in one line."; var message = Message.builder() .content(ContentBlock.fromText(inputText)) .role(ConversationRole.USER) .build(); try { // Send the message with a basic inference configuration. ConverseResponse response = client.converse(request -> request .modelId(modelId) .messages(message) .inferenceConfig(config -> config .maxTokens(512) .temperature(0.5F) .topP(0.9F))); // Retrieve the generated text from Bedrock's response object. var responseText = response.output().message().content().get(0).text(); System.out.println(responseText); return responseText; } catch (SdkClientException e) { System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage()); throw new RuntimeException(e); } } public static void main(String[] args) { converse(); } }

비동기 자바 클라이언트에서 베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

// Use the Converse API to send a text message to Anthropic Claude // with the async Java client. import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; import software.amazon.awssdk.services.bedrockruntime.model.Message; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class ConverseAsync { public static String converseAsync() { // Create a Bedrock Runtime client in the AWS Region you want to use. // Replace the DefaultCredentialsProvider with your preferred credentials provider. var client = BedrockRuntimeAsyncClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()) .region(Region.US_EAST_1) .build(); // Set the model ID, e.g., Claude 3 Haiku. var modelId = "anthropic.claude-3-haiku-20240307-v1:0"; // Create the input text and embed it in a message object with the user role. var inputText = "Describe the purpose of a 'hello world' program in one line."; var message = Message.builder() .content(ContentBlock.fromText(inputText)) .role(ConversationRole.USER) .build(); // Send the message with a basic inference configuration. var request = client.converse(params -> params .modelId(modelId) .messages(message) .inferenceConfig(config -> config .maxTokens(512) .temperature(0.5F) .topP(0.9F)) ); // Prepare a future object to handle the asynchronous response. CompletableFuture<String> future = new CompletableFuture<>(); // Handle the response or error using the future object. request.whenComplete((response, error) -> { if (error == null) { // Extract the generated text from Bedrock's response object. String responseText = response.output().message().content().get(0).text(); future.complete(responseText); } else { future.completeExceptionally(error); } }); try { // Wait for the future object to complete and retrieve the generated text. String responseText = future.get(); System.out.println(responseText); return responseText; } catch (ExecutionException | InterruptedException e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getMessage()); throw new RuntimeException(e); } } public static void main(String[] args) { converseAsync(); } }
JavaScript
SDK JavaScript (v3) 에 대한
참고

더 많은 정보가 있습니다. GitHub 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

// Use the Conversation API to send a text message to Anthropic Claude. import { BedrockRuntimeClient, ConverseCommand, } from "@aws-sdk/client-bedrock-runtime"; // Create a Bedrock Runtime client in the AWS Region you want to use. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Set the model ID, e.g., Claude 3 Haiku. const modelId = "anthropic.claude-3-haiku-20240307-v1:0"; // Start a conversation with the user message. const userMessage = "Describe the purpose of a 'hello world' program in one line."; const conversation = [ { role: "user", content: [{ text: userMessage }], }, ]; // Create a command with the model ID, the message, and a basic configuration. const command = new ConverseCommand({ modelId, messages: conversation, inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 }, }); try { // Send the command to the model and wait for the response const response = await client.send(command); // Extract and print the response text. const responseText = response.output.message.content[0].text; console.log(responseText); } catch (err) { console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); process.exit(1); }
Python
SDK파이썬용 (보토3)
참고

더 많은 정보가 있습니다. GitHub 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

# Use the Conversation API to send a text message to Anthropic Claude. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Claude 3 Haiku. model_id = "anthropic.claude-3-haiku-20240307-v1:0" # Start a conversation with the user message. user_message = "Describe the purpose of a 'hello world' program in one line." conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = client.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] print(response_text) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
Rust
SDK러스트용
참고

더 많은 정보가 있습니다 GitHub. 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

베드록의 컨버스를 사용하여 앤트로픽 클로드에게 문자 메시지를 보내세요. API

#[tokio::main] async fn main() -> Result<(), BedrockConverseError> { tracing_subscriber::fmt::init(); let sdk_config = aws_config::defaults(BehaviorVersion::latest()) .region(CLAUDE_REGION) .load() .await; let client = Client::new(&sdk_config); let response = client .converse() .model_id(MODEL_ID) .messages( Message::builder() .role(ConversationRole::User) .content(ContentBlock::Text(USER_MESSAGE.to_string())) .build() .map_err(|_| "failed to build message")?, ) .send() .await; match response { Ok(output) => { let text = get_converse_output_text(output)?; println!("{}", text); Ok(()) } Err(e) => Err(e .as_service_error() .map(BedrockConverseError::from) .unwrap_or_else(|| BedrockConverseError("Unknown service error".into()))), } } fn get_converse_output_text(output: ConverseOutput) -> Result<String, BedrockConverseError> { let text = output .output() .ok_or("no output")? .as_message() .map_err(|_| "output not a message")? .content() .first() .ok_or("no content in message")? .as_text() .map_err(|_| "content is not text")? .to_string(); Ok(text) }

명령문, 오류 유틸리티, 상수를 사용하세요.

use aws_config::BehaviorVersion; use aws_sdk_bedrockruntime::{ operation::converse::{ConverseError, ConverseOutput}, types::{ContentBlock, ConversationRole, Message}, Client, }; // Set the model ID, e.g., Claude 3 Haiku. const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0"; const CLAUDE_REGION: &str = "us-east-1"; // Start a conversation with the user message. const USER_MESSAGE: &str = "Describe the purpose of a 'hello world' program in one line."; #[derive(Debug)] struct BedrockConverseError(String); impl std::fmt::Display for BedrockConverseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Can't invoke '{}'. Reason: {}", MODEL_ID, self.0) } } impl std::error::Error for BedrockConverseError {} impl From<&str> for BedrockConverseError { fn from(value: &str) -> Self { BedrockConverseError(value.to_string()) } } impl From<&ConverseError> for BedrockConverseError { fn from(value: &ConverseError) -> Self { BedrockConverseError::from(match value { ConverseError::ModelTimeoutException(_) => "Model took too long", ConverseError::ModelNotReadyException(_) => "Model is not ready", _ => "Unknown", }) } }
  • API자세한 내용은 Converse in을 참조하십시오. AWS SDKRust API 레퍼런스를 위해.

전체 목록은 다음과 같습니다. AWS SDK개발자 가이드 및 코드 예제는 을 참조하십시오이 서비스를 다음과 함께 사용 AWS SDK. 이 항목에는 시작에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.