MetaモデルLlama - Amazon Bedrock

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

MetaモデルLlama

このセクションでは、 の次のモデルを使用するための推論パラメータとコード例を示しますMeta。

  • Llama 2

  • Llama 2 Chat

  • Llama 3 Instruct

InvokeModel または InvokeModelWithResponseStream (ストリーミング) を使用してMetaLlamaモデルに推論リクエストを行います。このとき、使用するモデルのモデル ID が必要になります。モデル ID を取得するには、「」を参照してくださいAmazon Bedrock IDs

リクエストとレスポンス

リクエスト本文は、 InvokeModelまたは へのリクエストの bodyフィールドに渡されますInvokeModelWithResponseStream

Request

Llama 2 Chat、Llama 2、および Llama 3 Instructモデルには、次の推論パラメータがあります。

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

必須パラメータを以下に示します。

  • prompt – (必須) モデルに渡すプロンプト。

    プロンプト形式の詳細については、MetaLlama 2「」およびMetaLlama 3「」を参照してください。

オプションのパラメータを以下に示します。

  • temperature – 低い値を使用してレスポンスのランダム性を減らします。

    デフォルト値 最小値 最大値

    0.5

    0

    1

  • top_p – 低い値を使用して、可能性の低いオプションを無視します。0 または 1.0 に設定すると、このオプションは無効になります。

    デフォルト値 最小値 最大値

    0.9

    0

    1

  • max_gen_len – 生成されたレスポンスで使用するトークンの最大数を指定します。生成されたテキストの長さが max_gen_len を超えると、モデルはレスポンスを切り捨てます。

    デフォルト値 最小値 最大値

    512

    1

    2048

Response

Llama 2 Chat、Llama 2、および Llama 3 Instructモデルは、テキスト補完推論呼び出しに対して次のフィールドを返します。

{ "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 の値を大きくしてやり直すことを検討してください。

サンプルのコード

この例では、Meta13B Llama 2 Chat モデルを呼び出す方法を示します。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate text with Meta Llama 2 Chat (on demand). """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_text(model_id, body): """ Generate an image using Meta Llama 2 Chat on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The text that the model generated, token information, and the reason the model stopped generating text. """ logger.info("Generating image with Meta Llama 2 Chat model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Meta Llama 2 Chat example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'meta.llama2-13b-chat-v1' prompt = """What is the average lifespan of a Llama?""" max_gen_len = 128 temperature = 0.1 top_p = 0.9 # Create request body. body = json.dumps({ "prompt": prompt, "max_gen_len": max_gen_len, "temperature": temperature, "top_p": top_p }) try: response = generate_text(model_id, body) print(f"Generated Text: {response['generation']}") print(f"Prompt Token count: {response['prompt_token_count']}") print(f"Generation Token count: {response['generation_token_count']}") print(f"Stop reason: {response['stop_reason']}") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) else: print( f"Finished generating text with Meta Llama 2 Chat model {model_id}.") if __name__ == "__main__": main()