Meta模型Llama - Amazon Bedrock

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

Meta模型Llama

本节提供推理参数和使用以下模型的代码示例。Meta

  • Llama 2

  • Llama 2 Chat

  • Llama 3 Instruct

您可以使用InvokeModelInvokeModelWithResponseStream(流式传输)向MetaLlama模型发出推理请求。您需要获得希望使用的模型的模型 ID。要获取模型 ID,请参阅亚马逊 Bedrock 型号 ID

请求和回应

请求正文在请求body字段中传递给InvokeModelInvokeModelWithResponseStream

Request

Llama 2 ChatLlama 2、和Llama 3 Instruct模型具有以下推理参数。

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

以下是必要参数。

  • p rompt —(必填)要传递给模型的提示。

    有关提示格式的信息,请参见MetaLlama 2MetaLlama 3

以下是可选参数。

  • 温度-使用较低的值来降低响应中的随机性。

    默认 最低 最高

    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 ChatLlama 2、和Llama 3 Instruct模型返回文本完成推理调用的以下字段。

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

下面提供了有关每个字段的更多信息。

  • 生成-生成的文本。

  • prom@@ pt_token_count — 提示中的代币数量。

  • generation_token_count — 生成的文本中的标记数量。

  • stop_ reason — 响应停止生成文本的原因。可能的值有:

    • stop – 模型已结束为输入提示生成文本。

    • length – 生成的文本的令牌长度超过了对 InvokeModel(如果您要对输出进行流式传输,则为 InvokeModelWithResponseStream)的调用中的 max_gen_len 值。响应被截断为 max_gen_len 个令牌。考虑增大 max_gen_len 的值并重试。

代码示例

此示例说明如何调用 MetaLlama 2 Chat13B 模型。

# 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()