亚马逊AWS官方博客

Amazon Bedrock 现已支持访问 Meta 的 Llama 2 Chat 13B 模型

今天,我们宣布 Meta 的 Llama 2 Chat 13B 大型语言模型(LLM)将在 Amazon Bedrock 上推出。通过此次发布,Amazon Bedrock 成为第一个为 Meta 的下一代 LLM Llama 2 提供完全托管的 API 的公有云服务。现在,各种规模的组织都可以在 Amazon Bedrock 上访问 Llama 2 Chat 模型,而无需管理底层基础设施。这是可访问性方面的一大飞跃。

Amazon Bedrock 是一项完全托管的服务,提供各种来自领先 AI 公司(包括 AI21 LabsAnthropicCohereStability AIAmazon 以及现在的 Meta)的高性能基础模型(FM),以及用于构建生成式人工智能应用程序的广泛功能,可简化开发,同时维护隐私和安全。您可以在 Antje 的这篇文章中了解更多关于 Amazon Bedrock 的信息

Llama 2 是由 Meta 推出的一系列公开可用的 LLM。Llama 2 基础模型使用来自在线公共数据来源的 2 万亿个令牌进行预训练。Meta 表示,在训练 Llama 2 13B 时,每小时要使用 184,320 块 GPU。这相当于,如果使用单个 GPU,要耗费 21.04 年的时间(未考虑闰年因素)。

Llama 2 Chat 模型以基本模型为基础构建而成,针对对话应用场景进行了优化。它通过 100 多万次人工注释(这种技术称为基于人类反馈进行强化学习(RLHF))进行了调整,并经过了 Meta 的测试,可在聊天应用场景中识别性能差距并减少潜在的问题回应数量(例如冒犯性回应或不当回应)。

为了推动打造负责任的协作式人工智能创新生态系统,Meta 为所有使用 Llama 2 的个人、创作者、开发者、研究人员、学者和各种规模的企业建立了一系列资源。其中我特别喜欢 Meta 的负责任使用指南,该资源面向开发者,提供了以负责任的方式构建由 LLM 提供支持的产品的最佳实践和注意事项,涵盖了从创始到部署的各个开发阶段。本指南可以很好地融入 AWS 工具和资源集中,支持以负责任的方式构建 AI。

现在,您可以通过调用 Amazon Bedrock API,或者使用 AWS SDKAWS 命令行界面(AWS CLI)将 LLama 2 Chat 模型集成到使用任意编程语言编写的应用程序中。

Llama 2 Chat 的实际操作
经常阅读 AWS 新闻博客的人都很清楚,我们乐于向您展示我们在博客中所写的技术。因此,让我们编写代码来与 Llama2 进行交互吧!

几周前,我非常荣幸地在 AWS UG 秘鲁会议上发表了演讲。JeffMarcia 也在场。Jeff 在会议开幕时发表了一场关于生成式人工智能的鼓舞人心的演讲,他使用了一整墙人工智能生成的美洲驼图像,这是秘鲁的标志性动物。那么,关于 Llama 2 Chat,还有哪些比美洲驼更值得谈论的话题呢?

(在写代码之前,我忍不住分享了我在马丘比丘旅游期间拍摄的两张美洲驼的照片)

马丘比丘的一只白色美洲驼 马丘比丘的一只棕色美洲驼

要开始在 Bedrock 上使用新模型,我首先要在控制台上导航到 Amazon Bedrock。我在左下角的窗格中选择模型访问权限,选择右上角的编辑按钮,然后启用对 Llama 2 Chat 模型的访问权限。

Bedrock Llama2 授予访问权限

在左侧导航栏的平台下选择聊天,无需编写任何代码即可与模型进行交互。

LLama2 的 Bedrock 聊天平台

现在我知道,我已可访问模型,接下来我在笔记本电脑上打开代码编辑器。我假定您已经配置了 AWS 命令行界面(AWS CLI),这将允许 AWS SDK 找到您的 AWS 凭证。在本演示中,我将使用 Python,但我想说明使用任何语言都可以调用 Bedrock。我还会分享一个公开的要点,其中包含用 Swift 编程语言编写的相同代码示例。

回到 Python 上来,我首先运行 ListFoundationModels API 调用,查找 Llama 2 Chat 13B 的 modelId

import boto3

bedrock = boto3.client(service_name='bedrock', region_name='us-east-1')
listModels = bedrock.list_foundation_models(byProvider='meta')
print("\n".join(list(map(lambda x: f"{x['modelName']} : { x['modelId'] }", listModels['modelSummaries']))))

运行以下代码可生成列表:

Llama 2 Chat 13B : meta.llama2-13b-chat-v1

我选择 meta-llama2-chat-13b 模型 ID,然后编写代码,向 LLama 2 Chat 13B 模型发送提示。

import boto3
import json

llamaModelId = 'meta.llama2-13b-chat-v1' 
prompt = "What is the difference between a llama and an alpaca?"

llamaPayload = json.dumps({ 
	'prompt': prompt,
    'max_gen_len': 512,
	'top_p': 0.9,
	'temperature': 0.2
})

bedrock_runtime = boto3.client(
    service_name='bedrock-runtime',
    region_name='us-east-1'
)
response = bedrock_runtime.invoke_model(
    body=llamaPayload,
    modelId=llamaModelId,
    accept='application/json',
    contentType='application/json'
)
body = response.get('body').read().decode('utf-8')
response_body = json.loads(body)
print(response_body['generation'].strip())

得到的回应如下:

Llamas and alpacas are both members of the camelid family,
but they are different species with distinct physical and behavioral characteristics.
Here are some of the main differences: (...)

为简洁起见,我把答案修改了一下。

您可以调整不同的请求参数,以查看参数会对回应造成什么影响。

优化您的提示
要从 Llama 2 Chat 模型获得更出色的结果,您可以尝试几种提示工程技巧,例如使用角色或思维链提示。

使用角色时,在提问之前,您可以通过提供给模型一个角色和指示来告诉它该如何表现。例如:

Prompt: You are a teacher.You explain things for six-year old kids.
What is the difference between a llama and an alpaca?

Answer: Great question! Both llamas and alpacas are animals that live in South America, and they look a little bit similar.But there are some differences that we can talk about.
First, let's talk about their fur.Llamas have longer fur than alpacas.It's like they have a big, fluffy coat that helps them stay warm in the cold weather.Alpacas, on the other hand, have shorter fur that's a little bit smoother.
Another difference is their ears.Llamas have banana-shaped ears that are really big, while alpacas have small, triangular ears.
Finally, let's talk about their size.Llamas are generally bigger than alpacas.They can grow up to 6 feet tall and weigh a lot, while alpacas are usually smaller, only growing up to 3 feet tall.
So, those are some of the main differences between llamas and alpacas.They're both really cool animals, and it's fun to learn about them!

借助思维链提示,您可以要求模型解释得出答案的每一个步骤。例如:

Prompt: You are a a very intelligent bot with exceptional critical thinking.
I went to the market and bought 10 apples.I gave 2 apples to your friend and 2 to the helper.I then went and bought 5 more apples and ate 1.How many apples did I remain with?
Let's think step by step.

Answer:
1.You went to the market and bought 10 apples.
2.You gave 2 apples to your friend.
3.You gave 2 apples to the helper.
4.You went back to the market and bought 5 more apples.
5.You ate 1 apple.
Now, let's count how many apples you have:
10 apples (from the first trip to the market) - 2 apples (given to your friend) - 2 apples (given to the helper) = 6 apples
6 apples + 5 apples (from the second trip to the market) = 11 apples
11 apples - 1 apple (that you ate) = 10 apples
Therefore, you remain with 10 apples.
I hope this helps! Do you have any more questions or would you like to play again?

Meta 在了解 Llama 笔记本的第 4.2 节中分享了更多示例。

推出情况
Llama 2 Chat 模型现已面向两个可以使用 Bedrock 的 AWS 区域的所有 AWS 客户开放:美国东部(弗吉尼亚州北部)和美国西部(俄勒冈州)。

您将需支付模型推理费用。您可以选择按使用量付费方式,这样做无需支付任何预付费用或周期性费用;AWS 按所处理的输入和输出令牌收费。或者,您可以预置足够的吞吐量来满足应用程序的性能要求,以换取基于时间的期限承诺。Bedrock 定价页面包含详细信息

借助这些信息,您可以做好准备,开始在应用程序中使用 Amazon Bedrock 和 Llama 2 Chat 模型。

立即构建!

— seb