使用調用模型 API 在 Amazon 基岩上調用美洲駝 2 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用調用模型 API 在 Amazon 基岩上調用美洲駝 2

下列程式碼範例示範如何使用叫用模型 API,將文字訊息傳送至 Meta Lama 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
SDK for Go V2
注意

還有更多關於 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 的開發套件
注意

還有更多關於 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
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 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 的詳細資訊,請參閱AWS 開發套件InvokeModel中的 Python (博托 3) API 參考。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS SDK 使用此服務。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。