本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Meta Llama 模型
本部分介绍了 Meta Llama 模型的请求参数和响应字段。使用此信息通过InvokeModel和 InvokeModelWithResponseStream(流式传输)操作对MetaLlama模型进行推理调用。本部分还包括 Python 代码示例,展示了如何调用 Meta Llama 模型。要在推理操作中使用模型,您需要相关模型的模型 ID。要获取模型 ID,请参阅 Amazon Bedrock 中支持的根基模型。有些模型也可以使用 ConverseAPI。要检查 Converse API 是否支持特定MetaLlama模型,请参阅支持的模型和模型功能。有关更多代码示例,请参阅 使用 Amazon Bedrock 的代码示例 AWS SDKs。
Amazon Bedrock 中的基础模型支持输入和输出模态,这些模态因模型而异。要查看 Meta Llama 模型支持的模态,请参阅 Amazon Bedrock 中支持的根基模型。要查看 Meta Llama 模型支持哪些 Amazon Bedrock 功能,请参阅 Amazon Bedrock 中支持的根基模型。要查看MetaLlama模型在哪些 AWS 区域可用,请参阅Amazon Bedrock 中支持的根基模型。
使用 Meta Llama 模型进行推理调用时,您可以为模型创建提示。有关为 Amazon Bedrock 支持的模型创建提示的一般信息,请参阅 提示工程概念。有关 Meta Llama 的特定提示信息,请参阅 MetaLlama 提示工程指南
注意
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”,并在提示中加入具体说明。