SuperAPI AI 工具集

mlflow-tracking

数据与分析

查看项目

更新于 10-15 20:33

技能介绍

用途:在 Python 项目中搭建并运行 MLflow,做机器学习实验跟踪(参数、指标、产物)、模型注册表,以及 MLflow 2.14+ 的 LLM 调用追踪;适用于需要在 UI 中对比 run、做框架自动记录、自建跟踪服务器或带别名做模型版本晋升的场景。不适用于需要数据版本化的可复现流水线(改用 dvc-ml-workflow)、专门使用 Weights & Biases、Databricks 托管 MLflow,或无需 UI 的一次性运行。

部署模式需先选定:文件模式 file:./mlruns(临时运行,不支持模型注册表,register_model 会报错);SQLite+mlflow ui(推荐个人使用,sqlite:///mlflow.db);Docker Compose 栈(推荐团队/生产,http://host:8000,PostgreSQL + MinIO);Databricks 托管超出范围。

主要能力:手动或自动记录(mlflow.autolog、mlflow.sklearn.autolog、mlflow.pytorch.autolog、mlflow.transformers.autolog 等);用 mlflow.search_runs 以 pandas DataFrame 查询并排序 run;模型注册与别名(set_registered_model_alias、models:/my-model@Champion);LLM 追踪(mlflow.openai.autolog 及 anthropic、langchain、llama_index、dspy、autogen、crewai、litellm,或 @mlflow.trace 手动 span)在 UI 的 Traces 标签查看;可用 mlflow-widgets 在 marimo/Jupyter 中做实时图表。

用法要点:设 mlflow.set_tracking_uri("sqlite:///mlflow.db") 或 "http://localhost:8000",子进程/CI 用 export MLFLOW_TRACKING_URI。SQLite 初始化:bash skills/local/mlflow-tracking/scripts/init-mlflow-sqlite.sh;服务器:bash skills/local/mlflow-tracking/scripts/start-mlflow-server.sh --target-dir infra/mlflow;查看:mlflow ui --backend-store-uri sqlite:///mlflow.db --port 5001;终端 tail-runs.sh 支持 --experiment、--top-n、--sort-by、--format。

限制与注意:mlflow ui 不会自动读取 SQLite,必须显式传 --backend-store-uri,否则界面为空;set_tracking_uri() 仅进程内有效;autolog 必须在 start_run() 前调用;模型注册表需数据库后端;macOS 5000 端口可能被 AirPlay Receiver 占用(脚本用 5001/8000);用 S3/MinIO 产物时客户端环境需设 AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY;SQLite 并发写入会锁库,多训练进程需迁移到 PostgreSQL;Stages 在 MLflow ≥ 2.9 已弃用,改用别名;run 名不唯一,程序化使用 run_id;MLFLOW_REGISTRY_URI 通常无需设置。MLflow 约每 4–6 周发版,LLM 追踪变动快,版本相关问题请先查阅官方文档:https://mlflow.org/docs/latest/tracking.html、https://mlflow.org/docs/latest/model-registry.html、https://mlflow.org/docs/latest/llms/tracing/index.html、https://mlflow.org/docs/latest/tracking/autolog.html。