使用调用模型 API 在 Amazon Bedrock 上调用 Meta Llama 2 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用调用模型 API 在 Amazon Bedrock 上调用 Meta Llama 2

以下代码示例展示了如何使用调用模型 API 向 Meta Llama 2 发送短信。

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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
适用于 Go V2 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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
适用于 PHP 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用调用模型 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 的详细信息,请参阅适用InvokeModelPython 的AWS SDK (Boto3) API 参考

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将此服务与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。