Connect Your Development Environment to MLflow
This guide shows you how to connect your development environment to an MLflow Experiment. You can run MLflow on your local machine, self-host the open source MLflow service, or use a managed offering, such as Databricks Managed MLflow.
An MLflow Experiment is the container for your GenAI application. Learn more about the Experiment and what it contains in the data model section.
Prerequisites
- OSS MLflow
- Databricks
- Python Environment: Python 3.8+ with pip installed
- Local or Remote Server: Access to run MLflow tracking server
- Databricks Workspace: Access to a Databricks workspace
This guide describes using a Databricks Personal Access Token. MLflow also works with the other Databricks-supported authentication methods.
Setup Instructions
- OSS MLflow
- Databricks - Local IDE
- Databricks - Notebook
Step 1: Install MLflow
Install MLflow for local development:
pip install --upgrade "mlflow>=3.1"
Step 2: Start MLflow Tracking Server
Option A: Local Tracking (Default)
MLflow will automatically use local file storage if no tracking URI is specified:
import mlflow
# Creates local mlruns directory for experiments
mlflow.set_experiment("my-genai-experiment")
Option B: Remote Tracking Server
Start a remote MLflow tracking server:
# Start MLflow server (in a separate terminal)
mlflow server --host 0.0.0.0 --port 5000
Then configure your client to use the remote server:
import mlflow
# Connect to remote MLflow server
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("my-genai-experiment")
Option C: Database Backend
For production use, configure MLflow with a database backend:
# 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
Step 3: Configure Environment (Optional)
For consistent configuration across your team, use environment variables:
# .env file
MLFLOW_TRACKING_URI=http://localhost:5000
MLFLOW_EXPERIMENT_NAME=my-genai-experiment
Load in your Python code:
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"))
Step 4: Verify Your Connection
Create a test file and run this code:
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!")
Step 5: Access MLflow UI
Open your browser to view the MLflow UI:
- Local tracking:
http://localhost:5000
(if running mlflow server) - File-based tracking: Run
mlflow ui
in your project directory, then go tohttp://localhost:5000
Step 1: Install MLflow
Install MLflow with Databricks connectivity:
pip install --upgrade "mlflow[databricks]>=3.1"
Step 2: Create an MLflow Experiment
- Open your Databricks workspace
- Go to Experiments in the left sidebar under Machine Learning
- At the top of the Experiments page, click on New GenAI Experiment
Step 3: Configure Authentication
Choose one of the following authentication methods:
Option A: Environment Variables
- In your MLflow Experiment, click Generate API Key
- Copy and run the generated code in your terminal:
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>
Option B: .env File
- In your MLflow Experiment, click Generate API Key
- Copy the generated code to a
.env
file in your project root:
DATABRICKS_TOKEN=<databricks-personal-access-token>
DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com
MLFLOW_TRACKING_URI=databricks
MLFLOW_EXPERIMENT_ID=<experiment-id>
- Install the
python-dotenv
package:
pip install python-dotenv
- Load environment variables in your code:
# At the beginning of your Python script
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
Step 4: Verify Your Connection
Create a test file and run this code to verify your connection:
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!")
Step 1: Install MLflow
Databricks runtimes include MLflow, but for the best experience with GenAI capabilities, update to the latest version:
%pip install --upgrade "mlflow[databricks]>=3.1"
dbutils.library.restartPython()
Step 2: Create a Notebook
Creating a Databricks Notebook will create an MLflow Experiment that is the container for your GenAI application. Learn more about the Experiment and what it contains in the data model section.
- Open your Databricks workspace
- Go to New at the top of the left sidebar
- Click Notebook
Step 3: Configure Authentication
No additional authentication configuration is needed when working within a Databricks Notebook. The notebook automatically has access to your workspace and the associated MLflow Experiment.
Step 4: Verify Your Connection
Run this code in a notebook cell to verify your connection:
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!")
Next Steps
Now that your environment is connected to MLflow, try the other GenAI quickstarts:
- Instrument your app with tracing: Follow the IDE quickstart or Notebook quickstart to instrument your first GenAI app
- Evaluate your app's quality: Use the evaluation quickstart to systematically test and improve your app's quality