mlflow.llama_index

mlflow.llama_index.autolog(log_traces: bool = True, disable: bool = False, silent: bool = False)[source]

启用(或禁用)并配置从 LlamaIndex 到 MLflow 的自动日志记录。当前,MLflow 仅支持用于追踪的自动日志记录。

Parameters
  • log_traces – 如果 True,则为 LlamaIndex 模型记录跟踪信息。如果 False,在推理期间不收集任何跟踪信息。默认值为 True

  • disable – 如果 True,则禁用 LlamaIndex 的自动日志记录集成。如果 False,则启用 LlamaIndex 的自动日志记录集成。

  • silent – 如果 True,在 LlamaIndex 的 autologging 期间抑制来自 MLflow 的所有事件日志和警告。如果 False,则显示所有事件和警告。

mlflow.llama_index.load_model(model_uri, dst_path=None)[source]

从本地文件或一次运行中加载 LlamaIndex 索引/引擎/工作流。

Parameters
  • model_uri

    MLflow 模型的 URI 格式位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs:/<mlflow_run_id>/run-relative/path/to/model

    • mlflow-artifacts:/path/to/model

    有关受支持的 URI 方案的更多信息,请参见 Referencing Artifacts.

  • dst_path – 用于下载模型工件的本地文件系统路径。该目录如果提供,必须已存在。如果未指定,将创建一个本地输出路径。

Returns

一个 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 工件记录。

注意

仅在‘Model-from-Code’保存模式下支持保存非索引对象。 请参阅 Models From Code Guide 获取更多信息。

注意

在记录模型时,MLflow 会自动保存 Settings 对象的状态,以便在推理时使用相同的设置。但请注意,Settings 对象中的某些信息不会被保存,包括:

  • 用于避免密钥泄露的 API 密钥。

  • 不可序列化的函数对象。

Parameters
  • llama_index_model

    要保存的 LlamaIndex 对象。支持的模型类型包括:

    1. 一个 Index 对象。

    2. 一个 Engine 对象,例如 ChatEngine、QueryEngine、Retriever。

    3. 一个 Workflow 对象。

    4. 表示包含 LlamaIndex 模型定义的脚本路径的字符串

      属于上述类型之一。

  • artifact_path – 已弃用。请改用 name

  • engine_type

    在保存 Index 对象时需要,用于确定当该索引作为 pyfunc 模型加载时的推理接口。该字段在保存其他 LlamaIndex 对象时是必需的。支持的取值如下:

    • "chat": 将索引作为 LlamaIndex ChatEngine 的一个实例加载。

    • "query": 将索引作为 LlamaIndex QueryEngine 的一个实例加载。

    • "retriever": 将索引作为 LlamaIndex Retriever 的一个实例加载。

  • model_config

    The model configuration to apply when loading the model back with mlflow.pyfunc.load_model(). It will be applied in a different way depending on the model type and saving method:

    For in-memory Index objects saved directly, it will be passed as keyword arguments to instantiate the LlamaIndex engine with the specified engine type at logging.

    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)
    

    For other model types saved with the Model-from-Code <https://www.mlflow.org/docs/latest/model/models-from-code.html> method, the config will be accessed via the :py:class`~mlflow.models.ModelConfig` object within your model code.

    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

    本地文件系统中指向 Python 文件依赖(或包含文件依赖的目录)路径的列表。这些文件在模型加载时会被预先添加到系统路径中。如果为某个模型声明了依赖文件且多个文件之间存在导入依赖关系,那么这些文件应从一个共同的根路径声明相对导入,以避免在加载模型时发生导入错误。

    有关 code_paths 功能、推荐的使用模式和限制的详细说明,请参阅 code_paths usage guide

  • registered_model_name – 如果提供,则在 registered_model_name 下创建一个模型版本,如果不存在具有该名称的注册模型,则同时创建一个注册模型。

  • signature – 一个描述模型输入和输出 Schema 的 Model Signature 对象。模型签名可以使用 infer_signature 函数(位于 mlflow.models.signature)推断得到。

  • input_example – 一个或多个有效模型输入实例。输入示例用于提示应向模型提供何种数据。它将被转换为一个 Pandas DataFrame,然后使用 Pandas 的 split-oriented 格式序列化为 json,或者转换为一个 numpy array,其中示例将通过将其转换为列表的方式序列化为 json。字节使用 base64 编码。当 signature 参数为 None 时,输入示例用于推断模型签名。

  • await_registration_for – 等待模型版本完成创建并进入 READY 状态的秒数。默认情况下,该函数等待五分钟。指定 0 或 None 以跳过等待。

  • pip_requirements – 要么是一个 pip 依赖字符串的可迭代对象(例如 ["llama_index", "-r requirements.txt", "-c constraints.txt"])要么是本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,该项描述了该模型应运行的环境。如果 None,则由 mlflow.models.infer_pip_requirements() 从当前软件环境推断出默认的依赖列表。如果依赖推断失败,则回退到使用 get_default_pip_requirements。依赖和约束会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖也会被写入模型的 conda 环境(conda.yaml)文件中的 pip 部分。

  • extra_pip_requirements

    要么是一个 pip 需求字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,该参数描述了附加的 pip 依赖,这些依赖会被追加到基于用户当前软件环境自动生成的默认 pip 依赖集合中。requirements 和 constraints 会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型的 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定:

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    This example 演示了如何使用 pip_requirementsextra_pip_requirements 指定 pip 依赖。

  • conda_env

    要么是 Conda 环境的字典表示,要么是指向 conda 环境 yaml 文件的路径。如果提供,它描述了应在其中运行该模型的环境。至少,它应指定 get_default_conda_env() 中包含的依赖项。如果 None,则会将一个 conda 环境(其 pip 依赖由 mlflow.models.infer_pip_requirements() 推断得出)添加到模型中。如果依赖推断失败,则回退使用 get_default_pip_requirements。来自 conda_env 的 pip 依赖将被写入 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 Prompt Registry 注册的 prompt URI 列表,用于与模型关联。 Each prompt URI should be in the form prompt://. 在与模型关联之前,这些 prompt 应该先在 MLflow Prompt Registry 中注册。

    这将在模型和 prompt 之间创建相互链接。关联的 prompt 可以在存储于 MLmodel 文件中的模型元数据中看到。通过 Prompt Registry UI,你也可以导航到该模型。

    import mlflow
    
    prompt_template = "Hi, {name}! How are you doing today?"
    
    # 在 MLflow Prompt Registry 中注册一个 prompt
    mlflow.prompts.register_prompt("my_prompt", prompt_template, description="A simple prompt")
    
    # 使用已注册的 prompt 记录模型
    with mlflow.start_run():
        model_info = mlflow.pyfunc.log_model(
            name=MyModel(),
            name="model",
            prompts=["prompt:/my_prompt/1"]
        )
    
    print(model_info.prompts)
    # 输出:['prompt:/my_prompt/1']
    
    # 加载 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 模型保存到本地文件系统的某个路径上。

注意

仅在 'Model-from-Code' 保存模式下支持保存非索引对象。有关更多信息,请参阅 Models From Code Guide

注意

在记录模型时,MLflow 会自动保存 Settings 对象的状态,以便在推理时使用相同的设置。但请注意,Settings 对象中的某些信息不会被保存,包括:

  • API 密钥用于防止密钥泄露。

  • 不可序列化的函数对象。

Parameters
  • llama_index_model

    要保存的 LlamaIndex 对象。支持的模型类型有:

    1. 一个 Index 对象。

    2. 一个 Engine 对象,例如 ChatEngine、QueryEngine、Retriever。

    3. 一个 Workflow 对象。

    4. 表示包含 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

    本地文件系统中指向 Python 文件依赖(或包含文件依赖的目录)路径的列表。这些文件在模型加载时会被预先添加到系统路径中。如果为某个模型声明了依赖文件且多个文件之间存在导入依赖关系,那么这些文件应从一个共同的根路径声明相对导入,以避免在加载模型时发生导入错误。

    有关 code_paths 功能、推荐的使用模式和限制的详细说明,请参阅 code_paths usage guide

  • mlflow_model – 一个 MLflow 模型对象,用于指定将此模型添加到的 flavor。

  • signature – 一个描述模型输入和输出 Schema 的 Model Signature 对象。模型签名可以使用 infer_signature 函数从 mlflow.models.signature 推断得到。

  • input_example – 一个或多个有效模型输入实例。输入示例用于提示应向模型提供何种数据。它将被转换为一个 Pandas DataFrame,然后使用 Pandas 的 split-oriented 格式序列化为 json,或者转换为一个 numpy array,其中示例将通过将其转换为列表的方式序列化为 json。字节使用 base64 编码。当 signature 参数为 None 时,输入示例用于推断模型签名。

  • pip_requirements – 要么是一个 pip 依赖字符串的可迭代对象(例如 ["llama_index", "-r requirements.txt", "-c constraints.txt"])要么是本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,则用于描述该模型应在其上运行的环境。如果 None,则从当前软件环境中由 mlflow.models.infer_pip_requirements() 推断出默认的依赖列表。如果依赖推断失败,则回退使用 get_default_pip_requirements。两者(requirements 和 constraints)会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型的 conda 环境的 pip 部分(conda.yaml)文件。

  • extra_pip_requirements

    要么是一个 pip 需求字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,该参数描述了附加的 pip 依赖,这些依赖会被追加到基于用户当前软件环境自动生成的默认 pip 依赖集合中。requirements 和 constraints 会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。依赖项也会被写入模型的 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定:

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    This example 演示了如何使用 pip_requirementsextra_pip_requirements 指定 pip 依赖。

  • conda_env

    可以是 Conda 环境的字典表示,或指向 conda 环境 yaml 文件的路径。如果提供,它描述了应在该环境中运行此模型。至少应指定包含在 get_default_conda_env() 中的依赖项。如果 None,则会向模型添加一个由 mlflow.models.infer_pip_requirements() 推断出 pip 依赖项的 conda 环境。如果依赖推断失败,则回退使用 get_default_pip_requirements。来自 conda_env 的 pip 依赖将写入 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 文件中的自定义元数据字典。