新闻动态

  • 首页
  • 新闻动态
  • 使用 Meta Llama 3 和 Amazon SageMaker JumpStart 的最佳提示

使用 Meta Llama 3 和 Amazon SageMaker JumpStart 的最佳提示

2026-01-27 13:02:30

使用 Meta Llama 3 和 Amazon SageMaker JumpStart 的最佳提示实践

文章重点

Meta Llama 3 是一款功能强大的大型语言模型LLM,为开发者提供了新的可能性。有效的提示设计对解锁模型潜力至关重要。本文介绍了在 Amazon SageMaker JumpStart 中使用 Meta Llama 3 的提示策略与技术。

Meta Llama 3 是 Meta 最新推出的大型语言模型,凭借其卓越的能力,迅速在人工智慧AI领域引起了广泛关注。随著开发者和企业开始探索这款强大模型的潜力,设计有效的提示成为释放其全部潜力的关键。

本文将深入探讨在使用 Amazon SageMaker JumpStart 时,针对 Meta Llama 3 的最佳提示实践与技巧,帮助您获得高质量的相关输出。我们讨论如何使用系统提示和少量示例,并如何优化推理参数,让您能够充分利用 Meta Llama 3。无论您是建立聊天机器人、内容生成器还是自订 AI 应用,这些提示策略将协助您充分运用这一尖端模型的力量。

Meta Llama 2 与 Meta Llama 3 的比较

Meta Llama 3 在大型语言模型LLM领域的趋势向前迈进了一大步。基于其前身 Meta Llama 2 的基础,最新版本在多种自然语言任务中展现了最先进的性能。与 Meta Llama 2 相比,Meta Llama 3 在推理、代码生成和指令遵循等领域的能力有显著提升。

Meta Llama 3 发布了四个新的 LLM,这是基于 Meta Llama 2 架构的改进版本。它们有两种变体80亿和700亿参数,并提供基本的预训练版本和经过指导调整的版本。此外,Meta 还在训练一个更大的 4000 亿参数模型,预计将进一步增强 Meta Llama 3 的能力。所有 Meta Llama 3 的变体具有令人印象深刻的 8000 个标记上下文长度,允许它们更好地处理与先前模型相比的长输入。

Meta Llama 3 与 Meta Llama 2 相比引入了几项架构上的变更,使用了仅解码器的转换器和全新的 128000 个代码器,以提高令牌效率和整体模型性能。Meta 将大量精力投入到策划一个超过 15 兆个来自各种公开来源的多样化预训练数据集,涵盖了 STEM、历史、当前事件等多方面。Meta 的后训练过程减少了错误拒绝率,旨在更好地使输出与人类偏好对齐,同时增加响应的多样性。

解决方案概述

SageMaker JumpStart 是 Amazon SageMaker 机器学习ML平台中的一个强大功能,为 ML 实践者提供一个完整的公共和专有基础模型FM中心。通过这项管理服务,ML 实践者可以访问来自领先模型中心和供应商的日益增长的前沿模型,并将它们部署到专用的 SageMaker 实例中,在隔离的环境中进行个性化模型训练和部署。

随著 Meta Llama 3 现已在 SageMaker JumpStart 上线,开发者可以通过无缝的部署过程来利用它的能力。您可以访问完整的 Amazon SageMaker MLOps 工具套件,如 Amazon SageMaker Pipelines、Amazon SageMaker Debugger,以及监控工具,这一切都在受保护的 AWS 环境内,并遵循虚拟私有云 (VPC) 控制。

借鉴我们在 Llama2Chat 时的经验,我们强调了一些关键技术,用于设计有效的提示并获得高质量的响应,以适应您的应用需求。无论您是构建会话型 AI 助手、改善搜索引擎,还是挑战语言理解的极限,这些提示策略将帮助您解除 Meta Llama 3 的全部潜能。

在继续深入提示之前,让我们确保拥有跟随范例所需的所有条件。

前置条件

要使用 SageMaker JumpStart 试用此解决方案,您需要以下前置条件:

使用 Meta Llama 3 和 Amazon SageMaker JumpStart 的最佳提示需求AWS 帐户,用于存储所有 AWS 资源。AWS 身份与访问管理IAM角色以访问 SageMaker。想了解 IAM 如何与 SageMaker 一起工作,请参阅 Amazon SageMaker 的身份与访问管理。访问 Amazon SageMaker Studio 或 SageMaker 笔记本实例,亦或是 PyCharm 或 Visual Studio Code 等互动开发环境IDE。建议使用 SageMaker Studio 进行简单的部署和推理。克隆 GitHub 存储库 以使用随附的笔记本。一个 mlg512xlarge 实例,用于模型部署的端点。您可能需要请求配额增长,请参见 请求配额增长。

在 SageMaker JumpStart 上部署 Meta Llama 3 8B

您可以透过 SageMaker JumpStart 模型中心,从 SageMaker Studio 或 SageMaker SDK 部署自己的模型端点。使用 SageMaker Studio 时,请完成以下步骤:

在 SageMaker Studio 中,在导航窗格选择 JumpStart。 选择 Meta 作为模型提供者,以查看 Meta AI 提供的所有模型。 选择 Meta Llama 8B Instruct 模型以查看有关该模型的详细信息,如许可证、训练所使用的数据以及如何使用该模型。 选择 Deploy 以将模型部署到端点。 您可以使用默认的端点和网络配置,或根据需要对其进行修改。 选择 Deploy 以部署该模型。

编写有效提示

当与像 Meta Llama 3 这样的 LLM 打交道时,提示设计至关重要。它是告知模型您希望它执行什么操作以及引导其响应的主要方式。针对每次交互设计清晰、具体的提示,对于获得有用且相关的输出至关重要。

尽管语言模型在其构建和训练方面存在某些相似之处,但在有效提示方面每个模型都有其自己的特点。这是因为它们基于不同数据、采用不同技术和设置进行训练,这可能导致行为和性能上的微妙差异。例如,一些模型可能对提示的具体措辞或结构更为敏感,而其他模型则可能需要更多上下文或示例才能生成准确的响应。另外,模型的用途和领域也能影响最佳提示策略,因为不同的任务可能需要不同的方法。

应进行实验和调整提示,以找出最适合每个特定模型和应用的有效方法。这一迭代过程对于发挥每个模型的全部潜力至关重要,并确保输出符合您的需求。

提示组件

在本节中,我们将讨论 Meta Llama 3 Instruct 在提示中期望的组件。换行‘n’是提示格式的一部分;为了示范的清晰性,我们将其表示为实际的换行。

小熊加速器bear458

以下是一个带有系统消息的示范提示:

ltbeginoftextgtltstartheaderidgtsystemltendheaderidgtYou are a helpful AI assistant for travel tips and recommendationslteotidgtltstartheaderidgtuserltendheaderidgtWhat can you help me withlteotidgtltstartheaderidgtassistantltendheaderidgt

该提示包含以下关键部分:

lt beginoftextgt 指定提示的开始。lt startheaderidgtsystemltendheaderidgt 指定后续消息的角色例如,system。您是一个提供旅游建议和推荐的 AI 助手 包含系统消息。lt eotidgt 指定输入消息的结束。lt startheaderidgtuserltendheaderidgt 指定后续消息的角色例如,user。您可以帮我什么? 包含用户消息。lt startheaderidgtassistantltendheaderidgt 以助手标题结束,提示模型开始生成。模型期待在提示结尾出现助手标题,以启动其补全。

依据此提示,Meta Llama 3 生成 {{assistantmessage}},并通过生成 lteotidgt 来标示 {{assistantmessage}} 的结束。

以下是一个带有单一用户消息的示范提示:

ltbeginoftextgtltstartheaderidgtuserltendheaderidgtWhat is Frances capitallteotidgtltstartheaderidgtassistantltendheaderidgt

接下来是系统提示和用户与助手之间的多轮对话:

ltbeginoftextgtltstartheaderidgtsystemltendheaderidgtYou are a helpful AI assistant for travel tips and recommendationslteotidgtltstartheaderidgtuserltendheaderidgtWhat is Frances capitallteotidgtltstartheaderidgtassistantltendheaderidgtBonjour! The capital of France is Paris!lteotidgtltstartheaderidgtuserltendheaderidgtWhat can I do therelteotidgtltstartheaderidgtassistantltendheaderidgtParis the City of Light offers a romantic getaway with mustsee attractions like the Eiffel Tower and Louvre Museum romantic experiences like river cruises and charming neighborhoods and delicious food and drink options with helpful tips for making the most of your triplteotidgtltstartheaderidgtuserltendheaderidgtGive me a detailed list of the attractions I should visit and time it takes in each one to plan my trip accordinglylteotidgtltstartheaderidgtassistantltendheaderidgt

基本技术

以下是编写提示的一些基本技术:

零样本提示 零样本提示不提供示例,完全依赖模型预先存在的知识根据给定的指令生成反应。以下是一个零样本提示示例:

ltbeginoftextgtltstartheaderidgtsystemltendheaderidgtProvide a brief summary of the causes of World War IIlteotidgtltstartheaderidgtuserltendheaderidgtThe causes of World War II includelteotidgtltstartheaderidgtassistantltendheaderidgt

这产生了以下回应:the aggressive expansion of fascist and nationalist ideologies particularly in Germany Italy and Japan which led to a complex web of alliances and tensions between nations Some key factors contributing to the outbreak of World War II include 1 The Treaty of Versailles The harsh penalties and reparations imposed on Germany after World War I led to widespread resentment and a sense of injustice creating a fertile ground for extremist ideologies like Nazism to flourish 2 Rise of Fascist and Nationalist Regimes The rise of fascist regimes in Germany Italy and Japan led by charismatic leaders like Adolf Hitler Benito Mussolini and Hideki Tojo who pursued aggressive expansionist policies and militarization 3 Appeasement Policy The policy of appeasement pursued by Britain and France which involved giving in to Germanys demands in the hope of avoiding war only emboldened Hitlers aggression and created a power vacuum in Europe 4 Economic Crisis The global economic crisis of the 1930s known as the Great Depression led to widespread unemployment poverty and social unrest creating an environment in which extremist ideologies could thrive 5 Japanese Aggression Japans invasion of Manchuria in 1931 and its subsequent withdrawal from the League of Nations marked the beginning of its expansionist policies which would eventually lead to its alliance with Germany and Italy 6 German Remilitarization of the Rhineland Germanys remilitarization of the Rhineland in 1936 which was demilitarized under the Treaty of Versailles was a direct challenge to the Treaty and marked the beginning of Germanys aggressive expansion 7 Anschluss with Austria Germanys annexation of Austria in 1938 known as the Anschluss further expanded its territory and created a sense of unease among its neighbors 8 Munich Agreement The Munich Agreement of 1938 in which Britain and France allowed Germany to annex the Sudetenland a region of Czechoslovakia with a majority German population is seen as a failed attempt to appease Hitler and avoid war 9 Invasion of Poland Germanys invasion of Poland on September 1 1939 marked the beginning of World War II as it prompted the United Kingdom and France to declare war on Germany These complex and interconnected factors ultimately led to the outbreak of World War II one of the deadliest conflicts in human history

少量样本提示 少量样本提示涉及提供少量示例通常是两个或更多以指定所需的输入和输出格式。模型从这些示例中学习,以生成新输入的适当反应。以下是一个少量样本提示示例:

ltbeginoftextgtltstartheaderidgtSystemltendheaderidgtExtract the relevant information from the following paragraph and present it in a JSON formatlteotidgtltstartheaderidgtUserltendheaderidgtMichael Doe a 45yearold teacher from Boston Massachusetts is an avid reader and enjoys gardening during his spare timeExample 1Paragraph John Doe is a 32yearold software engineer from San Francisco California He enjoys hiking and playing guitar in his free timeemployee { fullname John Doe city San Francisco state California occupation software engineer hobbies [ hiking playing guitar ] recentTravel not provided}Example 2Paragraph Emily Jax a 27yearold marketing manager from New York City loves traveling and trying new cuisines She recently visited Paris and enjoyed the citys rich cultural heritageemployee { fullname Emily Jax city New York City state New York occupation marketing manager hobbies [ traveling trying new cuisines ] recentTravel Paris} lteotidgtltstartheaderidgtAssistantltendheaderidgt

这产生了以下回应:employee { fullname Michael Doe city Boston state Massachusetts occupation teacher hobbies [ reading gardening ] recentTravel not provided