Package org.mlflow.tracking
Class MlflowClient
java.lang.Object
org.mlflow.tracking.MlflowClient
- All Implemented Interfaces:
Closeable
,Serializable
,AutoCloseable
Client to an MLflow Tracking Sever.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionReturn a default client based on the MLFLOW_TRACKING_URI environment variable.MlflowClient
(String trackingUri) Instantiate a new client using the provided tracking uri.MlflowClient
(MlflowHostCredsProvider hostCredsProvider) Create a new MlflowClient; users should prefer constructing ApiClients viaMlflowClient()
orMlflowClient(String)
if possible. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the MlflowClient and releases any associated resources.createExperiment
(String experimentName) Create a new experiment using the default artifact location provided by the server.createExperiment
(Service.CreateExperiment request) Create a new experiment.Create a new run under the default experiment with no application name.Create a new run under the given experiment.createRun
(Service.CreateRun request) Create a new run.void
deleteExperiment
(String experimentId) Mark an experiment and associated runs, params, metrics, etc.void
Delete a run with the given ID.void
Delete a tag on the run ID with a specific key.downloadArtifacts
(String runId) Return a local directory containing *all* artifacts within the run's artifact directory.downloadArtifacts
(String runId, String artifactPath) Return a local file or directory containing all artifacts within the given artifactPath within the run's root artifactDirectory.downloadLatestModelVersion
(String modelName, String stage) Returns a directory containing all artifacts within the latest registered model version in the given stage.downloadModelVersion
(String modelName, String version) Returns a directory containing all artifacts within the given registered model version.getExperiment
(String experimentId) getExperimentByName
(String experimentName) getLatestVersions
(String modelName) Return the latest model version for each stage.getLatestVersions
(String modelName, Iterable<String> stages) Return the latest model version for each stage requested.getMetricHistory
(String runId, String key) getModelVersion
(String modelName, String version) import org.mlflow.api.proto.ModelRegistry.ModelVersion; ModelVersion modelVersion = getModelVersion("model", "version");getModelVersionDownloadUri
(String modelName, String version) Return the model URI containing for the given model version.getRegisteredModel
(String modelName) Returns a RegisteredModel from the model registry for the given model name.Get metadata, params, tags, and metrics for a run.listArtifacts
(String runId) List the artifacts immediately under the run's root artifact directory.listArtifacts
(String runId, String artifactPath) List the artifacts immediately under the given artifactPath within the run's root artifact directory.listRunInfos
(String experimentId) void
logArtifact
(String runId, File localFile) Upload the given local file or directory to the run's root artifact directory.void
logArtifact
(String runId, File localFile, String artifactPath) Upload the given local file or directory to an artifactPath within the run's root directory.void
logArtifacts
(String runId, File localDir) Upload all files within the given local directory the run's root artifact directory.void
logArtifacts
(String runId, File localDir, String artifactPath) Upload all files within the given local director an artifactPath within the run's root artifact directory.void
logBatch
(String runId, Iterable<Service.Metric> metrics, Iterable<Service.Param> params, Iterable<Service.RunTag> tags) Log multiple metrics, params, and/or tags against a given run (argument runId).void
Log a new metric against the given run, as a key-value pair.void
Log a new metric against the given run, as a key-value pair.void
Log a parameter against the given run, as a key-value pair.void
renameExperiment
(String experimentId, String newName) Update an experiment's name.void
restoreExperiment
(String experimentId) Restore an experiment marked for deletion.void
restoreRun
(String runId) Restore a deleted run with the given ID.Return up to 1000 active experiments.searchExperiments
(String searchFilter) Return up to the first 1000 active experiments that satisfy the search query.searchExperiments
(String searchFilter, Service.ViewType experimentViewType, int maxResults, List<String> orderBy) Return experiments that satisfy the search query.searchExperiments
(String searchFilter, Service.ViewType experimentViewType, int maxResults, List<String> orderBy, String pageToken) Return experiments that satisfy the search query.Return up to 1000 model versions.searchModelVersions
(String searchFilter) Return up to 1000 model versions that satisfy the search query.searchModelVersions
(String searchFilter, int maxResults, List<String> orderBy) Return model versions that satisfy the search query.searchModelVersions
(String searchFilter, int maxResults, List<String> orderBy, String pageToken) Return model versions that satisfy the search query.searchRuns
(List<String> experimentIds, String searchFilter) Deprecated.searchRuns
(List<String> experimentIds, String searchFilter, Service.ViewType runViewType) Deprecated.As of 1.1.0 - please usesearchRuns(List, String, ViewType, int)
or similar that returns a page of Run results.searchRuns
(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults) Return runs from provided list of experiments that satisfy the search query.searchRuns
(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults, List<String> orderBy) Return runs from provided list of experiments that satisfy the search query.searchRuns
(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults, List<String> orderBy, String pageToken) Return runs from provided list of experiments that satisfy the search query.Send a GET to the following path, including query parameters.Send a POST to the following path, with a String-encoded JSON body.void
setExperimentTag
(String experimentId, String key, String value) Log a new tag against the given experiment as a key-value pair.void
Log a new tag against the given run, as a key-value pair.void
setTerminated
(String runId) Set the status of a run to be FINISHED at the current time.void
setTerminated
(String runId, Service.RunStatus status) Set the status of a run to be completed at the current time.void
setTerminated
(String runId, Service.RunStatus status, long endTime) Set the status of a run to be completed at the given endTime.
-
Field Details
-
DEFAULT_EXPERIMENT_ID
- See Also:
-
-
Constructor Details
-
MlflowClient
public MlflowClient()Return a default client based on the MLFLOW_TRACKING_URI environment variable. -
MlflowClient
Instantiate a new client using the provided tracking uri. -
MlflowClient
Create a new MlflowClient; users should prefer constructing ApiClients viaMlflowClient()
orMlflowClient(String)
if possible.
-
-
Method Details
-
getRun
Get metadata, params, tags, and metrics for a run. A single value is returned for each metric key: the most recently logged metric value at the largest step.- Returns:
- Run associated with the ID.
-
getMetricHistory
-
createRun
Create a new run under the default experiment with no application name.- Returns:
- RunInfo created by the server.
-
createRun
Create a new run under the given experiment.- Returns:
- RunInfo created by the server.
-
createRun
Create a new run. This method allows providing all possible fields of CreateRun, and can be invoked as follows:import org.mlflow.api.proto.Service.CreateRun; CreateRun.Builder request = CreateRun.newBuilder(); request.setExperimentId(experimentId); request.setSourceVersion("my-version"); createRun(request.build());
- Returns:
- RunInfo created by the server.
-
listRunInfos
- Returns:
- A list of all RunInfos associated with the given experiment.
-
searchRuns
Deprecated.As of 1.1.0 - please usesearchRuns(List, String, ViewType, int)
or similar that returns a page of Run results.Return RunInfos from provided list of experiments that satisfy the search query.- Parameters:
experimentIds
- List of experiment IDs.searchFilter
- SQL compatible search query string. Format of this query string is similar to that specified on MLflow UI. Example : "params.model = 'LogisticRegression' and metrics.acc = 0.9" If null, the result will be equivalent to having an empty search filter.- Returns:
- A list of all RunInfos that satisfy search filter.
-
searchRuns
public List<Service.RunInfo> searchRuns(List<String> experimentIds, String searchFilter, Service.ViewType runViewType) Deprecated.As of 1.1.0 - please usesearchRuns(List, String, ViewType, int)
or similar that returns a page of Run results.Return RunInfos from provided list of experiments that satisfy the search query.- Parameters:
experimentIds
- List of experiment IDs.searchFilter
- SQL compatible search query string. Format of this query string is similar to that specified on MLflow UI. Example : "params.model = 'LogisticRegression' and metrics.acc != 0.9" If null, the result will be equivalent to having an empty search filter.runViewType
- ViewType for expected runs. One of (ACTIVE_ONLY, DELETED_ONLY, ALL) If null, only runs with viewtype ACTIVE_ONLY will be searched.- Returns:
- A list of all RunInfos that satisfy search filter.
-
searchRuns
public RunsPage searchRuns(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults) Return runs from provided list of experiments that satisfy the search query.- Parameters:
experimentIds
- List of experiment IDs.searchFilter
- SQL compatible search query string. Format of this query string is similar to that specified on MLflow UI. Example : "params.model = 'LogisticRegression' and metrics.acc != 0.9" If null, the result will be equivalent to having an empty search filter.runViewType
- ViewType for expected runs. One of (ACTIVE_ONLY, DELETED_ONLY, ALL) If null, only runs with viewtype ACTIVE_ONLY will be searched.maxResults
- Maximum number of runs desired in one page.- Returns:
- A list of all Runs that satisfy search filter.
-
searchRuns
public RunsPage searchRuns(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults, List<String> orderBy) Return runs from provided list of experiments that satisfy the search query.- Parameters:
experimentIds
- List of experiment IDs.searchFilter
- SQL compatible search query string. Format of this query string is similar to that specified on MLflow UI. Example : "params.model = 'LogisticRegression' and metrics.acc != 0.9" If null, the result will be equivalent to having an empty search filter.runViewType
- ViewType for expected runs. One of (ACTIVE_ONLY, DELETED_ONLY, ALL) If null, only runs with viewtype ACTIVE_ONLY will be searched.maxResults
- Maximum number of runs desired in one page.orderBy
- List of properties to order by. Example: "metrics.acc DESC".- Returns:
- A list of all Runs that satisfy search filter.
-
searchRuns
public RunsPage searchRuns(List<String> experimentIds, String searchFilter, Service.ViewType runViewType, int maxResults, List<String> orderBy, String pageToken) Return runs from provided list of experiments that satisfy the search query.- Parameters:
experimentIds
- List of experiment IDs.searchFilter
- SQL compatible search query string. Format of this query string is similar to that specified on MLflow UI. Example : "params.model = 'LogisticRegression' and metrics.acc != 0.9" If null, the result will be equivalent to having an empty search filter.runViewType
- ViewType for expected runs. One of (ACTIVE_ONLY, DELETED_ONLY, ALL) If null, only runs with viewtype ACTIVE_ONLY will be searched.maxResults
- Maximum number of runs desired in one page.orderBy
- List of properties to order by. Example: "metrics.acc DESC".pageToken
- String token specifying the next page of results. It should be obtained from a call tosearchRuns(List, String)
.- Returns:
- A page of Runs that satisfy the search filter.
-
searchExperiments
public ExperimentsPage searchExperiments(String searchFilter, Service.ViewType experimentViewType, int maxResults, List<String> orderBy) Return experiments that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "attribute.name = 'MyExperiment'" - "tags.problem_type = 'iris_regression'" If null, the result will be equivalent to having an empty search filter.experimentViewType
- ViewType for expected experiments. One of (ACTIVE_ONLY, DELETED_ONLY, ALL). If null, only experiments with viewtype ACTIVE_ONLY will be searched.maxResults
- Maximum number of experiments desired in one page.orderBy
- List of properties to order by. Example: "metrics.acc DESC".- Returns:
- A page of experiments that satisfy the search filter.
-
searchExperiments
Return up to 1000 active experiments.- Returns:
- A page of active experiments with up to 1000 items.
-
searchExperiments
Return up to the first 1000 active experiments that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "attribute.name = 'MyExperiment'" - "tags.problem_type = 'iris_regression'" If null, the result will be equivalent to having an empty search filter.- Returns:
- A page of up to active 1000 experiments that satisfy the search filter.
-
searchExperiments
public ExperimentsPage searchExperiments(String searchFilter, Service.ViewType experimentViewType, int maxResults, List<String> orderBy, String pageToken) Return experiments that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "attribute.name = 'MyExperiment'" - "tags.problem_type = 'iris_regression'" If null, the result will be equivalent to having an empty search filter.experimentViewType
- ViewType for expected experiments. One of (ACTIVE_ONLY, DELETED_ONLY, ALL). If null, only experiments with viewtype ACTIVE_ONLY will be searched.maxResults
- Maximum number of experiments desired in one page.orderBy
- List of properties to order by. Example: "metrics.acc DESC".pageToken
- String token specifying the next page of results. It should be obtained from a call tosearchExperiments(String)
.- Returns:
- A page of experiments that satisfy the search filter.
-
getExperiment
- Returns:
- An experiment with the given ID.
-
getExperimentByName
- Returns:
- The experiment associated with the given name or Optional.empty if none exists.
-
createExperiment
Create a new experiment using the default artifact location provided by the server.- Parameters:
experimentName
- Name of the experiment. This must be unique across all experiments.- Returns:
- Experiment ID of the newly created experiment.
-
createExperiment
Create a new experiment. This method allows providing all possible fields of CreateExperiment, and can be invoked as follows:import org.mlflow.api.proto.Service.CreateExperiment; CreateExperiment.Builder request = CreateExperiment.newBuilder(); request.setName(name); request.setArtifactLocation(artifactLocation); request.addTags(experimentTag); createExperiment(request.build());
- Returns:
- ID of the experiment created by the server.
-
deleteExperiment
Mark an experiment and associated runs, params, metrics, etc. for deletion. -
restoreExperiment
Restore an experiment marked for deletion. -
renameExperiment
Update an experiment's name. The new name must be unique. -
deleteRun
Delete a run with the given ID. -
restoreRun
Restore a deleted run with the given ID. -
logParam
Log a parameter against the given run, as a key-value pair. This cannot be called against the same parameter key more than once. -
logMetric
Log a new metric against the given run, as a key-value pair. Metrics are recorded against two axes: timestamp and step. This method uses the number of milliseconds since the Unix epoch for the timestamp, and it uses the default step of zero.- Parameters:
runId
- The ID of the run in which to record the metric.key
- The key identifying the metric for which to record the specified value.value
- The value of the metric.
-
logMetric
Log a new metric against the given run, as a key-value pair. Metrics are recorded against two axes: timestamp and step.- Parameters:
runId
- The ID of the run in which to record the metric.key
- The key identifying the metric for which to record the specified value.value
- The value of the metric.timestamp
- The timestamp at which to record the metric value.step
- The step at which to record the metric value.
-
setExperimentTag
Log a new tag against the given experiment as a key-value pair.- Parameters:
experimentId
- The ID of the experiment on which to set the tagkey
- The key used to identify the tag.value
- The value of the tag.
-
setTag
Log a new tag against the given run, as a key-value pair.- Parameters:
runId
- The ID of the run on which to set the tagkey
- The key used to identify the tag.value
- The value of the tag.
-
deleteTag
Delete a tag on the run ID with a specific key. This is irreversible.- Parameters:
runId
- String ID of the runkey
- Name of the tag
-
logBatch
public void logBatch(String runId, Iterable<Service.Metric> metrics, Iterable<Service.Param> params, Iterable<Service.RunTag> tags) Log multiple metrics, params, and/or tags against a given run (argument runId). Argument metrics, params, and tag iterables can be nulls. -
setTerminated
Set the status of a run to be FINISHED at the current time. -
setTerminated
Set the status of a run to be completed at the current time. -
setTerminated
Set the status of a run to be completed at the given endTime. -
sendGet
Send a GET to the following path, including query parameters. This is mostly an internal API, but allows making lower-level or unsupported requests.- Returns:
- JSON response from the server.
-
sendPost
Send a POST to the following path, with a String-encoded JSON body. This is mostly an internal API, but allows making lower-level or unsupported requests.- Returns:
- JSON response from the server.
-
sendPatch
-
logArtifact
Upload the given local file or directory to the run's root artifact directory. For example,logArtifact(runId, "/my/localModel") listArtifacts(runId) // returns "localModel"
- Parameters:
runId
- Run ID of an existing MLflow run.localFile
- File or directory to upload. Must exist.
-
logArtifact
Upload the given local file or directory to an artifactPath within the run's root directory. For example,logArtifact(runId, "/my/localModel", "model") listArtifacts(runId, "model") // returns "model/localModel"
(i.e., the localModel file is now available in model/localModel). If logging a directory, the directory is renamed to artifactPath.- Parameters:
runId
- Run ID of an existing MLflow run.localFile
- File or directory to upload. Must exist.artifactPath
- Artifact path relative to the run's root directory. Should NOT start with a /.
-
logArtifacts
Upload all files within the given local directory the run's root artifact directory. For example, if /my/local/dir/ contains two files "file1" and "file2", thenlogArtifacts(runId, "/my/local/dir") listArtifacts(runId) // returns "file1" and "file2"
- Parameters:
runId
- Run ID of an existing MLflow run.localDir
- Directory to upload. Must exist, and must be a directory (not a simple file).
-
logArtifacts
Upload all files within the given local director an artifactPath within the run's root artifact directory. For example, if /my/local/dir/ contains two files "file1" and "file2", thenlogArtifacts(runId, "/my/local/dir", "model") listArtifacts(runId, "model") // returns "model/file1" and "model/file2"
(i.e., the contents of the local directory are now available in model/).- Parameters:
runId
- Run ID of an existing MLflow run.localDir
- Directory to upload. Must exist, and must be a directory (not a simple file).artifactPath
- Artifact path relative to the run's root directory. Should NOT start with a /.
-
listArtifacts
List the artifacts immediately under the run's root artifact directory. This does not recursively list; instead, it will return FileInfos with isDir=true where further listing may be done.- Parameters:
runId
- Run ID of an existing MLflow run.
-
listArtifacts
List the artifacts immediately under the given artifactPath within the run's root artifact directory. This does not recursively list; instead, it will return FileInfos with isDir=true where further listing may be done.- Parameters:
runId
- Run ID of an existing MLflow run.artifactPath
- Artifact path relative to the run's root directory. Should NOT start with a /.
-
downloadArtifacts
Return a local directory containing *all* artifacts within the run's artifact directory. Note that this will download the entire directory path, and so may be expensive if the directory has a lot of data.- Parameters:
runId
- Run ID of an existing MLflow run.
-
downloadArtifacts
Return a local file or directory containing all artifacts within the given artifactPath within the run's root artifactDirectory. For example, if "model/file1" and "model/file2" exist within the artifact directory, thendownloadArtifacts(runId, "model") // returns a local directory containing "file1" and "file2" downloadArtifacts(runId, "model/file1") // returns a local *file* with the contents of file1.
Note that this will download the entire subdirectory path, and so may be expensive if the subdirectory has a lot of data.- Parameters:
runId
- Run ID of an existing MLflow run.artifactPath
- Artifact path relative to the run's root directory. Should NOT start with a /.
-
getLatestVersions
Return the latest model version for each stage. The current available stages are: [None, Staging, Production, Archived].import org.mlflow.api.proto.ModelRegistry.ModelVersion; List
<ModelVersion>
detailsList = getLatestVersions("model"); for (ModelVersion details : detailsList) { System.out.println("Model Name: " + details.getModelVersion() .getRegisteredModel() .getName()); System.out.println("Model Version: " + details.getModelVersion().getVersion()); System.out.println("Current Stage: " + details.getCurrentStage()); }- Parameters:
modelName
- The name of the model- Returns:
- A collection of
ModelRegistry.ModelVersion
-
getLatestVersions
public List<ModelRegistry.ModelVersion> getLatestVersions(String modelName, Iterable<String> stages) Return the latest model version for each stage requested. The current available stages are: [None, Staging, Production, Archived].import org.mlflow.api.proto.ModelRegistry.ModelVersion; List
<ModelVersion>
detailsList = getLatestVersions("model", Lists.newArrayList<String>
("Staging")); for (ModelVersion details : detailsList) { System.out.println("Model Name: " + details.getModelVersion() .getRegisteredModel() .getName()); System.out.println("Model Version: " + details.getModelVersion().getVersion()); System.out.println("Current Stage: " + details.getCurrentStage()); }- Parameters:
modelName
- The name of the modelstages
- A list of stages- Returns:
- The latest model version
ModelRegistry.ModelVersion
-
getModelVersion
import org.mlflow.api.proto.ModelRegistry.ModelVersion; ModelVersion modelVersion = getModelVersion("model", "version");
- Parameters:
modelName
- Name of the containing registered model. *version
- Version number as a string of the model version.- Returns:
- a single model version
ModelRegistry.ModelVersion
-
getRegisteredModel
Returns a RegisteredModel from the model registry for the given model name.import org.mlflow.api.proto.ModelRegistry.RegisteredModel; RegisteredModel registeredModel = getRegisteredModel("model");
- Parameters:
modelName
- Name of the containing registered model. *- Returns:
- a registered model
ModelRegistry.RegisteredModel
-
getModelVersionDownloadUri
Return the model URI containing for the given model version. The model URI can be used to download the model version artifacts.String modelUri = getModelVersionDownloadUri("model", 0);
- Parameters:
modelName
- The name of the modelversion
- The version number of the model- Returns:
- The specified model version's URI.
-
downloadModelVersion
Returns a directory containing all artifacts within the given registered model version. The method will download the model version artifacts to the local file system. Note that this method will not work if the `download_uri` refers to a single file (and not a directory) due to the way many ArtifactRepository's `download_artifacts` handle empty subpaths.File modelVersionDir = downloadModelVersion("model", 0);
- Parameters:
modelName
- The name of the modelversion
- The version number of the model- Returns:
- A directory (
File
) containing model artifacts
-
downloadLatestModelVersion
Returns a directory containing all artifacts within the latest registered model version in the given stage. The method will download the model version artifacts to the local file system.File modelVersionDir = downloadLatestModelVersion("model", "Staging");
(i.e., the contents of the local directory are now available).- Parameters:
modelName
- The name of the modelstage
- The name of the stage- Returns:
- A directory (
File
) containing model artifacts
-
searchModelVersions
public ModelVersionsPage searchModelVersions(String searchFilter, int maxResults, List<String> orderBy) Return model versions that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "name = 'model_name'" - "run_id = '...'" If null, the result will be equivalent to having an empty search filter.maxResults
- Maximum number of model versions desired in one page.orderBy
- List of properties to order by. Example: "name DESC".- Returns:
- A page of model versions that satisfy the search filter.
-
searchModelVersions
Return up to 1000 model versions.- Returns:
- A page of model versions with up to 1000 items.
-
searchModelVersions
Return up to 1000 model versions that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "name = 'model_name'" - "run_id = '...'" If null, the result will be equivalent to having an empty search filter.- Returns:
- A page of model versions with up to 1000 items.
-
searchModelVersions
public ModelVersionsPage searchModelVersions(String searchFilter, int maxResults, List<String> orderBy, String pageToken) Return model versions that satisfy the search query.- Parameters:
searchFilter
- SQL compatible search query string. Examples: - "name = 'model_name'" - "run_id = '...'" If null, the result will be equivalent to having an empty search filter.maxResults
- Maximum number of model versions desired in one page.orderBy
- List of properties to order by. Example: "name DESC".pageToken
- String token specifying the next page of results. It should be obtained from a call tosearchModelVersions(String)
.- Returns:
- A page of model versions that satisfy the search filter.
-
close
public void close()Closes the MlflowClient and releases any associated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
searchRuns(List, String, ViewType, int)
or similar that returns a page of Run results.