高级教程:MLflow 中对 OpenAI 嵌入的支持
欢迎来到本高级指南,了解如何在 MLflow 框架中实现 OpenAI 嵌入。本教程深入探讨了 OpenAI 强大嵌入的配置和使用,这是现代机器学习模型中的一个关键组件。
理解嵌入
嵌入是一种表示学习形式,其中单词、短语甚至整个文档都转换为高维空间中的向量。这些向量捕获语义含义,使模型能够更有效地理解和处理语言。嵌入在自然语言处理 (NLP) 中广泛用于文本分类、情感分析和语言翻译等任务。
嵌入如何工作
嵌入通过将文本数据映射到向量来实现,使得向量之间的距离和方向表示单词或短语之间的关系。例如,在训练有素的嵌入空间中,同义词彼此靠近,而无关的术语则相距较远。这种空间排列允许算法识别上下文和语义,从而增强其解释和响应自然语言的能力。
本教程内容
- 嵌入端点配置:在 MLflow 中设置和利用 OpenAI 的嵌入端点。
- 实际应用:比较不同网页文本内容以确定其上下文特定内容的相似程度的实际示例。
- 效率和精度提升:使用 OpenAI 嵌入改进模型性能的技术。
在本教程结束时,您将全面了解如何在 MLflow 项目中集成和利用 OpenAI 嵌入,从而驾驭高级 NLP 技术的强大功能。您还将看到使用文档文本嵌入来比较其相似性的实际应用。此用例对于 Web 内容开发特别有用,因为它是执行搜索引擎优化 (SEO) 时的一项关键任务,以确保网站页面内容彼此不太相似(这可能导致页面排名下降)。
所需包
为了运行本教程,您需要从 PyPI 安装 beautifulsoup4
。
让我们深入了解嵌入的世界,探索它们对机器学习模型的变革性影响!
import warnings
# Disable a few less-than-useful UserWarnings from setuptools and pydantic
warnings.filterwarnings("ignore", category=UserWarning)
import os
import numpy as np
import openai
import requests
from bs4 import BeautifulSoup
from sklearn.metrics.pairwise import cosine_similarity, euclidean_distances
import mlflow
from mlflow.models.signature import ModelSignature
from mlflow.types.schema import ColSpec, ParamSchema, ParamSpec, Schema, TensorSpec
assert "OPENAI_API_KEY" in os.environ, " OPENAI_API_KEY environment variable must be set"
将 OpenAI 模型与 MLflow 集成以实现文档相似性
在本教程部分,我们将演示在 MLflow 中设置和利用 OpenAI 嵌入模型以完成文档相似性任务的过程。
关键步骤
-
设置 MLflow 实验:我们首先使用
mlflow.set_experiment("Documentation Similarity")
在 MLflow 中设置实验上下文,特别是针对文档相似性。 -
在 MLflow 中记录模型:我们启动一个 MLflow 运行,并记录元数据和访问配置参数以与特定的 OpenAI 端点通信。我们在此处选择的 OpenAI 端点指向模型“text-embedding-ada-002”,选择它是因为它具有强大的嵌入功能。在此步骤中,我们详细说明了这些访问配置、嵌入任务、输入/输出模式以及批处理大小等参数。
-
加载记录的模型以供使用:记录 MLflow 模型后,我们继续使用 MLflow 的
pyfunc
模块加载它。这是在 MLflow 生态系统中应用模型来执行文档相似性任务的关键步骤。
这些步骤对于将对 OpenAI 嵌入模型的访问集成到 MLflow 中至关重要,从而促进文档相似性分析等高级 NLP 操作。
mlflow.set_experiment("Documenatation Similarity")
with mlflow.start_run():
model_info = mlflow.openai.log_model(
model="text-embedding-ada-002",
task=openai.embeddings,
name="model",
signature=ModelSignature(
inputs=Schema([ColSpec(type="string", name=None)]),
outputs=Schema([TensorSpec(type=np.dtype("float64"), shape=(-1,))]),
params=ParamSchema([ParamSpec(name="batch_size", dtype="long", default=1024)]),
),
)
# Load the model in pyfunc format
model = mlflow.pyfunc.load_model(model_info.model_uri)
用于嵌入分析的网页文本提取
本教程的这一部分介绍了旨在从网页中提取和准备文本的函数,这是在应用嵌入模型进行分析之前的关键步骤。
函数概述
-
insert_space_after_tags:
- 在 BeautifulSoup 对象中的特定 HTML 标签后添加空格,以提高文本可读性。
-
extract_text_from_url:
- 使用其 URL 和目标 ID 从指定的网页部分提取文本。过滤并组织来自
<h>
、<li>
和<p>
等标签的文本,排除某些不相关的部分。
- 使用其 URL 和目标 ID 从指定的网页部分提取文本。过滤并组织来自
这些函数对于预处理 Web 内容至关重要,可确保输入到嵌入模型中的文本干净、相关且结构良好。
def insert_space_after_tags(soup, tags):
"""
Insert a space after each tag specified in the provided BeautifulSoup object.
Args:
soup: BeautifulSoup object representing the parsed HTML.
tags: List of tag names (as strings) after which space should be inserted.
"""
for tag_name in tags:
for tag in soup.find_all(tag_name):
tag.insert_after(" ")
def extract_text_from_url(url, id):
"""
Extract and return text content from a specific section of a webpage.
"""
try:
response = requests.get(url)
response.raise_for_status() # Raises HTTPError for bad requests (4XX, 5XX)
except requests.exceptions.RequestException as e:
return f"Request failed: {e}"
soup = BeautifulSoup(response.text, "html.parser")
target_div = soup.find("div", {"class": "section", "id": id})
if not target_div:
return "Target element not found."
insert_space_after_tags(target_div, ["strong", "a"])
content_tags = target_div.find_all(["h1", "h2", "h3", "h4", "h5", "h6", "li", "p"])
filtered_tags = [
tag
for tag in content_tags
if not (
(tag.name == "li" and tag.find("p") and tag.find("a", class_="reference external"))
or (tag.name == "p" and tag.find_parent("ul"))
or (tag.get_text(strip=True).lower() == "note")
)
]
return "
".join(tag.get_text(separator=" ", strip=True) for tag in filtered_tags)
详细工作流:
- 函数
extract_text_from_url
首先使用requests
库获取网页内容。 - 然后,它使用 BeautifulSoup 解析 HTML 内容。
- 针对特定的 HTML 标签进行文本提取,确保内容与嵌入分析相关且结构良好。
- 在
extract_text_from_url
中调用insert_space_after_tags
函数,以提高提取后的文本可读性。
测量嵌入之间的相似性和距离
在本教程的下一部分,我们利用 sklearn
中的两个函数来测量文档嵌入之间的相似性和距离,这对于评估和比较基于文本的机器学习模型至关重要。
函数概述
-
cosine_similarity:
- 目的:计算两个嵌入向量之间的余弦相似度。
- 工作原理:此函数通过查找两个向量之间角度的余弦来计算相似度,这是一种评估两个文档内容相似程度的常用方法。
- 相关性:在 NLP 中非常有用,特别是对于文档检索和聚类等任务,其目标是查找内容相似的文档。
-
euclidean_distances:
- 目的:计算两个嵌入向量之间的欧几里得距离。
- 功能:与
cosine_similarity
类似,此函数计算欧几里得距离,即嵌入空间中两点之间的“直线”距离。此度量对于理解两个文档之间的差异程度很有用。 - 在 NLP 中的相关性:提供更直观的物理距离度量,适用于文档分类和异常检测等任务。
这些函数对于分析和比较嵌入模型的输出至关重要,提供对不同文本数据在相似性和区别方面的关系的洞察。
使用嵌入比较网页
本教程的这一部分介绍了一个函数 compare_pages
,旨在利用嵌入模型比较两个网页的内容。此函数是理解给定两个网页在文本内容上有多相似或多不同的关键。
函数概述
- 函数名称:
compare_pages
- 目的:比较两个网页并根据其内容返回相似度分数。
- 参数:
url1
和url2
:要比较的网页的 URL。id1
和id2
:每个页面上主要文本内容 div 的目标 ID。
工作原理
- 文本提取:该函数首先使用
extract_text_from_url
函数从每个网页的指定部分提取文本。 - 嵌入预测:然后它使用先前加载的 OpenAI 模型为提取的文本生成嵌入。
- 相似度和距离测量:该函数计算两个嵌入的余弦相似度和欧几里得距离。这些指标提供了网页内容相似或不相似程度的可量化度量。
- 结果:返回一个包含余弦相似度分数和欧几里得距离的元组。如果文本提取失败,它会返回错误消息。
实际应用
此函数在需要比较不同网页内容的场景中特别有用,例如内容管理、抄袭检测或用于 SEO 目的的相似性分析。
通过利用嵌入和相似性度量的强大功能,compare_pages
提供了一种稳健的方法来定量评估网页内容的相似性和差异。
def compare_pages(url1, url2, id1, id2):
"""
Compare two webpages and return the similarity score.
Args:
url1: URL of the first webpage.
url2: URL of the second webpage.
id1: The target id for the div containing the main text content of the first page
id2: The target id for the div containing the main text content of the second page
Returns:
A tuple of floats representing the similarity score for cosine similarity and euclidean distance.
"""
text1 = extract_text_from_url(url1, id1)
text2 = extract_text_from_url(url2, id2)
if text1 and text2:
embedding1 = model.predict([text1])
embedding2 = model.predict([text2])
return (
cosine_similarity(embedding1, embedding2),
euclidean_distances(embedding1, embedding2),
)
else:
return "Failed to retrieve content."
MLflow 文档页面之间的相似性分析
在本教程部分,我们将演示 compare_pages
函数的实际应用,通过比较 MLflow 文档中的两个特定页面。我们的目标是评估 MLflow 2.8.1 版本中主大型语言模型 (LLM) 页面与 LLM 评估页面内容的相似程度。
过程概述
- 目标网页:
- 主 LLM 页面:MLflow 2.8.1 版本 LLM 页面
- LLM 评估页面:MLflow 2.8.1 LLM 评估
- 内容 ID:我们使用“llms”表示主 LLM 页面,使用“mlflow-llm-evaluate”表示 LLM 评估页面,以定位特定内容部分。
- 比较执行:调用
compare_pages
函数,并传入这些 URL 和内容 ID 以执行分析。
结果
- 余弦相似度与欧几里得距离:该函数返回两个关键指标
- 余弦相似度:测量两个页面嵌入向量之间角度的余弦。值越高表示相似度越大。
- 欧几里得距离:表示嵌入空间中两点之间的“直线”距离,值越低表示相似度越接近。
解释
结果显示高度的余弦相似度(0.8792),表明两个页面的内容在上下文和涵盖的主题方面非常相似。欧几里得距离为 0.4914,虽然相对较低,但提供了互补的视角,表明内容具有一定程度的独特性。
结论
此分析突出了使用嵌入和相似性指标比较网页内容的有效性。在实际应用中,它有助于理解文档中的重叠和差异,从而有助于内容优化、冗余减少和确保主题的全面覆盖。
# Get the similarity between the main LLMs page in the MLflow Docs and the LLM Evaluation page for the 2.8.1 release of MLflow
llm_cosine, llm_euclid = compare_pages(
url1="https://www.mlflow.org/docs/2.8.1/llms/index.html",
url2="https://www.mlflow.org/docs/2.8.1/llms/llm-evaluate/index.html",
id1="llms",
id2="mlflow-llm-evaluate",
)
print(
f"The cosine similarity between the LLMs page and the LLM Evaluation page is: {llm_cosine} and the euclidean distance is: {llm_euclid}"
)
The cosine similarity between the LLMs page and the LLM Evaluation page is: [[0.879243]] and the euclidean distance is: [[0.49144073]]
MLflow LLM 和插件页面相似度简要概述
本节演示了 MLflow 大型语言模型 (LLM) 页面和 2.8.1 版本插件页面之间的快速相似性分析。
分析执行
- 比较页面:
- LLM 页面:MLflow 2.8.1 版本 LLM 页面
- 插件页面:MLflow 2.8.1 版本插件页面
- 使用的 ID:LLM 页面使用“llms”,插件页面使用“mflow-plugins”。
- 函数:使用
compare_pages
进行比较。
结果
- 余弦相似度:0.6806,表示内容中度相似。
- 欧几里得距离:0.7992,表明两个页面的上下文和主题存在显著差异。
结果反映了 LLM 和插件页面之间存在中等程度的相似性,其内容具有显著的独特性。此分析有助于理解 MLflow 文档不同部分之间的关系和内容重叠。
# Get the similarity between the main LLMs page in the MLflow Docs and the Plugins page for the 2.8.1 release of MLflow
plugins_cosine, plugins_euclid = compare_pages(
url1="https://www.mlflow.org/docs/2.8.1/llms/index.html",
url2="https://www.mlflow.org/docs/2.8.1/plugins.html",
id1="llms",
id2="mflow-plugins",
)
print(
f"The cosine similarity between the LLMs page and the MLflow Projects page is: {plugins_cosine} and the euclidean distance is: {plugins_euclid}"
)
The cosine similarity between the LLMs page and the MLflow Projects page is: [[0.68062298]] and the euclidean distance is: [[0.79922088]]
教程回顾:在 MLflow 中利用 OpenAI 嵌入
在本教程结束时,让我们回顾一下我们探索的关于在 MLflow 框架中使用 OpenAI 嵌入的关键概念和技术。
主要收获
-
在 MLflow 中集成 OpenAI 模型:
- 我们学习了如何在 MLflow 中记录和加载 OpenAI 的“text-embedding-ada-002”模型,这是在机器学习工作流中利用这些嵌入的关键一步。
-
文本提取和预处理:
- 本教程介绍了从网页提取和预处理文本的方法,确保数据干净且结构化,以便进行嵌入分析。
-
计算相似度和距离:
- 我们深入研究了测量文档嵌入之间余弦相似度和欧几里得距离的函数,这对于比较文本内容至关重要。
-
实际应用:网页内容比较:
- 通过比较不同的 MLflow 文档页面,演示了这些概念的实际应用。我们使用 OpenAI 模型生成的嵌入分析了其内容的相似性和差异。
-
解释结果:
- 本教程提供了对相似度和距离指标结果的解释,突出了它们在理解内容关系方面的相关性。
结论
本高级教程旨在提高您在 MLflow 中应用 OpenAI 嵌入的技能,重点关注文档相似性分析等实际应用。通过集成这些强大的 NLP 工具,我们展示了如何从文本数据中提取更多价值和见解,这是现代机器学习项目的关键方面。
我们希望本指南能为您在 MLflow 框架中理解和应用 OpenAI 嵌入提供信息和帮助。
下一步是什么?
要继续您的学习之旅,请参阅有关 MLflow 的 OpenAI 风味的更多高级教程。