

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

# Meta Llama 模型
<a name="model-parameters-meta"></a>

本部分介绍了 Meta Llama 模型的请求参数和响应字段。使用这些信息，您可以通过 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 和 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)（流式传输）操作对 Meta Llama 模型进行推理调用。本部分还包括 Python 代码示例，展示了如何调用 Meta Llama 模型。要在推理操作中使用模型，您需要相关模型的模型 ID。要获取模型 ID，请参阅 [Amazon Bedrock 中支持的根基模型](models-supported.md)。有些模型还能与 [Converse API](conversation-inference.md) 配合使用。要查看 Converse API 是否支持特定的 Meta Llama 模型，请参阅[支持的模型和模型功能](conversation-inference-supported-models-features.md)。有关更多代码示例，请参阅 [使用 Amazon Bedrock 的代码示例 AWS SDKs](service_code_examples.md)。

Amazon Bedrock 中的基础模型支持输入和输出模态，这些模态因模型而异。要查看 Meta Llama 模型支持的模态，请参阅 [Amazon Bedrock 中支持的根基模型](models-supported.md)。要查看 Meta Llama 模型支持哪些 Amazon Bedrock 功能，请参阅 [Amazon Bedrock 中支持的根基模型](models-supported.md)。要查看 Meta Llama 模型在哪些 AWS 区域中可用，请参阅 [Amazon Bedrock 中支持的根基模型](models-supported.md)。

使用 Meta Llama 模型进行推理调用时，您可以为模型创建提示。有关为 Amazon Bedrock 支持的模型创建提示的一般信息，请参阅 [提示工程概念](prompt-engineering-guidelines.md)。有关 Meta Llama 的特定提示信息，请参阅 [MetaLlama 提示工程指南](https://ai.meta.com/llama/get-started/#prompting)。

**注意**  
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

**Topics**
+ [

## 请求和响应
](#model-parameters-meta-request-response)
+ [

## 代码示例
](#api-inference-examples-meta-llama)

## 请求和响应
<a name="model-parameters-meta-request-response"></a>

请求正文在请求的 `body` 字段中传递给 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 或 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)。

**注意**  
您无法将 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html) 或 [ConverseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html)（流式传输）操作与 Llama 4 Instruct 结合使用。

------
#### [ Request ]

Llama 3 Instruct、Llama 3.1 Instruct、Llama 3.2 Instruct 和 Llama 4 Instruct 模型具有以下推理参数：

```
{
    "prompt": string,
    "temperature": float,
    "top_p": float,
    "max_gen_len": int
}
```

注意：Llama 3.2 及更高版本的模型向请求结构添加了 `images`，它是一个字符串列表。示例：`images: Optional[List[str]]`

以下是必要参数：
+  **prompt** –（必要）要传递给模型的提示。要获得最佳结果，可使用以下模板设置对话的格式。

  ```
  <|begin_of_text|><|start_header_id|>user<|end_header_id|>
  
  What can you help me with?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  ```

  **带系统提示的示例模板**

  下面是一个包含系统提示符的示例。

  ```
  <|begin_of_text|><|start_header_id|>system<|end_header_id|>
  
  You are a helpful AI assistant for travel tips and recommendations<|eot_id|><|start_header_id|>user<|end_header_id|>
  
  What can you help me with?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  ```

  **多回合对话示例**

  以下是一个多回合对话的示例提示。

  ```
  <|begin_of_text|><|start_header_id|>user<|end_header_id|>
  
  What is the capital of France?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  
  The capital of France is Paris!<|eot_id|><|start_header_id|>user<|end_header_id|>
  
  What is the weather like in Paris?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  ```

  **带系统提示的示例模板**

  有关更多信息，请参阅 [Meta。Llama 3](https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3)

以下是可选参数：
+ **temperature** – 使用较低的值可降低响应中的随机性。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-meta.html)
+ **top\$1p** – 使用较低的值可忽略不太可能的选项。设置为 0 或 1.0 可禁用。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-meta.html)
+ **max\$1gen\$1len** – 指定要在生成的响应中使用的最大词元数。一旦生成的文本超过 `max_gen_len`，模型就会截断响应。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/model-parameters-meta.html)

------
#### [ Response ]

Llama 3 Instruct 模型将为文本补全推理调用返回以下字段。

```
{
    "generation": "\n\n<response>",
    "prompt_token_count": int,
    "generation_token_count": int,
    "stop_reason" : string
}
```

下面提供了有关每个字段的更多信息。
+ **generation** – 生成的文本。
+ **prompt\$1token\$1count** – 提示中的词元数。
+ **generation\$1token\$1count** – 生成的文本中的词元数。
+ **stop\$1reason** – 响应停止生成文本的原因。可能的值有：
  + **stop** – 模型已结束为输入提示生成文本。
  + **length** – 生成的文本的令牌长度超过了对 `InvokeModel`（如果您要对输出进行流式传输，则为 `InvokeModelWithResponseStream`）的调用中的 `max_gen_len` 值。响应被截断为 `max_gen_len` 个令牌。考虑增大 `max_gen_len` 的值并重试。

------

## 代码示例
<a name="api-inference-examples-meta-llama"></a>

此示例展示了如何调用 *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\$1gen\$1len”，并在提示中加入特定说明。