本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Meta Llama 模型
本節說明 Meta Llama 模型的請求參數和回應欄位。使用此資訊透過 InvokeModel 和 InvokeModelWithResponseStream (串流) 操作對 Meta Llama 模型進行推論呼叫。本節也包含 Python 程式碼範例,示範如何呼叫 Meta Llama 模型。若要在推論操作中使用模型,您需要模型的模型 ID。若要取得模型 ID,請參閱 Amazon Bedrock 中支援的基礎模型。某些模型也可以使用 Converse API。若要檢查 Converse API 是否支援特定 Meta Llama 模型,請參閱 支援的模型和模型功能。如需更多程式碼範例,請參閱 使用 AWSSDKs Amazon Bedrock 程式碼範例。
Amazon Bedrock 中的基礎模型支援輸入和輸出模態,因模型而異。若要檢查 Meta Llama 模型支援的模態,請參閱 Amazon Bedrock 中支援的基礎模型。若要檢查 Meta Llama 模型支援的 Amazon Bedrock 功能,請參閱 Amazon Bedrock 中支援的基礎模型。若要檢查哪些 AWS 區域提供 Meta Llama 模型,請參閱 Amazon Bedrock 中支援的基礎模型。
當您使用 Meta Llama 模型進行推論呼叫時,您會包含模型的提示。如需建立 Amazon Bedrock 支援之模型提示的相關資訊,請參閱 提示工程概念。如需 Meta Llama 特定提示資訊,請參閱《Meta Llama 提示工程指南
注意
Llama 3.2 Instruct 和 Llama 3.3 Instruct 模型使用地理柵欄。這表示這些模型不能在可供「區域」表中所列模型使用的 AWS 區域之外使用。
本節提供有關使用 Meta 中下列模型的資訊。
Llama 3 Instruct
Llama 3.1 Instruct
Llama 3.2 Instruct
Llama 3.3 Instruct
Llama 4 Instruct
請求與回應
請求本文在請求 body 欄位中傳遞到 InvokeModel 或 InvokeModelWithResponseStream。
注意
您無法將 InvokeModelWithResponseStream 或 ConverseStream (串流) 操作搭配 Llama 4 Instruct 使用。
範例程式碼
此範例展示如何 Llama 3 Instruct 模型。
# Use the native inference API to send a text message to Meta Llama 3. import boto3 import json from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS 區域 of your choice. client = boto3.client("bedrock-runtime", region_name="us-west-2") # Set the model ID, e.g., Llama 3 70b Instruct. model_id = "meta.llama3-70b-instruct-v1:0" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Embed the prompt in Llama 3's instruction format. formatted_prompt = f""" <|begin_of_text|><|start_header_id|>user<|end_header_id|> {prompt} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """ # Format the request payload using the model's native structure. native_request = { "prompt": formatted_prompt, "max_gen_len": 512, "temperature": 0.5, } # Convert the native request to JSON. request = json.dumps(native_request) try: # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1) # 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)
此範例展示如何使用 Llama 3 Instruct 模型控制產生長度。如需詳細回應或摘要,請調整 `max_gen_len`,並在提示中包含特定指示。