跳到主要内容

设置追踪标签

标签是可变的键值对,您可以将其附加到追踪(traces)上,以添加有价值的标签和上下文,从而对追踪进行分组和筛选。例如,您可以根据用户输入的主题或正在处理的请求类型来标记追踪,并将它们组合在一起进行分析和质量评估。

通过 MLflow UI 设置标签

在进行中的追踪上设置标签

使用 mlflow.update_current_trace() 在追踪执行过程中添加标签。

python
import mlflow


@mlflow.trace
def my_func(x):
mlflow.update_current_trace(tags={"fruit": "apple"})
return x + 1


result = my_func(5)
注意

如果键已存在,mlflow.update_current_trace() 函数将使用新值更新该键。

在已完成的追踪上设置标签

对已经完成并记录的追踪添加或修改标签。

python
import mlflow

mlflow.set_trace_tag(trace_id="your-trace-id", key="tag_key", value="tag_value")

检索标签

标签存储在追踪对象的 info.tags 属性中。

python
import mlflow

trace = mlflow.get_trace(trace_id="your-trace-id")
print(trace.info.tags)
# Output: {'tag_key': 'tag_value'}

使用标签进行搜索和筛选

使用标签可以快速高效地找到特定的追踪。

python
# Search for traces with tag 'environment' set to 'production'
traces = mlflow.search_traces(filter_string="tags.environment = 'production'")

您还可以使用模式匹配功能,通过标签值查找追踪。

python
# Search for traces with tag that contains the word 'mlflow'
traces = mlflow.search_traces(filter_string="tags.topic LIKE '%mlflow%'")

请参阅搜索追踪指南,查看支持的完整过滤器语法列表。