모델 호출 API를 사용하여 Amazon Bedrock에서 메타 라마 2를 호출하십시오. - Amazon Bedrock

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

모델 호출 API를 사용하여 Amazon Bedrock에서 메타 라마 2를 호출하십시오.

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

.NET
AWS SDK for .NET
참고

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

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

/// <summary> /// Asynchronously invokes the Meta Llama 2 Chat model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that you want Llama 2 to complete.</param> /// <returns>The inference response from the model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for Meta Llama 2 Chat, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html /// </remarks> public static async Task<string> InvokeLlama2Async(string prompt) { string llama2ModelId = "meta.llama2-13b-chat-v1"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); string payload = new JsonObject() { { "prompt", prompt }, { "max_gen_len", 512 }, { "temperature", 0.5 }, { "top_p", 0.9 } }.ToJsonString(); string generatedText = ""; try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = llama2ModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { return JsonNode.ParseAsync(response.Body) .Result?["generation"]?.GetValue<string>() ?? ""; } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return generatedText; }
  • API 세부 정보는 AWS SDK for .NET API InvokeModel참조를 참조하십시오.

Go
SDK for Go V2
참고

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

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

// Each model provider has their own individual request and response formats. // For the format, ranges, and default values for Meta Llama 2 Chat, refer to: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html type Llama2Request struct { Prompt string `json:"prompt"` MaxGenLength int `json:"max_gen_len,omitempty"` Temperature float64 `json:"temperature,omitempty"` } type Llama2Response struct { Generation string `json:"generation"` } // Invokes Meta Llama 2 Chat on Amazon Bedrock to run an inference using the input // provided in the request body. func (wrapper InvokeModelWrapper) InvokeLlama2(prompt string) (string, error) { modelId := "meta.llama2-13b-chat-v1" body, err := json.Marshal(Llama2Request{ Prompt: prompt, MaxGenLength: 512, Temperature: 0.5, }) if err != nil { log.Fatal("failed to marshal", err) } output, err := wrapper.BedrockRuntimeClient.InvokeModel(context.TODO(), &bedrockruntime.InvokeModelInput{ ModelId: aws.String(modelId), ContentType: aws.String("application/json"), Body: body, }) if err != nil { ProcessError(err, modelId) } var response Llama2Response if err := json.Unmarshal(output.Body, &response); err != nil { log.Fatal("failed to unmarshal", err) } return response.Generation, nil }
  • API 세부 정보는 AWS SDK for Go API InvokeModel참조를 참조하십시오.

Java
SDK for Java 2.x
참고

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

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

// Send a prompt to Meta Llama 2 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 2 Chat 13B. var modelId = "meta.llama2-13b-chat-v1"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 2's prompt format. var prompt = "<s>[INST] " + userMessage + " [/INST]"; // 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 2 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
  • API 세부 정보는 AWS SDK for Java 2.x API InvokeModel참조를 참조하십시오.

JavaScript
JavaScript (v3) 용 SDK
참고

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

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

// Send a prompt to Meta Llama 2 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 2 Chat 13B. const modelId = "meta.llama2-13b-chat-v1"; // Define the user message to send. const userMessage = "Describe the purpose of a 'hello world' program in one sentence."; // Embed the message in Llama 2's prompt format. const prompt = `<s>[INST] ${userMessage} [/INST]`; // 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 2 prompt format at: // https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
  • API 세부 정보는 AWS SDK for JavaScript API InvokeModel참조를 참조하십시오.

PHP
SDK for PHP
참고

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

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

public function invokeLlama2($prompt) { # The different model providers have individual request and response formats. # For the format, ranges, and default values for Meta Llama 2 Chat, refer to: # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html $completion = ""; try { $modelId = 'meta.llama2-13b-chat-v1'; $body = [ 'prompt' => $prompt, 'temperature' => 0.5, 'max_gen_len' => 512, ]; $result = $this->bedrockRuntimeClient->invokeModel([ 'contentType' => 'application/json', 'body' => json_encode($body), 'modelId' => $modelId, ]); $response_body = json_decode($result['body']); $completion = $response_body->generation; } catch (Exception $e) { echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n"; } return $completion; }
  • API 세부 정보는 AWS SDK for PHP 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 2. 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 2 Chat 13B. model_id = "meta.llama2-13b-chat-v1" # Define the message to send. user_message = "Describe the purpose of a 'hello world' program in one line." # Embed the message in Llama 2's prompt format. prompt = f"<s>[INST] {user_message} [/INST]" # 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 버전에 대한 세부 정보도 포함되어 있습니다.