응답 스트림과 API 함께 Bedrock의 Converse를 사용하여 Amazon Bedrock에서 Anthropic Claude 호출 - Amazon Bedrock

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

응답 스트림과 API 함께 Bedrock의 Converse를 사용하여 Amazon Bedrock에서 Anthropic Claude 호출

다음 코드 예제에서는 Bedrock의 Converse를 사용하여 Anthropic Claude에 텍스트 메시지를 보내고 응답 스트림을 실시간으로 API 처리하는 방법을 보여줍니다.

.NET
AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Bedrock의 Converse를 사용하여 Anthropic Claude에 문자 메시지를 보내고 응답 스트림을 실시간으로 API 처리합니다.

// Use the Converse API to send a text message to Anthropic Claude // and print the response stream. using System; using System.Collections.Generic; using System.Linq; 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 ConverseStreamRequest { 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.ConverseStreamAsync(request); // Extract and print the streamed response text in real-time. foreach (var chunk in response.Stream.AsEnumerable()) { if (chunk is ContentBlockDeltaEvent) { Console.Write((chunk as ContentBlockDeltaEvent).Delta.Text); } } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); throw; }
  • 자세한 API 내용은 참조ConverseStream의 섹션을 참조하세요. AWS SDK for .NET API

Java
SDK Java 2.x용
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Bedrock의 Converse를 사용하여 Anthropic Claude에 문자 메시지를 보내고 응답 스트림을 실시간으로 API 처리합니다.

// Use the Converse API to send a text message to Anthropic Claude // and print the response stream. 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.ConverseStreamResponseHandler; import software.amazon.awssdk.services.bedrockruntime.model.Message; import java.util.concurrent.ExecutionException; public class ConverseStream { public static void main(String[] args) { // 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(); // Create a handler to extract and print the response text in real-time. var responseStreamHandler = ConverseStreamResponseHandler.builder() .subscriber(ConverseStreamResponseHandler.Visitor.builder() .onContentBlockDelta(chunk -> { String responseText = chunk.delta().text(); System.out.print(responseText); }).build() ).onError(err -> System.err.printf("Can't invoke '%s': %s", modelId, err.getMessage()) ).build(); try { // Send the message with a basic inference configuration and attach the handler. client.converseStream(request -> request.modelId(modelId) .messages(message) .inferenceConfig(config -> config .maxTokens(512) .temperature(0.5F) .topP(0.9F) ), responseStreamHandler).get(); } catch (ExecutionException | InterruptedException e) { System.err.printf("Can't invoke '%s': %s", modelId, e.getCause().getMessage()); } } }
  • 자세한 API 내용은 참조ConverseStream의 섹션을 참조하세요. AWS SDK for Java 2.x API

JavaScript
SDK 용 JavaScript (v3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Bedrock의 Converse를 사용하여 Anthropic Claude에 문자 메시지를 보내고 응답 스트림을 실시간으로 API 처리합니다.

// Use the Conversation API to send a text message to Anthropic Claude. import { BedrockRuntimeClient, ConverseStreamCommand, } 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 ConverseStreamCommand({ 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 streamed response text in real-time. for await (const item of response.stream) { if (item.contentBlockDelta) { process.stdout.write(item.contentBlockDelta.delta?.text); } } } catch (err) { console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); process.exit(1); }
  • 자세한 API 내용은 참조ConverseStream의 섹션을 참조하세요. AWS SDK for JavaScript API

Python
SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Bedrock의 Converse를 사용하여 Anthropic Claude에 문자 메시지를 보내고 응답 스트림을 실시간으로 API 처리합니다.

# Use the Conversation API to send a text message to Anthropic Claude # and print the response stream. 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. streaming_response = client.converse_stream( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the streamed response text in real-time. for chunk in streaming_response["stream"]: if "contentBlockDelta" in chunk: text = chunk["contentBlockDelta"]["delta"]["text"] print(text, end="") except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • API 자세한 내용은 ConverseStreamAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

Rust
SDK Rust용
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Anthropic Claude에 문자 메시지를 보내고 Bedrock의 를 사용하여 회신 토큰을 스트리밍합니다 ConverseStream API.

#[tokio::main] async fn main() -> Result<(), BedrockConverseStreamError> { 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_stream() .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; let mut stream = match response { Ok(output) => Ok(output.stream), Err(e) => Err(BedrockConverseStreamError::from( e.as_service_error().unwrap(), )), }?; loop { let token = stream.recv().await; match token { Ok(Some(text)) => { let next = get_converse_output_text(text)?; print!("{}", next); Ok(()) } Ok(None) => break, Err(e) => Err(e .as_service_error() .map(BedrockConverseStreamError::from) .unwrap_or(BedrockConverseStreamError( "Unknown error receiving stream".into(), ))), }? } println!(); Ok(()) } fn get_converse_output_text( output: ConverseStreamOutputType, ) -> Result<String, BedrockConverseStreamError> { Ok(match output { ConverseStreamOutputType::ContentBlockDelta(event) => match event.delta() { Some(delta) => delta.as_text().cloned().unwrap_or_else(|_| "".into()), None => "".into(), }, _ => "".into(), }) }

문, 오류 유틸리티 및 상수를 사용합니다.

use aws_config::BehaviorVersion; use aws_sdk_bedrockruntime::{ error::ProvideErrorMetadata, operation::converse_stream::ConverseStreamError, types::{ error::ConverseStreamOutputError, ContentBlock, ConversationRole, ConverseStreamOutput as ConverseStreamOutputType, 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 BedrockConverseStreamError(String); impl std::fmt::Display for BedrockConverseStreamError { 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 BedrockConverseStreamError {} impl From<&str> for BedrockConverseStreamError { fn from(value: &str) -> Self { BedrockConverseStreamError(value.into()) } } impl From<&ConverseStreamError> for BedrockConverseStreamError { fn from(value: &ConverseStreamError) -> Self { BedrockConverseStreamError( match value { ConverseStreamError::ModelTimeoutException(_) => "Model took too long", ConverseStreamError::ModelNotReadyException(_) => "Model is not ready", _ => "Unknown", } .into(), ) } } impl From<&ConverseStreamOutputError> for BedrockConverseStreamError { fn from(value: &ConverseStreamOutputError) -> Self { match value { ConverseStreamOutputError::ValidationException(ve) => BedrockConverseStreamError( ve.message().unwrap_or("Unknown ValidationException").into(), ), ConverseStreamOutputError::ThrottlingException(te) => BedrockConverseStreamError( te.message().unwrap_or("Unknown ThrottlingException").into(), ), value => BedrockConverseStreamError( value .message() .unwrap_or("Unknown StreamOutput exception") .into(), ), } } }
  • API 자세한 내용은 Rust 참조 ConverseStream 의 섹션을 참조하세요. AWS SDK API

개발자 안내서 및 코드 예제의 AWS SDK 전체 목록은 섹션을 참조하세요에서 Amazon Bedrock 사용 AWS SDK. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.