虽然 工作空间 帮助分离信任边界和访问控制,而标签帮助您在工作空间内组织资源。标签是您可以附加到资源的键值对。
您可以通过 LangSmith UI 或通过 REST API:
UI
以编程方式管理资源标签
标签部分
- 创建标签: **导航到工作空间** 设置 **页面,然后点击左侧边栏中的** 资源标签
- 。在这里,您会找到按键分组的现有标签值。LangSmith 默认创建 **Application** 和 **Environment** 键。您可以使用 **Application** 键来筛选 UI 中显示的资源。
- 选择 **新建标签** 在页面顶部。系统会提示您输入标签的键和值。请注意,您可以使用现有键或创建新键。
为资源分配标签
在创建新标签的同一侧边栏中,您还可以将资源分配给标签。在 **分配资源** 部分搜索相应资源并选择要标记的资源。
要取消分配资源上的标签,请点击 标签面板和资源标签面板中该标签旁边的垃圾桶图标。
删除标签
您可以从 **设置** 页面 > **资源标签** 页面删除标签的键或值。要删除键,请点击 该键旁边的垃圾桶图标。要删除值,请点击 该值旁边的垃圾桶图标。
如果您删除一个键,LangSmith 将删除与该键关联的所有值。删除值时,您将失去该值与资源之间的所有关联。
API
通过 API 管理标签
您可以使用 LangSmith REST API 以编程方式创建、分配和查询资源标签。所有标签端点都位于 /api/v1/workspaces/current/ 下,并且需要 API 密钥.
设置
导入 requests 库并配置您的 API 密钥和请求头。以下所有示例都假设这些变量在作用域内:
LANGSMITH_API_URL = "https://api.smith.langchain.com"
LANGSMITH_API_KEY = os.environ["LANGSMITH_API_KEY"]
headers = {"x-api-key": LANGSMITH_API_KEY, "Content-Type": "application/json"}
创建标签键
response = requests.post(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tag-keys",
headers=headers,
json={"key": "Environment", "description": "Deployment environment"},
)
response.raise_for_status()
tag_key = response.json()
tag_key_id = tag_key["id"]
创建标签值
response = requests.post(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tag-keys/{tag_key_id}/tag-values",
headers=headers,
json={"value": "production"},
)
response.raise_for_status()
tag_value = response.json()
tag_value_id = tag_value["id"]
为资源分配标签
response = requests.post(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/taggings",
headers=headers,
json={
"tag_value_id": tag_value_id,
"resource_type": "project",
"resource_id": "<project-uuid>",
},
)
response.raise_for_status()
tagging_id = response.json()["id"]
有效 resource_type values: project, dataset, prompt, experiment, queue, deployment, dashboard, evaluator, mcp_server, fleet_integration.
应用现有标签:如果键和值已存在(例如默认的 **Application** 键),请先获取其 ID,然后直接跳到分配步骤:
tags = requests.get(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tags",
headers=headers,
).json()
# tags is a list of {id, key, values: [{id, value}, ...]}
查询标签
# All tag keys and values in the workspace
tags = requests.get(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tags",
headers=headers,
).json()
# Tags on a specific resource
resource_tags = requests.get(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tags/resource",
headers=headers,
params={"resource_type": "project", "resource_id": "<project-uuid>"},
).json()
# All resources tagged with a specific value
taggings = requests.get(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/taggings",
headers=headers,
params={"tag_value_id": tag_value_id},
).json()
从资源移除标签
requests.delete(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/taggings/{tagging_id}",
headers=headers,
)
删除标签键或值
# Delete a value (removes all resource assignments for that value)
requests.delete(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tag-keys/{tag_key_id}/tag-values/{tag_value_id}",
headers=headers,
)
# Delete a key (also deletes all its values)
requests.delete(
f"{LANGSMITH_API_URL}/api/v1/workspaces/current/tag-keys/{tag_key_id}",
headers=headers,
)
For a full list of request/response fields, refer to the API 参考.