共用方式為


程式化地進行搜尋追蹤

使用 mlflow.search_traces() 程式化地搜尋和分析追蹤。 此函式可查詢儲存在 MLflow 追蹤伺服器、推論表或 Unity 目錄資料表中的追蹤資料。 你可以選擇痕跡子集進行分析或建立評估資料集。

mlflow.search_traces() 應用程式介面

def mlflow.search_traces(
    experiment_ids: list[str] | None = None,
    filter_string: str | None = None,
    max_results: int | None = None,
    order_by: list[str] | None = None,
    extract_fields: list[str] | None = None,
    run_id: str | None = None,
    return_type: Literal['pandas', 'list'] | None = None,
    model_id: str | None = None,
    sql_warehouse_id: str | None = None,
    include_spans: bool = True,
    locations: list[str] | None = None,
) -> pandas.DataFrame | list[Trace]

mlflow.search_traces() 讓你能在幾個維度上篩選和選擇資料:

  • 以查詢字串篩選
  • 依地點篩選:實驗、運行、建模或 Unity 目錄架構
  • 限制資料:最大結果、包含或排除跨度
  • 調整返回值格式:資料格式、資料順序

search_traces() 回傳 pandas 資料框架或物件清單 Trace ,這些物件可進一步分析或重新塑造成評估資料集。 請參閱這些返回類型的 結構細節

完整細節請參閱mlflow.search_traces() API 文件

mlflow.search_traces() 參數

類別 parameter: type Description Example
以查詢字串篩選 filter_string: str 請參閱 搜尋查詢語法 ,包括支援的篩選條件與比較器。 attributes.status = 'OK' AND tags.environment = 'production'
按地點篩選 locations: list[str] 這個參數可以是實驗 ID 清單或 Unity 目錄 catalog.schema 中用於篩選的位置。 利用此功能搜尋推論或 Unity 目錄資料表中儲存的痕跡。 ['591498498138889', '782498488231546']['my_catalog.my_schema']
run_id: str MLflow 執行識別碼 35464a26b0144533b09d8acbb4681985
model_id: str MLflow 模型識別碼 acc4c426-5dd7-4a3a-85de-da1b22ce05f1
極限資料 max_results: int 回傳的最大追蹤數(列數) 100
include_spans: bool 可從結果中包含或排除跨度。 跨度包含追蹤細節,且可使結果資料大小大幅增加。 True
回傳值格式 order_by: list[str] 請參考 語法與支援的鍵 ["timestamp_ms DESC", "status ASC"]
return_type: Literal['pandas', 'list'] 此函式可回傳 pandas DataFrame 或物件清單 Trace 。 請參閱 結構細節 'pandas'
已淘汰 experiment_ids: list[str] 請改用 locations
extract_fields: list[str] 請選擇返回的資料框架或追蹤物件中的欄位。
sql_warehouse_id: str 改用 MLFLOW_TRACING_SQL_WAREHOUSE_ID 環境變數

最佳做法

關鍵字論證

務必使用以mlflow.search_traces()命名的關鍵字參數。 它允許位置參數,但函數參數仍在演進中。

良好做法: mlflow.search_traces(filter_string="attributes.status = 'OK'")

不良做法: mlflow.search_traces([], "attributes.status = 'OK'")

filter_string 潛在問題

當使用 filter_string 這個參數來進行 mlflow.search_traces() 搜尋時,請記得:

  • 使用前綴: attributes.tags.metadata.
  • 如果標籤或屬性名稱包含點,請使用反引號: tags.`mlflow.traceName`
  • 只使用單引號: 'value' 不使用 "value"
  • Unix 時間戳(毫秒)用來表示時間,1749006880539 而非日期
  • 只使用 AND:不支援 OR

更多細節請參閱 搜尋查詢語法

SQL 倉庫整合

mlflow.search_traces() 可選擇性地使用 Databricks SQL 倉庫 ,以提升推論表或 Unity 目錄資料表中大型追蹤資料集的效能。 用 MLFLOW_TRACING_SQL_WAREHOUSE_ID 環境變數指定你的 SQL 倉庫 ID。

使用 Databricks SQL 倉庫執行追蹤查詢,以提升大型追蹤資料集的效能:

import os

os.environ['MLFLOW_TRACING_SQL_WAREHOUSE_ID'] = 'fa92bea7022e81fb'

# Use SQL warehouse for better performance
traces = mlflow.search_traces(
    filter_string="attributes.status = 'OK'",
    locations=['my_catalog.my_schema'],
)

分頁

mlflow.search_traces() 回傳的結果存於記憶體中,這對於較小的結果集很有效。 要處理大量結果集,請使用 MlflowClient.search_traces() ,因為它支援分頁。

後續步驟