连接您的开发环境到 MLflow
本指南将指导您如何将开发环境连接到 MLflow 实验。您可以在本地机器上运行 MLflow,自行托管开源 MLflow 服务,或使用 Databricks Managed MLflow 等托管服务。
MLflow 实验是您的 GenAI 应用程序的容器。在MLflow 文档中了解更多关于实验的信息。
先决条件
- OSS MLflow
- Databricks
- Python 环境:Python 3.8+,已安装 pip
- 本地或远程服务器:可以访问运行 MLflow 跟踪服务器
- Databricks 工作区:可以访问 Databricks 工作区
本指南介绍使用 Databricks Personal Access Token。MLflow 也支持其他Databricks 支持的认证方法。
设置说明
- OSS MLflow
- Databricks - 本地 IDE
- Databricks - Notebook
步骤 1:安装 MLflow
为本地开发安装 MLflow
pip install --upgrade "mlflow>=3.1"
步骤 2:启动 MLflow 跟踪服务器
选项 A:本地跟踪(默认)
如果未指定跟踪 URI,MLflow 将自动使用本地文件存储
import mlflow
# Creates local mlruns directory for experiments
mlflow.set_experiment("my-genai-experiment")
选项 B:远程跟踪服务器
启动远程 MLflow 跟踪服务器
# Start MLflow server (in a separate terminal)
mlflow server --host 0.0.0.0 --port 5000
然后配置您的客户端以使用远程服务器
import mlflow
# Connect to remote MLflow server
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("my-genai-experiment")
选项 C:数据库后端
生产环境请配置 MLflow 使用数据库后端
# Example with PostgreSQL
mlflow server \
--backend-store-uri postgresql://user:password@localhost:5432/mlflow \
--default-artifact-root s3://my-mlflow-bucket/artifacts \
--host 0.0.0.0 \
--port 5000
步骤 3:配置环境(可选)
为了在团队中保持一致的配置,请使用环境变量
# .env file
MLFLOW_TRACKING_URI=https://:5000
MLFLOW_EXPERIMENT_NAME=my-genai-experiment
在您的 Python 代码中加载
import os
from dotenv import load_dotenv
import mlflow
load_dotenv()
# Set tracking URI and experiment
mlflow.set_tracking_uri(os.getenv("MLFLOW_TRACKING_URI", "file:./mlruns"))
mlflow.set_experiment(os.getenv("MLFLOW_EXPERIMENT_NAME", "default"))
步骤 4:验证您的连接
创建测试文件并运行此代码
import mlflow
# Print connection information
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
print(f"Active Experiment: {mlflow.get_experiment_by_name('my-genai-experiment')}")
# Test logging
with mlflow.start_run():
mlflow.log_param("test_param", "test_value")
print("✓ Successfully connected to MLflow!")
步骤 5:访问 MLflow UI
在浏览器中打开以查看 MLflow UI
- 本地跟踪:
https://:5000(如果正在运行 mlflow server) - 基于文件的跟踪:在您的项目目录中运行
mlflow ui,然后访问https://:5000
步骤 1:安装 MLflow
安装支持 Databricks 连接的 MLflow
pip install --upgrade "mlflow[databricks]>=3.1"
步骤 2:创建 MLflow 实验
- 打开您的 Databricks 工作区
- 在左侧边栏的“Machine Learning”下,转到“Experiments”
- 在 Experiments 页面的顶部,点击“New GenAI Experiment”
步骤 3:配置认证
选择以下任一认证方法
选项 A:环境变量
- 在您的 MLflow 实验中,点击“Generate API Key”
- 复制并运行生成的代码到您的终端
export DATABRICKS_TOKEN=<databricks-personal-access-token>
export DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com
export MLFLOW_TRACKING_URI=databricks
export MLFLOW_EXPERIMENT_ID=<experiment-id>
选项 B:.env 文件
- 在您的 MLflow 实验中,点击“Generate API Key”
- 将生成的代码复制到项目根目录下的
.env文件中
DATABRICKS_TOKEN=<databricks-personal-access-token>
DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com
MLFLOW_TRACKING_URI=databricks
MLFLOW_EXPERIMENT_ID=<experiment-id>
- 安装
python-dotenv包
pip install python-dotenv
- 在您的代码中加载环境变量
# At the beginning of your Python script
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
步骤 4:验证您的连接
创建测试文件并运行此代码以验证您的连接
import mlflow
# Test logging to verify connection
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
with mlflow.start_run():
print("✓ Successfully connected to MLflow!")
步骤 1:安装 MLflow
Databricks 运行时已包含 MLflow,但为了获得最佳的 GenAI 功能体验,请更新到最新版本
%pip install --upgrade "mlflow[databricks]>=3.1"
dbutils.library.restartPython()
步骤 2:创建 Notebook
创建 Databricks Notebook 将会创建一个 MLflow 实验,它是您的 GenAI 应用程序的容器。在MLflow 文档中了解更多关于实验的信息。
- 打开您的 Databricks 工作区
- 在左侧边栏顶部点击“New”
- 点击“Notebook”
步骤 3:配置认证
在 Databricks Notebook 中工作时,无需额外的认证配置。Notebook 自动拥有对您工作区和相关 MLflow 实验的访问权限。
步骤 4:验证您的连接
在 Notebook 单元格中运行此代码以验证您的连接
import mlflow
# Test logging to verify connection
print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")
with mlflow.start_run():
print("✓ Successfully connected to MLflow!")
后续步骤
现在您的环境已连接到 MLflow,请尝试其他 GenAI 快速入门指南