MLflow DSPy 风味
dspy
风味正在积极开发中,并标记为实验性。公共 API 可能会发生变化,并且随着该风味的发展可能会添加新功能。
简介
DSPy 是一个用于通过算法优化 LM(语言模型)提示和权重的框架。它旨在通过将手工制作的提示字符串替换为模块化组件来改进提示工程过程。这些模块简洁、定义明确,并保持高质量和表达力,从而使提示创建更高效、更具可伸缩性。通过参数化这些模块并将提示视为一个优化问题,DSPy 可以更好地适应不同的语言模型,可能比专家制作的提示表现更好。这种模块化还使得更容易探索复杂的管道,从而可以根据特定任务或细微指标微调性能。
为什么将 DSPy 与 MLflow 一起使用?
DSPy 库与 MLflow 的原生集成帮助用户管理 DSPy 的开发生命周期。以下是将 DSPy 与 MLflow 一起使用的一些主要优势:
- MLflow Tracking 允许您跟踪 DSPy 程序的训练和执行。借助 MLflow API,您可以记录各种工件并组织训练运行,从而提高模型性能的可见性。
- MLflow Model 将编译后的 DSPy 程序与其依赖版本、输入和输出接口以及其他基本元数据打包在一起。这使您能够轻松部署编译后的 DSPy 程序,知道环境在 ML 生命周期的不同阶段保持一致。
- MLflow Evaluate 在 MLflow 中提供了评估生成式 AI 应用程序的原生功能。此功能有助于对编译后的 DSPy 程序的推理结果进行高效评估,确保强大的性能分析并促进快速迭代。
- MLflow Tracing 是一个强大的可观测性工具,用于监控和调试 DSPy 模型内部发生的情况,帮助您快速识别潜在瓶颈或问题。凭借其强大的自动日志记录功能,您无需添加任何代码,只需运行一个命令即可检测您的 DSPy 应用程序。
入门
在本入门教程中,您将学习 DSPy 的最基本组件以及如何利用与 MLflow 的集成来存储、检索和使用 DSPy 程序。
概念
模块(Module)
模块(Modules)是处理特定文本转换的组件,例如回答问题或进行摘要。它们取代了传统的、手工编写的提示,并且可以从示例中学习,使其更具适应性。
签名(Signature)
签名(signature)是模块输入和输出行为的自然语言描述。例如,“question -> answer”指定模块应将问题作为输入并返回答案。
优化器(Optimizer)
优化器(optimizer)通过调整模块以满足性能指标来改进 LM 管道,这可以通过生成更好的提示或微调模型来实现。
程序(Program)
程序是一组连接成管道以执行复杂任务的模块。DSPy 程序灵活,允许您使用编译器对其进行优化和调整。
自动追踪
MLflow Tracing 追踪是一项强大的功能,允许您监控和调试 DSPy 程序。借助 MLflow,您只需在代码中调用 mlflow.dspy.autolog()
函数即可启用自动追踪。
import mlflow
mlflow.dspy.autolog()
启用后,每当执行 DSPy 程序时,MLflow 都会生成追踪,并将其记录在您的 MLflow 实验中。
在此处了解更多关于 MLflow DSPy 追踪功能的信息。
在 MLflow 实验中跟踪 DSPy 程序
创建 DSPy 程序
Module 对象是 DSPy 和 MLflow 集成的核心。使用 DSPy,您可以通过一个或一组模块创建复杂的代理逻辑。
pip install mlflow dspy -U
import dspy
# Define our language model
lm = dspy.LM(model="openai/gpt-4o-mini", max_tokens=250)
dspy.settings.configure(lm=lm)
# Define a Chain of Thought module
class CoT(dspy.Module):
def __init__(self):
super().__init__()
self.prog = dspy.ChainOfThought("question -> answer")
def forward(self, question):
return self.prog(question=question)
dspy_model = CoT()
将程序记录到 MLflow
您可以使用 mlflow.dspy.log_model()
函数将 dspy.Module
对象记录到 MLflow 运行中。
我们还将指定模型签名。MLflow 模型签名定义了模型输入和输出的预期模式,确保模型推理期间的一致性和正确性。
import mlflow
# Start an MLflow run
with mlflow.start_run():
# Log the model
model_info = mlflow.dspy.log_model(
dspy_model,
artifact_path="model",
input_example="what is 2 + 2?",
)
加载模块进行推理
可以使用 mlflow.pyfunc.load_model()
函数加载保存的模块以进行推理。此函数返回一个由 DSPy 模块支持的 MLflow Python 模型。
import mlflow
# Load the model as an MLflow PythonModel
model = mlflow.pyfunc.load_model(model_info.model_uri)
# Predict with the object
response = model.predict("What kind of bear is best?")
print(response)
{
"reasoning": """The question "What kind of bear is best?" is often associated with a
humorous reference from the television show "The Office," where the character Jim
Halpert jokingly states, "Bears, beets, Battlestar Galactica." However, if we consider
the question seriously, it depends on the context. Different species of bears have
different characteristics and adaptations that make them "best" in various ways.
For example, the American black bear is known for its adaptability, while the polar bear is
the largest land carnivore and is well adapted to its Arctic environment. Ultimately, the
answer can vary based on personal preference or specific criteria such as strength,
intelligence, or adaptability.""",
"answer": """There isn\'t a definitive answer, as it depends on the context. However, many
people humorously refer to the American black bear or the polar bear when discussing
"the best" kind of bear.""",
}
要直接加载 DSPy 程序本身而不是 PyFunc 包装的模型,请使用 mlflow.dspy.load_model()
函数。
model = mlflow.dspy.load_model(model_uri)
优化器自动日志记录
MLflow DSPy 风味支持 DSPy 优化器的自动日志记录。有关详细信息,请参阅优化器自动日志记录页面。
常见问题
如何保存编译模型与未编译模型?
DSPy 通过更新各种 LLM 参数(例如提示、超参数和模型权重)来优化训练,从而编译模型。虽然 MLflow 允许记录编译和未编译的模型,但通常最好使用编译模型,因为它在实践中预期表现更好。
MLflow 可以序列化什么?
在 MLflow 中使用 mlflow.dspy.log_model()
或 mlflow.dspy.save_model()
时,DSPy 程序会被序列化并作为 .pkl
文件保存到跟踪服务器。这使得部署变得容易。在底层,MLflow 使用 cloudpickle
来序列化 DSPy 对象,但某些 DSPy 工件无法序列化。相关示例如下。
- API 令牌。这些应单独管理,并通过环境变量安全传递。
- DSPy 追踪对象,主要在训练期间使用,而不是推理期间。
如何管理机密信息?
使用 MLflow DSPy 风味进行序列化时,令牌会从设置对象中删除。用户有责任将所需的机密信息安全地传递到部署环境。
DSPy 的 settings
对象是如何保存的?
为确保程序的可重现性,服务上下文被转换为 Python 字典并与模型工件一起进行 pickle 序列化。服务上下文是一个在生成式 AI 框架中流行的概念。简单来说,它存储了项目全局的配置。对于 DSPy 特别是,我们可以设置语言模型、重排序器、适配器等信息。
DSPy 将此服务上下文存储在 Settings
单例类中。在 Settings
对象中设置的敏感 API 访问密钥在记录模型时不会持久化。部署 DSPy 模型时,必须确保部署环境已设置这些密钥,以便 DSPy 模型可以向需要访问密钥的服务进行远程调用。