모델 호출 API를 사용하여 Amazon Bedrock에서 메타 라마 3 호출 - Amazon Bedrock

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

모델 호출 API를 사용하여 Amazon Bedrock에서 메타 라마 3 호출

다음 코드 예제는 Invoke Model API를 사용하여 Meta Lama 3에 문자 메시지를 보내는 방법을 보여줍니다.

Java
SDK for Java 2.x
참고

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

Invoke Model API를 사용하여 문자 메시지를 보내세요.

// Send a prompt to Meta Llama 3 and print the response. public class InvokeModelQuickstart { public static void main(String[] args) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeClient.builder() .region(Region.US_WEST_2) .build(); // Set the model ID, e.g., Llama 3 8B Instruct. var modelId = "meta.llama3-8b-instruct-v1:0"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 3's prompt format. var prompt = MessageFormat.format(""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {0} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """, userMessage); // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Encode and send the request. var response = client.invokeModel(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId)); // Decode the native response body. var nativeResponse = new JSONObject(response.body().asUtf8String()); // Extract and print the response text. var responseText = nativeResponse.getString("generation"); System.out.println(responseText); } } // Learn more about the Llama 3 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3
  • API 세부 정보는 AWS SDK for Java 2.x API InvokeModel참조를 참조하십시오.

JavaScript
JavaScript (v3) 용 SDK
참고

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

Invoke Model API를 사용하여 문자 메시지를 보내세요.

// Send a prompt to Meta Llama 3 and print the response. import { BedrockRuntimeClient, InvokeModelCommand, } from "@aws-sdk/client-bedrock-runtime"; // Create a Bedrock Runtime client in the AWS Region of your choice. const client = new BedrockRuntimeClient({ region: "us-west-2" }); // Set the model ID, e.g., Llama 3 8B Instruct. const modelId = "meta.llama3-8b-instruct-v1:0"; // Define the user message to send. const userMessage = "Describe the purpose of a 'hello world' program in one sentence."; // Embed the message in Llama 3's prompt format. const prompt = ` <|begin_of_text|> <|start_header_id|>user<|end_header_id|> ${userMessage} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> `; // Format the request payload using the model's native structure. const request = { prompt, // Optional inference parameters: max_gen_len: 512, temperature: 0.5, top_p: 0.9, }; // Encode and send the request. const response = await client.send( new InvokeModelCommand({ contentType: "application/json", body: JSON.stringify(request), modelId, }), ); // Decode the native response body. /** @type {{ generation: string }} */ const nativeResponse = JSON.parse(new TextDecoder().decode(response.body)); // Extract and print the generated text. const responseText = nativeResponse.generation; console.log(responseText); // Learn more about the Llama 3 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3
  • API 세부 정보는 AWS SDK for JavaScript API InvokeModel참조를 참조하십시오.

Python
SDK for Python(Boto3)
참고

자세한 내용은 에서 확인할 수 GitHub 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

Invoke Model API를 사용하여 문자 메시지를 보내세요.

# Use the native inference API to send a text message to Meta Llama 3. import boto3 import json # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Llama 3 8b Instruct. model_id = "meta.llama3-8b-instruct-v1:0" # Define the message to send. user_message = "Describe the purpose of a 'hello world' program in one line." # Embed the message in Llama 3's prompt format. prompt = f""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {user_message} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """ # Format the request payload using the model's native structure. native_request = { "prompt": prompt, "max_gen_len": 512, "temperature": 0.5, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["generation"] print(response_text)
  • API에 대한 자세한 내용은 파이썬용AWS SDK (Boto3) API 레퍼런스를 참조하십시오 InvokeModel.

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