后端与接口
用 MLflow CLI 按条件检索与调试 trace(按…
当需要从 MLflow 实验里按 trace ID 取详情,或按会话、用户、状态、时间、耗时、标签、反馈等条件搜索 trace 并调试失败请求时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
用 MLflow CLI 按条件检索与调特定
解决思路
- 先判断场景:有具体 trace ID 时用单条获取 `mlflow traces get --trace-id <id>`;需要按条件查找时用搜索 `mlflow traces search --experiment-id <id> --filter-string "..."`。
- 执行前先运行 `mlflow traces search --help`,确认当前安装的 MLflow 版本支持的实际参数。
- 了解 trace 数据结构:TraceInfo 含 `trace_id`、`status`(OK/ERROR)、`timestamp_ms`、`execution_time_ms`、`tags`、`metadata`、`assessments`;Spans 是带 `name`、`type`、`attributes`、`start_time`、`end_time` 的操作树。
- 按 run ID 过滤:`mlflow traces search --run-id <run_id>`;只看失败:加 `--filter-string "trace.status = 'ERROR'"`;也可与 `--experiment-id` 组合使用。
- 按会话或用户过滤(调试常用):`--filter-string "metadata.\`mlflow.trace.session\` = 'session_abc123'"` 或 `metadata.\`mlflow.trace.user\` = 'user_456'`;查会话内失败用 `... AND trace.status = 'ERROR'`;按时间排序用 `--order-by "timestamp_ms ASC"`。
- 按状态过滤:`--filter-string "trace.status = 'ERROR'"` 或 `"trace.status = 'OK'"`。
- 按时间范围过滤(毫秒时间戳):如最近一小时 `--filter-string "trace.timestamp_ms > $(( $(date +%s)000 - 3600000 ))"`。
- 按执行耗时过滤慢 trace:`--filter-string "trace.execution_time_ms > 1000"`。
- 按标签和元数据过滤:`tag.environment = 'production'`、`metadata.user_id = 'user_123'`;键名含特殊字符时用反引号转义,如 `tag.\`model-name\` = 'gpt-4'`、`metadata.\`user.id\` = 'abc'`。
- 按评估/反馈过滤:`--filter-string "feedback.rating = 'positive'"`。
- 全文搜索:`--filter-string "trace.text LIKE '%error%'"`。
- 分页:用 `--max-results 50` 限制每页数量;输出中若有 `Next page token: <token>`,用 `--page-token "<token>"` 取下一页。
- 输出控制:`--output json` 指定格式,`--include-spans` 包含 span 详情,`--order-by "timestamp_ms DESC"` 排序。
- 需要单条 trace 详情时执行 `mlflow traces get --trace-id <trace_id>`。
- 需要更详细的过滤语法时,从文档 https://mlflow.org/docs/latest/genai/tracing/search-traces.md 获取支持的字段、操作符和示例;常用字段包括 `trace.status`、`trace.execution_time_ms`、`trace.timestamp_ms`、`metadata.\`mlflow.trace.session\``、`metadata.\`mlflow.trace.user\``、`tag.<key>`、`metadata.<key>`、`span.name`、`span.type`、`feedback.<name>`、`expectation.<name>`;模式操作符有 `LIKE`、`ILIKE`(不区分大小写)、`RLIKE`(正则)。
- 使用 Python API 时参考 `mlflow.search_traces()` 文档:https://mlflow.org/docs/latest/genai/tracing/search-traces.md。
适用边界 · 注意事项
- 没有可访问的 MLflow 服务或实验数据时无法使用。
- 未安装 MLflow CLI 或对应 Python 包时无法执行这些命令。