mlflow.sentence_transformers
- mlflow.sentence_transformers.get_default_conda_env()[source]
- 返回
使用
sentence_transformersflavor 生成的 MLflow Models 的默认 Conda 环境。
- mlflow.sentence_transformers.get_default_pip_requirements() list[str][source]
检索
sentence_transformersflavor 的最小依赖项集。- 返回
使用
sentence-transformersflavor 生成的 MLflow Models 的默认 pip 依赖项列表。调用save_model()和log_model()会生成一个至少包含这些依赖项的 pip 环境。
- mlflow.sentence_transformers.load_model(model_uri: str, dst_path: Optional[str] = None)[source]
注意
‘sentence_transformers’ MLflow Models 集成已知与
2.3.1<=sentence-transformers<=5.1.2兼容。当使用此范围之外的包版本时,sentence_transformers 的 MLflow Models 集成可能不会成功。从本地文件或 run 加载
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)[source]
注意
‘sentence_transformers’ MLflow Models 集成已知与
2.3.1<=sentence-transformers<=5.1.2兼容。当使用此范围之外的包版本时,sentence_transformers 的 MLflow Models 集成可能不会成功。注意
MLflow 2.12.0 及以上版本支持记录带有自定义代码的 Sentence Transformers 模型(即需要
trust_remote_code=True的模型)。将
sentence_transformers模型记录为当前 run 的 MLflow artifact。# 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 – MLflow 推理任务类型,用于
sentence-transformers模型。候选任务类型为 llm/v1/embeddings。inference_config – 一个有效的覆盖配置字典,可以在推理期间应用于
sentence-transformer模型实例。这些参数仅用于将模型加载为pyfuncModel 或在 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 依赖项字符串的可迭代对象(例如
["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,则会将一个具有由mlflow.models.infer_pip_requirements()推断出的 pip 依赖项的 Conda 环境添加到模型中。如果依赖项推断失败,则回退使用 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[source]
注意
‘sentence_transformers’ MLflow Models 集成已知与
2.3.1<=sentence-transformers<=5.1.2兼容。当使用此范围之外的包版本时,sentence_transformers 的 MLflow Models 集成可能不会成功。注意
MLflow 2.12.0 及以上版本支持保存带有自定义代码的 Sentence Transformers 模型(即需要
trust_remote_code=True的模型)。将训练好的
sentence-transformers模型保存到本地文件系统上的一个路径。- 参数
model – 一个训练好的
sentence-transformers模型。path – 要保存的序列化模型的本地路径目标。
task – MLflow 推理任务类型,用于
sentence-transformers模型。候选任务类型为 llm/v1/embeddings。inference_config – 一个有效的推理参数字典,可以在推理期间应用于
sentence-transformer模型实例。这些参数仅用于将模型加载为pyfuncModel 或在 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 依赖项字符串的可迭代对象(例如
["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,则会将一个具有由mlflow.models.infer_pip_requirements()推断出的 pip 依赖项的 Conda 环境添加到模型中。如果依赖项推断失败,则回退使用 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 文件中的自定义元数据字典。