Mozilla AI 指南发布,附带摘要代码示例

Mozilla AI 指南现已发布,欢迎您阅读并熟悉它。您可以点击此处访问它。

我们的愿景是,AI 指南将成为所有新开发者的入门指南,也是他们寻求清晰度和灵感的宝库,确保 AI 创新能够丰富日常生活。AI 指南的初始重点是语言模型,其目标是成为一个由社区驱动的协作资源,涵盖其他类型的模型。

为了开始,Mozilla AI 指南的前几部分将深入探讨有关大型语言模型 (LLM) 的常见问题。 AI 基础涵盖了 AI、ML、LLM 的概念,解释了这些概念的含义以及它们之间的关系。本节还分析了使用 LLM 的优缺点。 语言模型 101继续构建 AI 基础的共享知识,并深入探讨语言模型的下一层级。它将回答诸如“训练 ML 模型意味着什么”或“什么是‘人类在循环中’方法”等问题。

我们将跳到关于 选择 ML 模型 的最后一节,并在下面的代码中演示如何使用开源模型对特定文本进行摘要。您可以点击此处访问 Colab 笔记本,或继续阅读。

使用语言模型的第一步

与其他指南不同,本指南旨在通过以下方式帮助您选择适合自己想要完成的任务的模型:

  • 教您如何始终处于已发布的 AI 研究的前沿
  • 拓宽您对任何给定任务的当前开放选择的视角
  • 避免绑定到封闭源/封闭数据的大型语言模型(例如 OpenAI、Anthropic)
  • 创建一个数据主导的系统,用于始终识别和使用任何特定任务的最先进 (SOTA) 模型。

我们将以“文本摘要”作为我们的第一个任务。

那么……我们为什么不使用其中一个流行的大型语言模型呢?

好问题。大多数功能强大的 LLM 都能完成许多任务,包括摘要,但并非所有 LLM 都擅长您想让它们做的特定工作。我们应该弄清楚如何评估它们是否真的能够做到。

此外,许多当前流行的 LLM 并非开放的,是在未公开的数据集上训练的,并表现出偏差。负责任的 AI 使用需要谨慎选择,而我们在这里帮助您做出明智的选择。

最后,大多数大型 LLM 需要强大的 GPU 计算能力才能使用。虽然有很多模型可以作为服务使用,但其中大多数每 API 调用都需要付费。当某些更常见任务可以使用现有开放模型和现成硬件以良好的质量完成时,付费就显得没有必要了。

为什么使用开放模型很重要?

在过去几十年中,工程师们一直受益于能够从开源项目开始,最终将开源项目推向生产环境。这种默认状态现在正处于危险之中。

是的,有很多开放模型可以出色地完成工作。但是,大多数指南并没有讨论如何使用简单的步骤开始使用它们,而是偏向于现有的封闭 API。

资金正在流向商业 AI 项目,这些项目拥有比开源贡献者更大的预算来推广他们的工作,这不可避免地导致工程师从封闭源项目开始,并将昂贵的封闭项目推向生产环境。

我们的第一个项目 - 摘要

我们将:

  • 找到要进行摘要的文本。
  • 找出如何使用当前最先进的开源模型对它们进行摘要。
  • 编写一些代码来实现。
  • 使用相关指标评估结果的质量。

为简便起见,让我们以字符串形式获取 **Mozilla 可信 AI 指南**。

请注意,在现实世界中,您可能需要使用其他库来提取任何特定文件类型的文本内容。

import textwrap

content = """Mozilla's "Trustworthy AI" Thinking Points:

PRIVACY: How is data collected, stored, and shared? Our personal data powers everything from traffic maps to targeted advertising. Trustworthy AI should enable people to decide how their data is used and what decisions are made with it.

FAIRNESS: We’ve seen time and again how bias shows up in computational models, data, and frameworks behind automated decision making. The values and goals of a system should be power aware and seek to minimize harm. Further, AI systems that depend on human workers should protect people from exploitation and overwork.

TRUST: People should have agency and control over their data and algorithmic outputs, especially considering the high stakes for individuals and societies. For instance, when online recommendation systems push people towards extreme, misleading content, potentially misinforming or radicalizing them.

SAFETY: AI systems can carry high risk for exploitation by bad actors. Developers need to implement strong measures to protect our data and personal security. Further, excessive energy consumption and extraction of natural resources for computing and machine learning accelerates the climate crisis.

TRANSPARENCY: Automated decisions can have huge personal impacts, yet the reasons for decisions are often opaque. We need to mandate transparency so that we can fully understand these systems and their potential for harm."""

太好了。现在我们准备开始进行摘要了。

简短的背景介绍

AI 领域发展迅速,需要每周花大量时间阅读科学论文才能了解整体情况和最新技术。

对于刚接触 AI 的工程师来说,这需要付出很多努力,才能做到:

  • 发现哪些开放模型存在
  • 哪些模型适合任何特定任务
  • 哪些基准用于评估这些模型
  • 根据评估结果,哪些模型表现良好
  • 哪些模型可以在现有硬件上运行

对于有期限的工程师来说,这是一个问题。目前还没有关于如何使用开源 AI 模型的集中式讨论。相反,存在着分散的 X(以前称为 Twitter)帖子、随机的私人团体以及大量的口口相传。

但是,一旦我们有了解决上述所有问题的流程,您将拥有永远站在已发布的 AI 研究前沿的方法。

如何获取可用开放摘要模型的列表?

目前,我们推荐 Huggingface及其按任务划分的庞大开放模型目录。这是一个很好的起点。请注意,这些列表中也包含了更大的 LLM,因此我们需要进行筛选。

在这个庞大的摘要模型列表中,我们应该选择哪些呢?

我们不知道这些模型是在哪些数据集上训练的。例如,在新闻文章而不是 Reddit 帖子数据集上训练的摘要器在处理新闻文章时会表现得更好。

我们需要的是一套指标和基准,以便我们可以使用它们对这些模型进行苹果对苹果的比较。

如何评估摘要模型?

以下步骤可用于评估任何可用模型的任何任务。目前,这需要在几个数据源之间跳转,但我们将努力在未来简化这一过程。

步骤

  1. 找到用于训练摘要模型的最常见数据集。
  2. 找到在这些数据集上评估摘要模型的最常用指标。
  3. 快速审核训练数据来源、质量和任何表现出的偏差,以符合负责任的 AI 使用原则。

寻找数据集

最简单的方法是使用 Papers With Code,这是一个出色的资源,可以根据任务查找最新的科学论文,这些论文也附带代码库。

首先,按 引用次数最多的基于文本的英语数据集筛选 Papers With Code 的“文本摘要”数据集。

让我们选择(截至撰写本文时)引用次数最多的数据集—— “CNN/DailyMail”数据集。通常,引用次数最多是受欢迎程度的一个标志。

现在,您无需下载此数据集。但我们将回顾 Papers With Code 提供的信息,以便在下一步中了解更多信息。此数据集也可以在 Huggingface 上获得。

您需要检查三件事:

  • 许可证
  • 最近的论文
  • 数据是否可追踪,方法是否透明

首先,检查许可证。在本例中,它是 MIT 许可证,这意味着它可以用于商业项目和个人项目。

接下来,查看使用此数据集的论文是否最近。您可以按降序排列论文来做到这一点。这个特定数据集有很多来自 2023 年的论文——太棒了!

最后,让我们检查一下数据是否来自可靠的来源。在本例中,数据集是由 IBM 与蒙特利尔大学合作生成的。太好了。

现在,让我们深入了解如何评估使用此数据集的模型。

评估模型

接下来,我们寻找在摘要任务的不同数据集上常见的度量指标。但如果您不熟悉摘要文献,您就不知道这些指标是什么。

为了找到答案,请选择一个与您想看到的结果最接近的“子任务”。我们想对上面拉取的 CNN 文章进行摘要,因此让我们选择 “抽象文本摘要”

现在我们开始进入正题!这个页面包含大量新信息。

提到了三个新术语:ROUGE-1、ROUGE-2 和 ROUGE-L。这些是用于 衡量摘要性能 的指标。

还有一个模型列表及其在这三个指标上的得分——这正是我们想要的。

假设我们以 ROUGE-1 作为指标,现在我们可以更详细地评估排名前三的模型。所有三个模型都接近 50,这是一个很有希望的 ROUGE 得分(阅读有关 ROUGE 的资料)。

测试模型

好的,我们有几个候选模型,让我们选择一个可以在本地机器上运行的模型。许多模型在 GPU 上运行时表现最佳,但也有很多模型在 CPU 上也能快速生成摘要。让我们从其中一个开始——**Google 的 Pegasus**。

# first we install huggingface's transformers library
%pip install transformers sentencepiece

然后我们在 Huggingface 上 找到 Pegasus。请注意,Pegasus 部分训练数据集包括 CNN/DailyMail,这对我们的文章摘要来说是个好兆头。有趣的是,Google 还提供了一个仅在我们选择的特定数据集上训练的 Pegasus 变体,我们应该使用它。

from transformers import PegasusForConditionalGeneration, PegasusTokenizer 
import torch 

# Set the seed, this will help reproduce results. Changing the seed will 
# generate new results 
from transformers import set_seed 
set_seed(248602) 

# We're using the version of Pegasus specifically trained for summarization 
# using the CNN/DailyMail dataset 
model_name = "google/pegasus-cnn_dailymail"

# If you're following along in Colab, switch your runtime to a
# T4 GPU or other CUDA-compliant device for a speedup
device = "cuda" if torch.cuda.is_available() else "cpu" 

# Load the tokenizer
tokenizer = PegasusTokenizer.from_pretrained(model_name) 

# Load the model 
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(device)

# Tokenize the entire content
batch = tokenizer(content, padding="longest", return_tensors="pt").to(device)

# Generate the summary as tokens
summarized = model.generate(**batch)

# Decode the tokens back into text
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]

# Compare
def compare(original, summarized_text):
  print(f"Article text length: {len(original)}\n")
  print(textwrap.fill(summarized_text, 100))
  print()
  print(f"Summarized length: {len(summarized_text)}")

compare(content, summarized_text)
Article text length: 1427

Trustworthy AI should enable people to decide how their data is used.<n>values and goals of a system
should be power aware and seek to minimize harm.<n>People should have agency and control over their
data and algorithmic outputs.<n>Developers need to implement strong measures to protect our data and
personal security.

Summarized length: 320

好了,我们得到了一个结果!不过有点短。让我们看看能否让摘要更长一些……


set_seed(860912)

# Generate the summary as tokens, with a max_new_tokens
summarized = model.generate(**batch, max_new_tokens=800)
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]

compare(content, summarized_text)
Article text length: 1427

Trustworthy AI should enable people to decide how their data is used.<n>values and goals of a system
should be power aware and seek to minimize harm.<n>People should have agency and control over their
data and algorithmic outputs.<n>Developers need to implement strong measures to protect our data and
personal security.

Summarized length: 320

嗯,效果不太好。让我们尝试另一种方法,叫做“**采样**”。这种方法允许模型根据其条件概率分布(具体来说,是该词出现在前一个词之后的概率)来选择下一个词。

我们还会设置“**温度**”。这个变量用于控制生成输出的随机性和创造性水平。

set_seed(118511)
summarized = model.generate(**batch, do_sample=True, temperature=0.8, top_k=0)
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]
compare(content, summarized_text)
Article text length: 1427

Mozilla's "Trustworthy AI" Thinking Points:.<n>People should have agency and control over their data
and algorithmic outputs.<n>Developers need to implement strong measures to protect our data.

Summarized length: 193

更短了,但质量更高。调整温度可能会有所帮助。

set_seed(108814)
summarized = model.generate(**batch, do_sample=True, temperature=1.0, top_k=0)
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]
compare(content, summarized_text)
Article text length: 1427

Mozilla's "Trustworthy AI" Thinking Points:.<n>People should have agency and control over their data
and algorithmic outputs.<n>Developers need to implement strong measures to protect our data and
personal security.<n>We need to mandate transparency so that we can fully understand these systems
and their potential for harm.

Summarized length: 325

现在让我们尝试另一种生成方法,叫做**top_k** 采样——与考虑词汇表中所有可能的下一个词不同,模型只考虑前 'k' 个最有可能的下一个词。

这种技术有助于将模型的注意力集中在可能的延续上,并减少生成不相关或无意义文本的可能性。

它通过限制下一个词选择的池子来在创造性和连贯性之间取得平衡,但不会太多,以至于输出变得确定性。

set_seed(226012)
summarized = model.generate(**batch, do_sample=True, top_k=50)
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]
compare(content, summarized_text)
Article text length: 1427

Mozilla's "Trustworthy AI" Thinking Points look at ethical issues surrounding automated decision
making.<n>values and goals of a system should be power aware and seek to minimize harm.People
should have agency and control over their data and algorithmic outputs.<n>Developers need to
implement strong measures to protect our data and personal security.

Summarized length: 355

最后,让我们尝试**top_p** 采样——也称为核采样,是一种策略,模型只考虑累积概率超过阈值“p”的最小子集的前几个词。

与考虑固定数量词的**top_k** 不同,**top_p** 会根据下一个词的概率分布进行调整。这使其更加动态灵活。它通过允许在最有可能的词的总概率未达到“p”时选择不太可能的词来帮助创建多样化且合理的文本。

set_seed(21420041)
summarized = model.generate(**batch, do_sample=True, top_p=0.9, top_k=50)
summarized_decoded = tokenizer.batch_decode(summarized, skip_special_tokens=True)
summarized_text = summarized_decoded[0]
compare(content, summarized_text)

# saving this for later.
pegasus_summarized_text = summarized_text
Article text length: 1427

Mozilla's "Trustworthy AI" Thinking Points:.<n>People should have agency and control over their data
and algorithmic outputs.<n>Developers need to implement strong measures to protect our data and
personal security.<n>We need to mandate transparency so that we can fully understand these systems
and their potential for harm.

Summarized length: 325

要继续使用代码示例并查看另一个模型的测试,以及了解如何评估 ML 模型结果(一个全新的部分),请点击这里查看 Python 笔记本,并点击“在 Colab 中打开”来尝试你自己的自定义代码

请注意,本指南将不断更新,数据检索、图像生成和微调等新部分将陆续推出。

开发者贡献至关重要

在今天发布 Mozilla AI 指南之后不久,我们将发布我们的社区贡献指南。它将为开发者提供有关可以贡献的内容类型以及如何共享内容的指导。准备分享任何优秀的开源 AI 项目、实现、视频和音频模型。

让我们共同构建一个团结一致、协作且负责任的 AI 社区。

特别感谢 Kevin Li 和 Pradeep Elankumaran 共同完成这篇精彩的博文。

关于 Melissa Thermidor

Melissa 是 Mozilla 的高级互联网广告专家。

Melissa Thermidor 的更多文章…