

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 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 モデルに推論呼び出しを実行します。このセクションでは、Meta Llama モデルを呼び出す方法を示す Python コード例も含まれています。推論オペレーションでモデルを使用するには、そのモデルのモデル ID が必要です。モデル ID を取得するには、「[Amazon Bedrock でサポートされている基盤モデル](models-supported.md)」を参照してください。モデルによっては、[Converse API](conversation-inference.md) でも動作します。特定のMeta Llamaモデルが機能をサポートしているかどうかを確認するには、[「モデルの概要](model-cards.md)」を参照してください。コード例については、「[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>

リクエストボディは、[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) へのリクエストの `body` フィールドに渡されます。

**注記**  
[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|>
  ```

  **システムプロンプトを含むテンプレートの例**

  詳細については、「[https://llama.meta.com/docs/model-cards-and-prompt-formats/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/ja_jp/bedrock/latest/userguide/model-parameters-meta.html)
+ **top\_p** – 低い値を指定すると、可能性の低い選択肢を無視します。0 または 1.0 に設定すると、このオプションは無効になります。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/model-parameters-meta.html)
+ **max\_gen\_len** – 生成されたレスポンスで使用するトークンの最大数を指定します。生成されたテキストの長さが `max_gen_len` を超えると、モデルはレスポンスを切り捨てます。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/model-parameters-meta.html)

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

Llama 3 Instruct モデルは、Text Completion 推論呼び出しに対して次のフィールドを返します。

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

各フィールドの詳細は以下のとおりです。
+ **generation** – 生成されたテキスト。
+ **prompt\_token\_count** – プロンプト内のトークン数。
+ **generation\_token\_count** – 生成されたテキスト内のトークン数。
+ **stop\_reason** – モデルがテキストの生成を停止した理由。可能な値は以下のとおりです。
  + **停止** - モデルは入力プロンプトのテキストの生成を終了しました。
  + **長さ** - 生成されたテキストにおけるトークンの長さが `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\_gen\_len を調整し、プロンプトに特定の指示を含めます。