mlflow.llama_index

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

启用(或禁用)并配置从 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)[源代码]

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

参数
  • 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 方案的更多信息,请参阅 引用 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)[源代码]

将 LlamaIndex 模型记录为当前运行的 MLflow 工件。

注意

仅在“从代码创建模型”保存模式下支持保存非索引对象。有关更多信息,请参阅从代码创建模型指南

注意

记录模型时,MLflow 将自动保存 Settings 对象的[状态](https://docs.llamaindex.org.cn/en/stable/api_reference/Settings/),以便您可以在推理时使用相同的设置。但是,请注意,Settings 对象中的某些信息不会被保存,包括

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

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

参数
  • llama_index_model

    要保存的 LlamaIndex 对象。支持的模型类型如下

    1. 一个索引对象。

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

    3. 一个工作流对象。

    4. 一个字符串,表示包含上述类型之一的 LlamaIndex 模型定义的脚本的路径

      上述类型之一。

  • artifact_path – Deprecated. Use name instead.

  • engine_type

    在保存索引对象以确定将其作为 pyfunc 模型加载时的索引推理接口时是必需的。在保存其他 LlamaIndex 对象时,此字段**不是**必需的。支持的值如下

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

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

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

  • model_config

    使用 mlflow.pyfunc.load_model() 重新加载模型时应用的[模型配置](https://docs.llamaindex.org.cn/en/stable/api_reference/Settings/)。它将根据模型类型和保存方法以不同方式应用

    对于直接保存的内存中索引对象,它将作为关键字参数传递,以在日志记录时使用指定的引擎类型来实例化 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_paths functionality, recommended usage patterns and limitations, see the code_paths usage guide.

  • registered_model_name – 如果提供,则在 registered_model_name 下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。

  • signature – 描述模型输入和输出 Schema 的模型签名对象。可以使用 mlflow.models.signatureinfer_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.txtconstraints.txt 文件中,并作为模型的一部分存储。要求也会写入模型 conda 环境(conda.yaml)文件的 pip 部分。

  • extra_pip_requirements

    要么是 pip 要求字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了附加到根据用户当前软件环境自动生成的默认 pip 要求集中的额外 pip 要求。要求和约束都会自动解析并写入 requirements.txtconstraints.txt 文件中,并作为模型的一部分存储。要求也会写入模型 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_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 要求会写入一个 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。

  • kwargsmlflow.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[源代码]

将 LlamaIndex 模型保存到本地文件系统的路径。

注意

仅在“从代码创建模型”保存模式下支持保存非索引对象。有关更多信息,请参阅从代码创建模型指南

注意

记录模型时,MLflow 将自动保存 Settings 对象的[状态](https://docs.llamaindex.org.cn/en/stable/api_reference/Settings/),以便您可以在推理时使用相同的设置。但是,请注意,Settings 对象中的某些信息不会被保存,包括

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

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

参数
  • llama_index_model

    要保存的 LlamaIndex 对象。支持的模型类型如下

    1. 一个索引对象。

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

    3. 一个工作流对象。

    4. 一个字符串,表示包含上述类型之一的 LlamaIndex 模型定义的脚本的路径

      上述类型之一。

  • path – 要保存序列化模型(作为 YAML)的本地路径。

  • engine_type

    在保存索引对象以确定将其作为 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_paths functionality, recommended usage patterns and limitations, see the code_paths usage guide.

  • mlflow_model – 一个 MLflow 模型对象,指定该模型正在添加到的 flavor。

  • signature – 描述模型输入和输出 Schema 的模型签名对象。可以使用 mlflow.models.signatureinfer_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.txtconstraints.txt 文件中,并作为模型的一部分存储。要求也会写入模型 conda 环境(conda.yaml)文件的 pip 部分。

  • extra_pip_requirements

    要么是 pip 要求字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"]),要么是本地文件系统上 pip 要求文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了附加到根据用户当前软件环境自动生成的默认 pip 要求集中的额外 pip 要求。要求和约束都会自动解析并写入 requirements.txtconstraints.txt 文件中,并作为模型的一部分存储。要求也会写入模型 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_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 要求会写入一个 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 文件中的自定义元数据字典。