跳到主要内容

使用 Sentence Transformers 和 MLflow 进行高级释义挖掘

下载此笔记本

使用Sentence Transformers和MLflow踏上高级释义挖掘的丰富旅程。

学习目标

  • 应用sentence-transformers进行高级释义挖掘。
  • 在MLflow中开发为该任务量身定制的自定义PythonModel
  • 在MLflow生态系统中有效地管理和跟踪模型。
  • 利用MLflow的部署功能部署释义挖掘模型。

探索释义挖掘

探索识别语义相似但文本不同的句子的过程,这是文档摘要和聊天机器人开发等各种NLP应用的关键方面。

Sentence Transformers在释义挖掘中的作用

了解Sentence Transformers,专门用于生成丰富的句子嵌入,如何用于捕捉深层语义含义和比较文本内容。

MLflow:简化模型管理和部署

深入了解MLflow如何简化NLP模型的管理和部署过程,重点关注高效跟踪和可定制的模型实现。

加入我们,深入了解释义挖掘,并掌握使用MLflow管理和部署NLP模型的艺术。

import warnings

# Disable a few less-than-useful UserWarnings from setuptools and pydantic
warnings.filterwarnings("ignore", category=UserWarning)

释义挖掘模型简介

启动释义挖掘模型,集成Sentence Transformers和MLflow以完成高级NLP任务。

模型结构概述

  • 加载模型和语料库load_context方法:加载Sentence Transformer模型和用于释义识别的文本语料库的必备方法。
  • 释义挖掘逻辑predict方法:集成用于输入验证和释义挖掘的自定义逻辑,提供可自定义的参数。
  • 排序和过滤匹配项_sort_and_filter_matches辅助方法:通过基于相似度分数进行排序和过滤,确保相关且独特的释义识别。

主要特性

  • 高级NLP技术:利用Sentence Transformers进行语义文本理解。
  • 自定义逻辑集成:展示模型行为自定义的灵活性。
  • 用户自定义选项:允许最终用户根据各种用例调整匹配标准。
  • 处理效率:预编码语料库以实现高效的释义挖掘操作。
  • 强大的错误处理:结合验证以实现可靠的模型性能。

实际意义

该模型为各种应用中的释义检测提供了强大的工具,展示了在MLflow框架中有效使用自定义模型。

import warnings

import pandas as pd
from sentence_transformers import SentenceTransformer, util

import mlflow
from mlflow.models.signature import infer_signature
from mlflow.pyfunc import PythonModel


class ParaphraseMiningModel(PythonModel):
def load_context(self, context):
"""Load the model context for inference, including the customer feedback corpus."""
try:
# Load the pre-trained sentence transformer model
self.model = SentenceTransformer.load(context.artifacts["model_path"])

# Load the customer feedback corpus from the specified file
corpus_file = context.artifacts["corpus_file"]
with open(corpus_file) as file:
self.corpus = file.read().splitlines()

except Exception as e:
raise ValueError(f"Error loading model and corpus: {e}")

def _sort_and_filter_matches(
self,
query: str,
paraphrase_pairs: list[tuple[float, int, int]],
similarity_threshold: float,
):
"""Sort and filter the matches by similarity score."""

# Convert to list of tuples and sort by score
sorted_matches = sorted(paraphrase_pairs, key=lambda x: x[1], reverse=True)

# Filter and collect paraphrases for the query, avoiding duplicates
query_paraphrases = {}
for score, i, j in sorted_matches:
if score < similarity_threshold:
continue

paraphrase = self.corpus[j] if self.corpus[i] == query else self.corpus[i]
if paraphrase == query:
continue

if paraphrase not in query_paraphrases or score > query_paraphrases[paraphrase]:
query_paraphrases[paraphrase] = score

return sorted(query_paraphrases.items(), key=lambda x: x[1], reverse=True)

def predict(self, context, model_input, params=None):
"""Predict method to perform paraphrase mining over the corpus."""

# Validate and extract the query input
if isinstance(model_input, pd.DataFrame):
if model_input.shape[1] != 1:
raise ValueError("DataFrame input must have exactly one column.")
query = model_input.iloc[0, 0]
elif isinstance(model_input, dict):
query = model_input.get("query")
if query is None:
raise ValueError("The input dictionary must have a key named 'query'.")
else:
raise TypeError(
f"Unexpected type for model_input: {type(model_input)}. Must be either a Dict or a DataFrame."
)

# Determine the minimum similarity threshold
similarity_threshold = params.get("similarity_threshold", 0.5) if params else 0.5

# Add the query to the corpus for paraphrase mining
extended_corpus = self.corpus + [query]

# Perform paraphrase mining
paraphrase_pairs = util.paraphrase_mining(
self.model, extended_corpus, show_progress_bar=False
)

# Convert to list of tuples and sort by score
sorted_paraphrases = self._sort_and_filter_matches(
query, paraphrase_pairs, similarity_threshold
)

# Warning if no paraphrases found
if not sorted_paraphrases:
warnings.warn("No paraphrases found above the similarity threshold.", UserWarning)

return {sentence[0]: str(sentence[1]) for sentence in sorted_paraphrases}

准备用于释义挖掘的语料库

通过创建和准备多样化的语料库,为释义挖掘奠定基础。

语料库创建

  • 定义一个包含来自各种主题(包括太空探索、人工智能、园艺等)的句子的语料库。这种多样性使模型能够识别跨广泛主题的释义。

将语料库写入文件

  • 语料库保存到名为feedback.txt的文件中,这反映了大规模数据处理中的常见做法。
  • 此步骤还准备语料库,以便在释义挖掘模型中进行高效处理。

语料库的重要性

语料库是模型查找语义相似句子的关键数据集。它的多样性确保了模型在不同用例中的适应性和有效性。

corpus = [
"Exploring ancient cities in Europe offers a glimpse into history.",
"Modern AI technologies are revolutionizing industries.",
"Healthy eating contributes significantly to overall well-being.",
"Advancements in renewable energy are combating climate change.",
"Learning a new language opens doors to different cultures.",
"Gardening is a relaxing hobby that connects you with nature.",
"Blockchain technology could redefine digital transactions.",
"Homemade Italian pasta is a delight to cook and eat.",
"Practicing yoga daily improves both physical and mental health.",
"The art of photography captures moments in time.",
"Baking bread at home has become a popular quarantine activity.",
"Virtual reality is creating new experiences in gaming.",
"Sustainable travel is becoming a priority for eco-conscious tourists.",
"Reading books is a great way to unwind and learn.",
"Jazz music provides a rich tapestry of sound and rhythm.",
"Marathon training requires discipline and perseverance.",
"Studying the stars helps us understand our universe.",
"The rise of electric cars is an important environmental development.",
"Documentary films offer deep insights into real-world issues.",
"Crafting DIY projects can be both fun and rewarding.",
"The history of ancient civilizations is fascinating to explore.",
"Exploring the depths of the ocean reveals a world of marine wonders.",
"Learning to play a musical instrument can be a rewarding challenge.",
"Artificial intelligence is shaping the future of personalized medicine.",
"Cycling is not only a great workout but also eco-friendly transportation.",
"Home automation with IoT devices is enhancing living experiences.",
"Understanding quantum computing requires a grasp of complex physics.",
"A well-brewed cup of coffee is the perfect start to the day.",
"Urban farming is gaining popularity as a sustainable food source.",
"Meditation and mindfulness can lead to a more balanced life.",
"The popularity of podcasts has revolutionized audio storytelling.",
"Space exploration continues to push the boundaries of human knowledge.",
"Wildlife conservation is essential for maintaining biodiversity.",
"The fusion of technology and fashion is creating new trends.",
"E-learning platforms have transformed the educational landscape.",
"Dark chocolate has surprising health benefits when enjoyed in moderation.",
"Robotics in manufacturing is leading to more efficient production.",
"Creating a personal budget is key to financial well-being.",
"Hiking in nature is a great way to connect with the outdoors.",
"3D printing is innovating the way we create and manufacture objects.",
"Sommeliers can identify a wine's characteristics with just a taste.",
"Mind-bending puzzles and riddles are great for cognitive exercise.",
"Social media has a profound impact on communication and culture.",
"Urban sketching captures the essence of city life on paper.",
"The ethics of AI is a growing field in tech philosophy.",
"Homemade skincare remedies are becoming more popular.",
"Virtual travel experiences can provide a sense of adventure at home.",
"Ancient mythology still influences modern storytelling and literature.",
"Building model kits is a hobby that requires patience and precision.",
"The study of languages opens windows into different worldviews.",
"Professional esports has become a major global phenomenon.",
"The mysteries of the universe are unveiled through space missions.",
"Astronauts' experiences in space stations offer unique insights into life beyond Earth.",
"Telescopic observations bring distant galaxies within our view.",
"The study of celestial bodies helps us understand the cosmos.",
"Space travel advancements could lead to interplanetary exploration.",
"Observing celestial events provides valuable data for astronomers.",
"The development of powerful rockets is key to deep space exploration.",
"Mars rover missions are crucial in searching for extraterrestrial life.",
"Satellites play a vital role in our understanding of Earth's atmosphere.",
"Astrophysics is central to unraveling the secrets of space.",
"Zero gravity environments in space pose unique challenges and opportunities.",
"Space tourism might soon become a reality for many.",
"Lunar missions have contributed significantly to our knowledge of the moon.",
"The International Space Station is a hub for groundbreaking space research.",
"Studying comets and asteroids reveals information about the early solar system.",
"Advancements in space technology have implications for many scientific fields.",
"The possibility of life on other planets continues to intrigue scientists.",
"Black holes are among the most mysterious phenomena in space.",
"The history of space exploration is filled with remarkable achievements.",
"Future space missions could unlock the mysteries of dark matter.",
]

# Write out the corpus to a file
corpus_file = "/tmp/feedback.txt"
with open(corpus_file, "w") as file:
for sentence in corpus:
file.write(sentence + "
")

设置释义挖掘模型

准备Sentence Transformer模型,以便与MLflow集成,以利用其释义挖掘功能。

加载Sentence Transformer模型

  • 初始化all-MiniLM-L6-v2 Sentence Transformer模型,该模型非常适合生成适用于释义挖掘的句子嵌入。

准备输入示例

  • 创建DataFrame作为输入示例,以说明模型将处理的查询类型,有助于定义模型的输入结构。

保存模型

  • 将模型保存到/tmp/paraphrase_search_model,以便在与MLflow部署时方便移植和加载。

定义工件和语料库路径

  • 将保存的模型和语料库的路径指定为MLflow中的工件,这对于模型日志记录和再现至关重要。

生成用于签名的测试输出

  • 生成示例输出,说明模型预期的释义挖掘输出格式。

创建模型签名

  • 使用MLflow的infer_signature定义模型的输入和输出模式,并添加similarity_threshold参数以提高推理灵活性。
# Load a pre-trained sentence transformer model
model = SentenceTransformer("all-MiniLM-L6-v2")

# Create an input example DataFrame
input_example = pd.DataFrame({"query": ["This product works well. I'm satisfied."]})

# Save the model in the /tmp directory
model_directory = "/tmp/paraphrase_search_model"
model.save(model_directory)

# Define the path for the corpus file
corpus_file = "/tmp/feedback.txt"

# Define the artifacts (paths to the model and corpus file)
artifacts = {"model_path": model_directory, "corpus_file": corpus_file}

# Generate test output for signature
# Sample output for paraphrase mining could be a list of tuples (paraphrase, score)
test_output = [{"This product is satisfactory and functions as expected.": "0.8"}]

# Define the signature associated with the model
# The signature includes the structure of the input and the expected output, as well as any parameters that
# we would like to expose for overriding at inference time (including their default values if they are not overridden).
signature = infer_signature(
model_input=input_example, model_output=test_output, params={"similarity_threshold": 0.5}
)

# Visualize the signature, showing our overridden inference parameter and its default.
signature
inputs: 
['query': string]
outputs: 
['This product is satisfactory and functions as expected.': string]
params: 
['similarity_threshold': double (default: 0.5)]

创建实验

我们创建一个新的 MLflow 实验,以便我们要将模型记录到的运行不会记录到默认实验,而是具有其自己的上下文相关条目。

# If you are running this tutorial in local mode, leave the next line commented out.
# Otherwise, uncomment the following line and set your tracking uri to your local or remote tracking server.

# mlflow.set_tracking_uri("http://127.0.0.1:8080")

mlflow.set_experiment("Paraphrase Mining")
<Experiment: artifact_location='file:///Users/benjamin.wilson/repos/mlflow-fork/mlflow/docs/source/llms/sentence-transformers/tutorials/paraphrase-mining/mlruns/380691166097743403', creation_time=1701282619556, experiment_id='380691166097743403', last_update_time=1701282619556, lifecycle_stage='active', name='Paraphrase Mining', tags={}>

使用MLflow记录释义挖掘模型

使用MLflow记录自定义释义挖掘模型,这是模型管理和部署的关键步骤。

启动MLflow运行

  • 启动MLflow运行以在MLflow框架内创建模型日志记录和跟踪的全面记录。

在 MLflow 中记录模型

  • 使用MLflow的Python模型日志记录函数将自定义模型集成到MLflow生态系统中。
  • 为模型提供唯一的名称,以便在MLflow中轻松识别。
  • 记录已实例化的释义挖掘模型,以及输入示例、模型签名、工件和Python依赖项。

模型日志记录的结果和好处

  • 在MLflow中注册模型,以实现简化管理和部署,从而提高其可访问性和可跟踪性。
  • 确保跨部署环境的模型可重现性和版本控制。
with mlflow.start_run() as run:
model_info = mlflow.pyfunc.log_model(
name="paraphrase_model",
python_model=ParaphraseMiningModel(),
input_example=input_example,
signature=signature,
artifacts=artifacts,
pip_requirements=["sentence_transformers"],
)
Downloading artifacts:   0%|          | 0/11 [00:00<?, ?it/s]
2023/11/30 15:41:39 INFO mlflow.store.artifact.artifact_repo: The progress bar can be disabled by setting the environment variable MLFLOW_ENABLE_ARTIFACTS_PROGRESS_BAR to false
Downloading artifacts:   0%|          | 0/1 [00:00<?, ?it/s]

模型加载和释义挖掘预测

通过使用MLflow加载模型并执行预测,说明释义挖掘模型的实际应用。

加载模型进行推理

  • 利用MLflow的load_model函数检索和准备模型以进行推理。
  • 在MLflow注册表中使用其唯一URI定位和加载模型。

执行释义挖掘预测

  • 使用模型的predict方法进行预测,应用嵌入在模型类中的释义挖掘逻辑。
  • 传递具有设定的similarity_threshold的代表性查询,以在语料库中查找匹配的释义。

解释模型输出

  • 查看与查询语义相似的句子列表,突出显示模型的释义识别能力。
  • 分析相似度分数,以了解查询和语料库句子之间的语义关联程度。

结论

此演示验证了释义挖掘模型在实际场景中的有效性,强调了其在内容推荐、信息检索和对话式AI中的实用性。

# Load our model by supplying the uri that was used to save the model artifacts
loaded_dynamic = mlflow.pyfunc.load_model(model_info.model_uri)

# Perform a quick validation that our loaded model is performing adequately
loaded_dynamic.predict(
{"query": "Space exploration is fascinating."}, params={"similarity_threshold": 0.65}
)
{'Studying the stars helps us understand our universe.': '0.8207424879074097',
'The history of space exploration is filled with remarkable achievements.': '0.7770636677742004',
'Exploring ancient cities in Europe offers a glimpse into history.': '0.7461957335472107',
'Space travel advancements could lead to interplanetary exploration.': '0.7090306282043457',
'Space exploration continues to push the boundaries of human knowledge.': '0.6893945932388306',
'The mysteries of the universe are unveiled through space missions.': '0.6830739974975586',
'The study of celestial bodies helps us understand the cosmos.': '0.671358048915863'}

结论:见解和潜在增强

在本教程结束之际,让我们回顾一下使用Sentence Transformers和MLflow实现释义挖掘模型的过程。我们成功地构建并部署了一个能够识别语义相似句子的模型,展示了MLflow PythonModel实现的灵活性和强大功能。

主要收获

  • 我们学习了如何将高级NLP技术(特别是释义挖掘)与MLflow集成。这种集成不仅增强了模型管理,还简化了部署和可伸缩性。
  • MLflow中PythonModel实现的灵活性是核心主题。我们亲身看到了它如何允许将自定义逻辑合并到模型的预测函数中,以适应像释义挖掘这样的特定NLP任务。
  • 通过我们的自定义模型,我们探索了句子嵌入、语义相似性和语言理解的动态。这种理解在从内容推荐到对话式AI的各种应用中至关重要。

增强释义挖掘模型的想法

虽然我们的模型是一个强大的起点,但可以在predict函数中进行一些增强,使其更强大和功能更丰富

  1. 上下文过滤器:引入基于上下文线索或特定关键字的过滤器,以进一步细化搜索结果。此功能将允许用户将释义缩小到与其特定上下文或主题最相关的那些。

  2. 情感分析集成:结合情感分析,根据情感基调对释义进行分组。这在客户反馈分析等应用中特别有用,其中理解情感与内容同等重要。

  3. 多语言支持:扩展模型以支持多语言中的释义挖掘。此增强功能将显著拓宽模型在全球或多语言环境中的适用性。

通过向量数据库实现可伸缩性

  • 除了作为语料库的静态文本文件之外,更具可伸缩性和实际意义的方法是,将模型连接到外部向量数据库或内存存储。
  • 预计算的嵌入可以存储并在此类数据库中更新,以适应实时内容生成,而无需重新部署模型。这种方法将显著提高模型在实际应用中的可伸缩性和响应能力。

最终想法

构建和部署释义挖掘模型的旅程既富有启发性又实用。我们已经看到MLflow的PythonModel如何为创建自定义NLP解决方案提供灵活的画布,以及如何利用句子转换器深入研究语言语义。

本教程只是一个开始。释义挖掘和整个NLP领域还有巨大的探索和创新潜力。我们鼓励您在此基础上进行构建,尝试增强功能,并继续突破MLflow和高级NLP技术的可能性边界。