Documentation
¶
Overview ¶
Package mlflow is a client for the MLflow REST API (tracking + model registry).
Construct a Client with NewClient (it reads MLFLOW_TRACKING_URI from the environment when given ""), then call typed methods. The client is synchronous and best-effort: it retries transient failures but never buffers, so callers decide what to do on error.
Example ¶
Example shows a full run lifecycle: resolve an experiment, start a run, log a batch of metrics/params/tags, then mark the run finished.
package main
import (
"context"
"log"
"time"
"github.com/guygrigsby/mlflow"
)
func main() {
c, err := mlflow.NewClient("") // reads MLFLOW_TRACKING_URI
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
exp, err := c.GetOrCreateExperiment(ctx, "lm-100m-en")
if err != nil {
log.Fatal(err)
}
now := time.Now().UnixMilli()
run, err := c.CreateRun(ctx, exp, mlflow.WithRunName("r1"), mlflow.WithStartTime(now))
if err != nil {
log.Fatal(err)
}
err = c.LogBatch(ctx, run.Info.RunID,
[]mlflow.Metric{{Key: "loss", Value: 1.5, Timestamp: now, Step: 0}},
[]mlflow.Param{{Key: "lr", Value: "4e-4"}},
[]mlflow.RunTag{{Key: "stage", Value: "train"}})
if err != nil {
log.Fatal(err)
}
if err := c.UpdateRun(ctx, run.Info.RunID, mlflow.StatusFinished, time.Now().UnixMilli()); err != nil {
log.Fatal(err)
}
}
Output:
Index ¶
- func IsNotFound(err error) bool
- type APIError
- type Client
- func (c *Client) CreateExperiment(ctx context.Context, name string, tags ...ExperimentTag) (string, error)
- func (c *Client) CreateModelVersion(ctx context.Context, name, source string, runID string, ...) (*ModelVersion, error)
- func (c *Client) CreateRegisteredModel(ctx context.Context, name string, tags ...RegisteredModelTag) (*RegisteredModel, error)
- func (c *Client) CreateRun(ctx context.Context, experimentID string, opts ...RunOption) (*Run, error)
- func (c *Client) DeleteExperiment(ctx context.Context, id string) error
- func (c *Client) DeleteModelVersion(ctx context.Context, name, version string) error
- func (c *Client) DeleteModelVersionTag(ctx context.Context, name, version, key string) error
- func (c *Client) DeleteRegisteredModel(ctx context.Context, name string) error
- func (c *Client) DeleteRegisteredModelAlias(ctx context.Context, name, alias string) error
- func (c *Client) DeleteRegisteredModelTag(ctx context.Context, name, key string) error
- func (c *Client) DeleteRun(ctx context.Context, runID string) error
- func (c *Client) DeleteRunTag(ctx context.Context, runID, key string) error
- func (c *Client) DownloadArtifact(ctx context.Context, runID, path string) ([]byte, error)
- func (c *Client) GetExperimentByName(ctx context.Context, name string) (*Experiment, error)
- func (c *Client) GetLatestVersions(ctx context.Context, name string, stages ...string) ([]ModelVersion, error)
- func (c *Client) GetMetricHistory(ctx context.Context, runID, metricKey string) ([]Metric, error)
- func (c *Client) GetModelVersion(ctx context.Context, name, version string) (*ModelVersion, error)
- func (c *Client) GetModelVersionByAlias(ctx context.Context, name, alias string) (*ModelVersion, error)
- func (c *Client) GetModelVersionDownloadURI(ctx context.Context, name, version string) (string, error)
- func (c *Client) GetOrCreateExperiment(ctx context.Context, name string) (string, error)
- func (c *Client) GetRegisteredModel(ctx context.Context, name string) (*RegisteredModel, error)
- func (c *Client) GetRun(ctx context.Context, runID string) (*Run, error)
- func (c *Client) ListArtifacts(ctx context.Context, runID, path string) (files []FileInfo, nextPageToken string, err error)
- func (c *Client) LogBatch(ctx context.Context, runID string, metrics []Metric, params []Param, ...) error
- func (c *Client) LogInputs(ctx context.Context, runID string, datasets []DatasetInput) error
- func (c *Client) LogMetric(ctx context.Context, runID, key string, value float64, timestampMs, step int64) error
- func (c *Client) LogParam(ctx context.Context, runID, key, value string) error
- func (c *Client) RenameRegisteredModel(ctx context.Context, name, newName string) (*RegisteredModel, error)
- func (c *Client) RestoreExperiment(ctx context.Context, id string) error
- func (c *Client) RestoreRun(ctx context.Context, runID string) error
- func (c *Client) SearchExperiments(ctx context.Context, req SearchExperimentsRequest) ([]Experiment, string, error)
- func (c *Client) SearchModelVersions(ctx context.Context, filter string, maxResults int64, orderBy []string, ...) ([]ModelVersion, string, error)
- func (c *Client) SearchRegisteredModels(ctx context.Context, filter string, maxResults int64, orderBy []string, ...) ([]RegisteredModel, string, error)
- func (c *Client) SearchRuns(ctx context.Context, req SearchRunsRequest) ([]Run, string, error)
- func (c *Client) SetExperimentTag(ctx context.Context, id, key, value string) error
- func (c *Client) SetModelVersionTag(ctx context.Context, name, version, key, value string) error
- func (c *Client) SetRegisteredModelAlias(ctx context.Context, name, alias, version string) error
- func (c *Client) SetRegisteredModelTag(ctx context.Context, name, key, value string) error
- func (c *Client) SetRunTag(ctx context.Context, runID, key, value string) error
- func (c *Client) SetTag(ctx context.Context, runID, key, value string) error
- func (c *Client) TransitionModelVersionStage(ctx context.Context, name, version, stage string, archiveExisting bool) (*ModelVersion, error)
- func (c *Client) UpdateExperiment(ctx context.Context, id, newName string) error
- func (c *Client) UpdateModelVersion(ctx context.Context, name, version, description string) (*ModelVersion, error)
- func (c *Client) UpdateRegisteredModel(ctx context.Context, name, description string) (*RegisteredModel, error)
- func (c *Client) UpdateRun(ctx context.Context, runID string, status RunStatus, endTime int64) error
- type Dataset
- type DatasetInput
- type Experiment
- type ExperimentTag
- type FileInfo
- type Metric
- type ModelVersion
- type ModelVersionTag
- type Option
- type Param
- type RegisteredModel
- type RegisteredModelAlias
- type RegisteredModelTag
- type Run
- type RunData
- type RunInfo
- type RunOption
- type RunStatus
- type RunTag
- type SearchExperimentsRequest
- type SearchRunsRequest
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNotFound ¶
IsNotFound reports whether err is an APIError with RESOURCE_DOES_NOT_EXIST.
Example ¶
ExampleIsNotFound shows branching on a missing resource.
package main
import (
"context"
"fmt"
"log"
"github.com/guygrigsby/mlflow"
)
func main() {
c, err := mlflow.NewClient("")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
if _, err := c.GetExperimentByName(ctx, "does-not-exist"); mlflow.IsNotFound(err) {
fmt.Println("not found, creating")
_, _ = c.CreateExperiment(ctx, "does-not-exist")
}
}
Output:
Types ¶
type APIError ¶
type APIError struct {
Code string // MLflow error_code, e.g. "RESOURCE_DOES_NOT_EXIST"
Message string
HTTPStatus int
}
APIError is a non-2xx response from MLflow, mapped from its {"error_code","message"} envelope.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
NewClient builds a Client. trackingURI == "" reads MLFLOW_TRACKING_URI from the environment; empty in both is an error. A trailing slash is trimmed and "/api/2.0/mlflow/<path>" is appended by the transport.
func (*Client) CreateExperiment ¶
func (c *Client) CreateExperiment(ctx context.Context, name string, tags ...ExperimentTag) (string, error)
CreateExperiment creates a new experiment with the given name and optional tags. Returns the new experiment's ID.
func (*Client) CreateModelVersion ¶
func (c *Client) CreateModelVersion(ctx context.Context, name, source string, runID string, tags ...ModelVersionTag) (*ModelVersion, error)
CreateModelVersion registers a new model version. runID may be empty.
func (*Client) CreateRegisteredModel ¶
func (c *Client) CreateRegisteredModel(ctx context.Context, name string, tags ...RegisteredModelTag) (*RegisteredModel, error)
CreateRegisteredModel creates a new registered model with the given name and optional tags.
func (*Client) CreateRun ¶
func (c *Client) CreateRun(ctx context.Context, experimentID string, opts ...RunOption) (*Run, error)
CreateRun creates a new run under experimentID.
func (*Client) DeleteExperiment ¶
DeleteExperiment marks an experiment as deleted.
func (*Client) DeleteModelVersion ¶
DeleteModelVersion deletes a specific model version.
func (*Client) DeleteModelVersionTag ¶
DeleteModelVersionTag deletes a tag from a model version.
func (*Client) DeleteRegisteredModel ¶
DeleteRegisteredModel deletes a registered model by name.
func (*Client) DeleteRegisteredModelAlias ¶
DeleteRegisteredModelAlias removes an alias from a registered model.
func (*Client) DeleteRegisteredModelTag ¶
DeleteRegisteredModelTag deletes a tag from a registered model.
func (*Client) DeleteRunTag ¶
DeleteRunTag removes a tag from a run.
func (*Client) DownloadArtifact ¶
DownloadArtifact fetches the raw bytes of a single artifact file.
func (*Client) GetExperimentByName ¶
GetExperimentByName fetches an experiment by name.
func (*Client) GetLatestVersions ¶
func (c *Client) GetLatestVersions(ctx context.Context, name string, stages ...string) ([]ModelVersion, error)
GetLatestVersions returns the latest model versions for a registered model, optionally filtered to the given stages.
func (*Client) GetMetricHistory ¶
GetMetricHistory returns the full history of a metric from metrics/get-history.
func (*Client) GetModelVersion ¶
GetModelVersion retrieves a specific model version by name and version number.
func (*Client) GetModelVersionByAlias ¶
func (c *Client) GetModelVersionByAlias(ctx context.Context, name, alias string) (*ModelVersion, error)
GetModelVersionByAlias retrieves the model version that the given alias points to.
func (*Client) GetModelVersionDownloadURI ¶
func (c *Client) GetModelVersionDownloadURI(ctx context.Context, name, version string) (string, error)
GetModelVersionDownloadURI returns the URI for downloading a model version's artifacts.
func (*Client) GetOrCreateExperiment ¶
GetOrCreateExperiment returns the ID of an existing experiment by name, creating it if it does not exist.
func (*Client) GetRegisteredModel ¶
GetRegisteredModel retrieves a registered model by name.
func (*Client) ListArtifacts ¶
func (c *Client) ListArtifacts(ctx context.Context, runID, path string) (files []FileInfo, nextPageToken string, err error)
ListArtifacts returns the artifact listing for runID under path. Pass path=="" to list the root. nextPageToken is non-empty when more pages remain.
func (*Client) LogBatch ¶
func (c *Client) LogBatch(ctx context.Context, runID string, metrics []Metric, params []Param, tags []RunTag) error
LogBatch posts metrics, params, and tags in chunks that satisfy the MLflow API limits per call: ≤1000 metrics, ≤100 params, ≤100 tags, ≤1000 total. Chunks are sent sequentially; the first error aborts and is returned.
func (*Client) LogMetric ¶
func (c *Client) LogMetric(ctx context.Context, runID, key string, value float64, timestampMs, step int64) error
LogMetric posts a single metric value to runs/log-metric.
Example ¶
ExampleClient_LogMetric streams a metric across steps during a training loop.
package main
import (
"context"
"log"
"time"
"github.com/guygrigsby/mlflow"
)
func main() {
c, err := mlflow.NewClient("")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
runID := "abc123"
losses := []float64{2.1, 1.7, 1.4}
for step, loss := range losses {
ts := time.Now().UnixMilli()
if err := c.LogMetric(ctx, runID, "loss", loss, ts, int64(step)); err != nil {
log.Fatal(err)
}
}
}
Output:
func (*Client) RenameRegisteredModel ¶
func (c *Client) RenameRegisteredModel(ctx context.Context, name, newName string) (*RegisteredModel, error)
RenameRegisteredModel renames a registered model.
func (*Client) RestoreExperiment ¶
RestoreExperiment restores a deleted experiment.
func (*Client) RestoreRun ¶
RestoreRun undeletes a previously deleted run.
func (*Client) SearchExperiments ¶
func (c *Client) SearchExperiments(ctx context.Context, req SearchExperimentsRequest) ([]Experiment, string, error)
SearchExperiments searches experiments with the given criteria and returns matching experiments and the next-page token (empty string when no more pages).
func (*Client) SearchModelVersions ¶
func (c *Client) SearchModelVersions(ctx context.Context, filter string, maxResults int64, orderBy []string, pageToken string) ([]ModelVersion, string, error)
SearchModelVersions searches model versions with optional filter, ordering, and pagination. Returns the matching versions and the next page token.
func (*Client) SearchRegisteredModels ¶
func (c *Client) SearchRegisteredModels(ctx context.Context, filter string, maxResults int64, orderBy []string, pageToken string) ([]RegisteredModel, string, error)
SearchRegisteredModels searches registered models with optional filter, ordering, and pagination. Returns the matching models and the next page token (empty when there are no more pages).
func (*Client) SearchRuns ¶
SearchRuns searches runs matching req. Returns the matching runs and an opaque next-page token (empty when no further pages exist).
func (*Client) SetExperimentTag ¶
SetExperimentTag sets a tag on an experiment.
func (*Client) SetModelVersionTag ¶
SetModelVersionTag sets a tag on a model version.
func (*Client) SetRegisteredModelAlias ¶
SetRegisteredModelAlias creates or updates an alias pointing to a specific model version.
func (*Client) SetRegisteredModelTag ¶
SetRegisteredModelTag sets a tag on a registered model.
func (*Client) SetTag ¶
SetTag posts a single run tag to runs/set-tag. It is an alias for SetRunTag.
func (*Client) TransitionModelVersionStage ¶
func (c *Client) TransitionModelVersionStage(ctx context.Context, name, version, stage string, archiveExisting bool) (*ModelVersion, error)
TransitionModelVersionStage transitions a model version to a new stage. When archiveExisting is true, all other versions in the target stage are archived.
func (*Client) UpdateExperiment ¶
UpdateExperiment renames an experiment.
func (*Client) UpdateModelVersion ¶
func (c *Client) UpdateModelVersion(ctx context.Context, name, version, description string) (*ModelVersion, error)
UpdateModelVersion updates the description of a model version.
func (*Client) UpdateRegisteredModel ¶
func (c *Client) UpdateRegisteredModel(ctx context.Context, name, description string) (*RegisteredModel, error)
UpdateRegisteredModel updates the description of a registered model.
type Dataset ¶
type Dataset struct {
Name string `json:"name"`
Digest string `json:"digest,omitempty"`
SourceType string `json:"source_type,omitempty"`
Source string `json:"source,omitempty"`
Schema string `json:"schema,omitempty"`
Profile string `json:"profile,omitempty"`
}
Dataset describes a logged dataset.
type DatasetInput ¶
DatasetInput is a dataset associated with a run via LogInputs.
type Experiment ¶
type Experiment struct {
ExperimentID string `json:"experiment_id"`
Name string `json:"name"`
ArtifactLocation string `json:"artifact_location,omitempty"`
LifecycleStage string `json:"lifecycle_stage,omitempty"`
Tags []ExperimentTag `json:"tags,omitempty"`
}
type ExperimentTag ¶
type FileInfo ¶
type FileInfo struct {
Path string `json:"path"`
IsDir bool `json:"is_dir"`
FileSize int64 `json:"file_size,omitempty"`
}
FileInfo describes a single artifact entry returned by ListArtifacts.
type ModelVersion ¶
type ModelVersion struct {
Name string `json:"name"`
Version string `json:"version"`
CreationTimestamp int64 `json:"creation_timestamp,omitempty"`
LastUpdatedTimestamp int64 `json:"last_updated_timestamp,omitempty"`
CurrentStage string `json:"current_stage,omitempty"`
Description string `json:"description,omitempty"`
Source string `json:"source,omitempty"`
RunID string `json:"run_id,omitempty"`
Status string `json:"status,omitempty"`
Tags []ModelVersionTag `json:"tags,omitempty"`
Aliases []string `json:"aliases,omitempty"`
}
ModelVersion describes a specific version of a registered model.
type ModelVersionTag ¶
ModelVersionTag is a key-value tag on a model version.
type Option ¶
type Option func(*config)
func WithBasicAuth ¶
WithBasicAuth sets HTTP basic auth on every request.
func WithBearerToken ¶
WithBearerToken sets an Authorization: Bearer <tok> header on every request.
func WithHTTPClient ¶
WithHTTPClient overrides the default &http.Client{Timeout: 30s}.
func WithMaxRetries ¶
WithMaxRetries sets the retry budget for 5xx + transport errors (default 3). Retries are not idempotent-aware: a write whose response is lost in transit (transport error or 5xx after the server applied it) may be re-sent, so a retried CreateRun/LogMetric/LogBatch can double-write. This is acceptable under the best-effort, accept-gaps contract; set 0 to disable.
type RegisteredModel ¶
type RegisteredModel struct {
Name string `json:"name"`
CreationTimestamp int64 `json:"creation_timestamp,omitempty"`
LastUpdatedTimestamp int64 `json:"last_updated_timestamp,omitempty"`
Description string `json:"description,omitempty"`
LatestVersions []ModelVersion `json:"latest_versions,omitempty"`
Tags []RegisteredModelTag `json:"tags,omitempty"`
Aliases []RegisteredModelAlias `json:"aliases,omitempty"`
}
RegisteredModel describes a model registered in the MLflow Model Registry.
type RegisteredModelAlias ¶
RegisteredModelAlias maps an alias name to a model version number.
type RegisteredModelTag ¶
RegisteredModelTag is a key-value tag on a registered model.
type RunInfo ¶
type RunInfo struct {
RunID string `json:"run_id"`
ExperimentID string `json:"experiment_id"`
RunName string `json:"run_name,omitempty"`
Status RunStatus `json:"status,omitempty"`
StartTime int64 `json:"start_time,omitempty"` // unix ms
EndTime int64 `json:"end_time,omitempty"` // unix ms
ArtifactURI string `json:"artifact_uri,omitempty"`
LifecycleStage string `json:"lifecycle_stage,omitempty"`
}
type RunOption ¶
type RunOption func(*runCreate)
RunOption configures a CreateRun request.
func WithStartTime ¶
WithStartTime sets the run start time as unix milliseconds.
type SearchExperimentsRequest ¶
type SearchExperimentsRequest struct {
Filter string `json:"filter,omitempty"`
MaxResults int64 `json:"max_results,omitempty"`
OrderBy []string `json:"order_by,omitempty"`
PageToken string `json:"page_token,omitempty"`
ViewType string `json:"view_type,omitempty"`
}
SearchExperimentsRequest is the request body for SearchExperiments.
type SearchRunsRequest ¶
type SearchRunsRequest struct {
ExperimentIDs []string `json:"experiment_ids"`
Filter string `json:"filter,omitempty"`
MaxResults int32 `json:"max_results,omitempty"`
OrderBy []string `json:"order_by,omitempty"`
PageToken string `json:"page_token,omitempty"`
RunViewType string `json:"run_view_type,omitempty"`
}
SearchRunsRequest is the request body for SearchRuns.