跳到主要内容

设置跟踪标签

标签是可变的键值对,您可以将其附加到跟踪上,以添加有价值的标签和上下文,用于对跟踪进行分组和过滤。例如,您可以根据用户输入的��题或正在处理的请求类型来标记跟踪,并将它们分组以进行分析和质量评估。

通过 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%'")

搜索跟踪 指南中查看支持的过滤语法的完整列表。