mlflow.sentence_transformers
- mlflow.sentence_transformers.get_default_conda_env()[源代码]
- 返回
使用
sentence_transformers风格生成的 MLflow 模型的默认 Conda 环境。
- mlflow.sentence_transformers.get_default_pip_requirements() list[str][源代码]
检索
sentence_transformers风格的最小依赖项集合。- 返回
使用
sentence-transformers风格生成的 MLflow 模型的默认 pip 要求列表。对save_model()和log_model()的调用会生成一个最少包含这些要求的 pip 环境。
- mlflow.sentence_transformers.load_model(model_uri: str, dst_path: Optional[str] = None)[源代码]
注意
'sentence_transformers'MLflow 模型集成已知与2.3.1<=sentence-transformers<=5.2.0兼容。在低于此范围的包版本上使用 sentence_transformers 时,MLflow 模型集成可能不会成功。从本地文件或运行中加载
sentence_transformers对象。- 参数
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 – 用于下载模型工件的本地文件系统路径。如果提供了该目录,则该目录必须已存在。如果未指定,将创建一个本地输出路径。
- 返回
一个
sentence_transformers模型实例
- mlflow.sentence_transformers.log_model(model, artifact_path: str | None = None, task: str | None = None, inference_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)[源代码]
注意
'sentence_transformers'MLflow 模型集成已知与2.3.1<=sentence-transformers<=5.2.0兼容。在低于此范围的包版本上使用 sentence_transformers 时,MLflow 模型集成可能不会成功。注意
MLflow 在 2.12.0 及更高版本中支持记录需要
trust_remote_code=True的自定义代码的 Sentence Transformers 模型。将
sentence_transformers模型作为 MLflow 人工制品记录到当前运行中。# An example of using log_model for a sentence-transformers model and architecture: from sentence_transformers import SentenceTransformer import mlflow model = SentenceTransformer("all-MiniLM-L6-v2") data = "MLflow is awesome!" signature = mlflow.models.infer_signature( model_input=data, model_output=model.encode(data), ) with mlflow.start_run(): mlflow.sentence_transformers.log_model( model=model, name="sbert_model", signature=signature, input_example=data, )
- 参数
model – 一个训练好的
sentence-transformers模型。artifact_path – Deprecated. Use name instead.
task –
sentence-transformers模型的 MLflow 推理任务类型。候选任务类型为 llm/v1/embeddings。inference_config – 有效覆盖字典,可以在推理过程中应用于
sentence-transformer模型实例。这些参数仅用于将模型加载为pyfunc模型或在 Spark 中使用的情况。这些值不会应用于mlflow.sentence_transformers.load_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.registered_model_name – 如果提供,则在
registered_model_name下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。signature –
ModelSignature类的实例,描述模型的输入和输出。如果未指定但提供了input_example,则会根据提供的输入示例和模型自动推断签名。如果signature和input_example均未指定或自动签名推断失败,则会采用默认签名。为防止采用签名,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入和有效模型输出的数据集上调用infer_signature()。input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。await_registration_for – 等待模型版本完成创建并处于
READY状态的秒数。默认情况下,函数等待五分钟。指定 0 或 None 可跳过等待。pip_requirements – 要么是 pip 要求字符串的可迭代对象(例如
["sentence_transformers", "-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,则将一个 conda 环境添加到模型中,其 pip 要求通过mlflow.models.infer_pip_requirements()推断得出。如果要求推断失败,它将回退到使用 get_default_pip_requirements。来自conda_env的 pip 要求将写入 piprequirements.txt文件,并且完整的 conda 环境将写入conda.yaml。以下是 conda 环境的示例字典表示形式{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "sentence_transformers==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。
- mlflow.sentence_transformers.save_model(model, path: str, task: str | None = None, inference_config: dict[str, typing.Any] | None = None, code_paths: list[str] | None = 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[源代码]
注意
'sentence_transformers'MLflow 模型集成已知与2.3.1<=sentence-transformers<=5.2.0兼容。在低于此范围的包版本上使用 sentence_transformers 时,MLflow 模型集成可能不会成功。注意
MLflow 在 2.12.0 及更高版本中支持保存需要
trust_remote_code=True的自定义代码的 Sentence Transformers 模型。将训练好的
sentence-transformers模型保存到本地文件系统的路径。- 参数
model – 一个训练好的
sentence-transformers模型。path – 要保存的序列化模型的本地目标路径。
task –
sentence-transformers模型的 MLflow 推理任务类型。候选任务类型为 llm/v1/embeddings。inference_config – 有效的推理参数字典,可以在推理过程中应用于
sentence-transformer模型实例。这些参数仅用于将模型加载为pyfunc模型或在 Spark 中使用的情况。这些值不会应用于mlflow.sentence_transformers.load_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 –
ModelSignature类的实例,描述模型的输入和输出。如果未指定但提供了input_example,则会根据提供的输入示例和模型自动推断签名。如果signature和input_example均未指定或自动签名推断失败,则会采用默认签名。为防止采用签名,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入和有效模型输出的数据集上调用infer_signature()。input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。pip_requirements – 要么是 pip 要求字符串的可迭代对象(例如
["sentence_transformers", "-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,则将一个 conda 环境添加到模型中,其 pip 要求通过mlflow.models.infer_pip_requirements()推断得出。如果要求推断失败,它将回退到使用 get_default_pip_requirements。来自conda_env的 pip 要求将写入 piprequirements.txt文件,并且完整的 conda 环境将写入conda.yaml。以下是 conda 环境的示例字典表示形式{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "sentence_transformers==x.y.z" ], }, ], }
metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。