mlflow.llama_index
- mlflow.llama_index.autolog(log_traces: bool = True, disable: bool = False, silent: bool = False)[source]
启用(或禁用)并配置 LlamaIndex 到 MLflow 的自动日志记录。目前,MLflow 只支持追踪的自动日志记录。
- 参数
log_traces – 如果为
True,则会记录 LlamaIndex 模型的追踪信息。如果为False,则在推理过程中不收集追踪信息。默认为True。disable – 如果为
True,则禁用 LlamaIndex 自动日志记录集成。如果为False,则启用 LlamaIndex 自动日志记录集成。silent – 如果为
True,则在 LlamaIndex 自动日志记录期间抑制 MLflow 的所有事件日志和警告。如果为False,则显示所有事件和警告。
- mlflow.llama_index.load_model(model_uri, dst_path=None)[source]
从本地文件或运行中加载 LlamaIndex 索引/引擎/工作流。
- 参数
model_uri –
MLflow 模型在 URI 格式中的位置。例如:
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmlflow-artifacts:/path/to/model
有关支持的 URI 方案的更多信息,请参阅 引用 Artifacts。
dst_path – 用于下载模型工件的本地文件系统路径。如果提供了该目录,则该目录必须已存在。如果未指定,将创建一个本地输出路径。
- 返回
一个 LlamaIndex 索引对象。
- mlflow.llama_index.log_model(llama_index_model, artifact_path: str | None = None, engine_type: str | None = None, model_config: dict[str, typing.Any] | None = None, code_paths: list[str] | None = None, registered_model_name: str | None = None, signature: mlflow.models.signature.ModelSignature | None = None, input_example: Optional[Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple]] = None, await_registration_for=300, pip_requirements: list[str] | str | None = None, extra_pip_requirements: list[str] | str | None = None, conda_env=None, metadata: dict[str, typing.Any] | None = None, prompts: list[str | Prompt] | None = None, name: str | None = None, params: dict[str, typing.Any] | None = None, tags: dict[str, typing.Any] | None = None, model_type: str | None = None, step: int = 0, model_id: str | None = None, **kwargs)[source]
将 LlamaIndex 模型记录为当前运行的 MLflow 工件。
注意
仅支持在“代码模型”保存模式下保存非索引对象。有关更多信息,请参阅代码模型指南。
注意
记录模型时,MLflow 会自动保存
Settings对象的当前状态,以便您可以在推理时使用相同的设置。但是,请注意,Settings对象中的某些信息将不会被保存,包括:用于避免密钥泄露的 API 密钥。
不可序列化的函数对象。
- 参数
llama_index_model –
要保存的 LlamaIndex 对象。支持的模型类型包括:
一个 Index 对象。
一个 Engine 对象,例如 ChatEngine、QueryEngine、Retriever。
一个Workflow对象。
- 一个字符串,表示包含 LlamaIndex 模型定义的脚本的路径
上述类型之一。
artifact_path – Deprecated. Use name instead.
engine_type –
保存 Index 对象时必需,用于确定加载为 pyfunc 模型时的索引的推理接口。其他 LlamaIndex 对象保存时不需要此字段。支持的值如下:
"chat": 将索引加载为 LlamaIndex ChatEngine 的实例。"query": 将索引加载为 LlamaIndex QueryEngine 的实例。"retriever": 将索引加载为 LlamaIndex Retriever 的实例。
model_config –
使用
mlflow.pyfunc.load_model()加载模型时要应用的模型配置。它将根据模型类型和保存方法以不同的方式应用。对于直接保存的内存中 Index 对象,它将在记录时作为关键字参数传递,以使用指定的 engine_type 来实例化 LlamaIndex 引擎。
with mlflow.start_run() as run: model_info = mlflow.llama_index.log_model( index, name="index", engine_type="chat", model_config={"top_k": 10}, ) # When loading back, MLflow will call ``index.as_chat_engine(top_k=10)`` engine = mlflow.pyfunc.load_model(model_info.model_uri)
对于使用代码模型 <https://www.mlflow.org/docs/latest/model/models-from-code.html>方法保存的其他模型类型,配置将通过模型代码中的 :py:class`~mlflow.models.ModelConfig` 对象进行访问。
with mlflow.start_run() as run: model_info = mlflow.llama_index.log_model( "model.py", name="model", model_config={"qdrant_host": "localhost", "qdrant_port": 6333}, )
model.py
import mlflow from llama_index.vector_stores.qdrant import QdrantVectorStore import qdrant_client # The model configuration is accessible via the ModelConfig singleton model_config = mlflow.models.ModelConfig() qdrant_host = model_config.get("top_k", 5) qdrant_port = model_config.get("qdrant_port", 6333) client = qdrant_client.Client(host=qdrant_host, port=qdrant_port) vectorstore = QdrantVectorStore(client) # the rest of the model definition...
code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.registered_model_name – 如果提供,则在
registered_model_name下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。signature – 一个 Model Signature 对象,描述模型的输入和输出模式。可以使用mlflow.models.signature的
infer_signature函数推断模型签名。input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。await_registration_for – 等待模型版本完成创建并处于
READY状态的秒数。默认情况下,函数等待五分钟。指定 0 或 None 可跳过等待。pip_requirements – 要么是 pip 要求字符串的可迭代对象(例如
["llama_index", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如"requirements.txt")。如果提供,这将描述模型应在其中运行的环境。如果为None,则由当前软件环境中的mlflow.models.infer_pip_requirements()推断默认要求列表。如果要求推断失败,则回退使用 get_default_pip_requirements。要求和约束都会被自动解析并分别写入模型的一部分requirements.txt和constraints.txt文件,并存储为模型的一部分。要求也会被写入模型 conda 环境 (conda.yaml) 文件的pip部分。extra_pip_requirements –
要么是 pip 要求字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如"requirements.txt")。如果提供,这将描述追加到根据用户当前软件环境自动生成的默认 pip 要求集中的其他 pip 要求。要求和约束都会被自动解析并分别写入模型的一部分requirements.txt和constraints.txt文件,并存储为模型的一部分。要求也会被写入模型 conda 环境 (conda.yaml) 文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。conda_env –
要么是 Conda 环境的字典表示形式,要么是 conda 环境 yaml 文件的路径。如果提供,这将描述模型应在其中运行的环境。至少,它应该指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个通过mlflow.models.infer_pip_requirements()推断的 pip 要求的 conda 环境添加到模型中。如果要求推断失败,则回退使用 get_default_pip_requirements。来自conda_env的 pip 要求会被写入requirements.txt文件,而完整的 conda 环境会被写入conda.yaml。以下是一个 Conda 环境的字典表示的*示例*{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "llama_index==x.y.z" ], }, ], }
metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。
prompts –
与模型关联的 MLflow 提示注册表中已注册的提示 URI 列表。每个提示 URI 格式应为
prompt:/<name>/<version>。应在将提示与模型关联之前在 MLflow 提示注册表中注册这些提示。这将创建模型和提示之间的双向链接。关联的提示可以在 MLmodel 文件中存储的模型元数据中看到。从提示注册表 UI,您也可以导航到模型。
import mlflow prompt_template = "Hi, {name}! How are you doing today?" # Register a prompt in the MLflow Prompt Registry mlflow.prompts.register_prompt("my_prompt", prompt_template, description="A simple prompt") # Log a model with the registered prompt with mlflow.start_run(): model_info = mlflow.pyfunc.log_model( name=MyModel(), name="model", prompts=["prompt:/my_prompt/1"] ) print(model_info.prompts) # Output: ['prompt:/my_prompt/1'] # Load the prompt prompt = mlflow.genai.load_prompt(model_info.prompts[0])
name – 模型名称。
params – 要与模型一起记录的参数字典。
tags – 要与模型一起记录的标签字典。
model_type – 模型的类型。
step – 记录模型输出和指标的步骤
model_id – 模型的 ID。
kwargs –
mlflow.models.model.Model的其他参数
- mlflow.llama_index.save_model(llama_index_model, path: str, engine_type: str | None = None, model_config: str | dict[str, typing.Any] | None = None, code_paths=None, mlflow_model: mlflow.models.model.Model | None = None, signature: mlflow.models.signature.ModelSignature | None = None, input_example: Optional[Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple]] = None, pip_requirements: list[str] | str | None = None, extra_pip_requirements: list[str] | str | None = None, conda_env=None, metadata: dict[str, typing.Any] | None = None) None[source]
将 LlamaIndex 模型保存到本地文件系统上的路径。
注意
仅支持在“代码模型”保存模式下保存非索引对象。有关更多信息,请参阅代码模型指南。
注意
记录模型时,MLflow 会自动保存
Settings对象的当前状态,以便您可以在推理时使用相同的设置。但是,请注意,Settings对象中的某些信息将不会被保存,包括:用于避免密钥泄露的 API 密钥。
不可序列化的函数对象。
- 参数
llama_index_model –
要保存的 LlamaIndex 对象。支持的模型类型包括:
一个 Index 对象。
一个 Engine 对象,例如 ChatEngine、QueryEngine、Retriever。
一个Workflow对象。
- 一个字符串,表示包含 LlamaIndex 模型定义的脚本的路径
上述类型之一。
path – 要保存序列化模型(YAML 格式)的本地路径。
engine_type –
保存 Index 对象时必需,用于确定加载为 pyfunc 模型时的索引的推理接口。其他 LlamaIndex 对象保存时不需要此字段。支持的值如下:
"chat": 将索引加载为 LlamaIndex ChatEngine 的实例。"query": 将索引加载为 LlamaIndex QueryEngine 的实例。"retriever": 将索引加载为 LlamaIndex Retriever 的实例。
model_config – 使用
mlflow.pyfunc.load_model()加载模型时要应用的模型配置。它将根据模型类型和保存方法以不同的方式应用。有关更多详细信息和用法示例,请参阅log_model()的文档字符串。code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.mlflow_model – 一个 MLflow 模型对象,指定该模型正在添加到的 flavor。
signature – 一个 Model Signature 对象,描述模型的输入和输出模式。可以使用
mlflow.models.signature的infer_signature函数推断模型签名。input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。pip_requirements – 要么是 pip 要求字符串的可迭代对象(例如
["llama_index", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如"requirements.txt")。如果提供,这将描述模型应在其中运行的环境。如果为None,则由当前软件环境中的mlflow.models.infer_pip_requirements()推断默认要求列表。如果要求推断失败,则回退使用 get_default_pip_requirements。要求和约束都会被自动解析并分别写入模型的一部分requirements.txt和constraints.txt文件,并存储为模型的一部分。要求也会被写入模型 conda 环境 (conda.yaml) 文件的pip部分。extra_pip_requirements –
要么是 pip 要求字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如"requirements.txt")。如果提供,这将描述追加到根据用户当前软件环境自动生成的默认 pip 要求集中的其他 pip 要求。要求和约束都会被自动解析并分别写入模型的一部分requirements.txt和constraints.txt文件,并存储为模型的一部分。要求也会被写入模型 conda 环境 (conda.yaml) 文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。conda_env –
要么是 Conda 环境的字典表示形式,要么是 conda 环境 yaml 文件的路径。如果提供,这将描述模型应在其中运行的环境。至少,它应该指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个通过mlflow.models.infer_pip_requirements()推断的 pip 要求的 conda 环境添加到模型中。如果要求推断失败,则回退使用 get_default_pip_requirements。来自conda_env的 pip 要求会被写入requirements.txt文件,而完整的 conda 环境会被写入conda.yaml。以下是一个 Conda 环境的字典表示的*示例*{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "llama_index==x.y.z" ], }, ], }
metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。