Meta Llama   模型 - Amazon Bedrock

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

Meta Llama   模型

本节介绍的请求参数和响应字段 Meta Llama 模型。使用此信息进行推理调用 Meta Llama 使用InvokeModelInvokeModelWithResponseStream(流式传输)操作的模型。本节还包括 Python 显示如何调用的代码示例 Meta Llama 模型。要在推理操作中使用模型,您需要相关模型的模型 ID。要获取模型 ID,请参阅 Amazon Bedrock 中支持的根基模型。有些型号也可以使用 Converse API。 要检查是否 Converse API支持特定的 Meta Llama 模型,请参阅支持的模型和模型功能。有关更多代码示例,请参阅 使用 Amazon Bedrock 的代码示例 AWS SDKs

Amazon Bedrock 中的基础模型支持输入和输出模态,这些模态因模型而异。要检查一下模式 Meta Llama 模型支持,请参阅Amazon Bedrock 中支持的根基模型。要查看 Amazon Bedrock 有哪些 Meta Llama 模型支持,请参阅Amazon Bedrock 中支持的根基模型。要查看哪些 AWS 区域 Meta Llama 型号可在中找到,请参阅Amazon Bedrock 中支持的根基模型

当你使用进行推理调用时 Meta Llama 模型,则需要包含模型的提示。有关为 Amazon Bedrock 支持的模型创建提示的一般信息,请参阅 提示工程概念。对于 Meta Llama 具体的提示信息,请参阅 Meta Llama 及时的工程指南

注意

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

请求和响应

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

Request

Llama 2 Chat, Llama 2, Llama 3 Instruct, Llama 3.1 Instruct,以及 Llama 3.2 Instruct 模型具有以下推理参数。

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

NOTE: Llama 3.2 模型增加了images请求结构,即字符串列表。示例:images: Optional[List[str]]

以下是必要参数。

  • prompt –(必要)要传递给模型的提示。With Llama 2 Chat,使用以下模板设置对话的格式。

    <|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|>

    <<SYS>> 词元之间的指令为模型提供了系统提示。下面是一个包含系统提示符的示例。

    <s>[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>> There's a llama in my garden What should I do? [/INST]

    有关更多信息,请参阅下列内容。

以下是可选参数。

  • 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 – 响应停止生成文本的原因。可能的值有:

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

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

代码示例

此示例说明如何调用 Meta Llama 2 Chat 13B 型号。

# 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') response = bedrock.invoke_model( body=body, modelId=model_id) 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 = """<s>[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>> There's a llama in my garden What should I do? [/INST]""" 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()