|
@@ -165,3 +165,70 @@ async def get_category_tree(
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.exception("get_category_tree error")
|
|
logger.exception("get_category_tree error")
|
|
|
return ToolResult(title="获取分类树失败", output=f"错误: {e}")
|
|
return ToolResult(title="获取分类树失败", output=f"错误: {e}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@tool(description="获取指定分类下的所有元素,支持分页、排序和筛选")
|
|
|
|
|
+async def get_category_elements(
|
|
|
|
|
+ category_id: int,
|
|
|
|
|
+ source_type: str,
|
|
|
|
|
+ page_size: int = 50,
|
|
|
|
|
+ sort_by: str = "occurrence_count",
|
|
|
|
|
+ order: str = "desc",
|
|
|
|
|
+ min_occurrence: Optional[int] = None,
|
|
|
|
|
+) -> ToolResult:
|
|
|
|
|
+ """
|
|
|
|
|
+ 获取指定分类下的所有元素。
|
|
|
|
|
+
|
|
|
|
|
+ Args:
|
|
|
|
|
+ category_id: 分类的 entity_id
|
|
|
|
|
+ source_type: 维度,"实质" / "形式" / "意图"
|
|
|
|
|
+ page_size: 每页数量,1-200(默认50)
|
|
|
|
|
+ sort_by: 排序字段,"name" / "id" / "occurrence_count"(默认)
|
|
|
|
|
+ order: 排序方向,"asc" / "desc"(默认)
|
|
|
|
|
+ min_occurrence: 最小出现次数,用于过滤低频元素(可选)
|
|
|
|
|
+ """
|
|
|
|
|
+ params = {
|
|
|
|
|
+ "source_type": source_type,
|
|
|
|
|
+ "category_entity_id": category_id,
|
|
|
|
|
+ "page_size": page_size,
|
|
|
|
|
+ "sort_by": sort_by,
|
|
|
|
|
+ "order": order,
|
|
|
|
|
+ }
|
|
|
|
|
+ if min_occurrence is not None:
|
|
|
|
|
+ params["min_occurrence"] = min_occurrence
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ async with httpx.AsyncClient(timeout=30.0) as client:
|
|
|
|
|
+ resp = await client.get(f"{BASE_URL}/api/agent/search/elements", params=params)
|
|
|
|
|
+ resp.raise_for_status()
|
|
|
|
|
+ data = resp.json()
|
|
|
|
|
+
|
|
|
|
|
+ total = data.get("total", 0)
|
|
|
|
|
+ results = data.get("results", [])
|
|
|
|
|
+
|
|
|
|
|
+ lines = [f"分类 {category_id} 下的元素({source_type}维度)共 {total} 个,返回 {len(results)} 个:\n"]
|
|
|
|
|
+ for r in results:
|
|
|
|
|
+ eid = r.get("id", "")
|
|
|
|
|
+ name = r.get("name", "")
|
|
|
|
|
+ occ = r.get("occurrence_count", 0)
|
|
|
|
|
+ desc = r.get("description", "")
|
|
|
|
|
+ category = r.get("category", {})
|
|
|
|
|
+ cat_path = category.get("path", "")
|
|
|
|
|
+
|
|
|
|
|
+ lines.append(f"[元素] entity_id={eid} | {name} | 出现次数={occ}")
|
|
|
|
|
+ if desc:
|
|
|
|
|
+ lines.append(f" 描述: {desc}")
|
|
|
|
|
+ if cat_path:
|
|
|
|
|
+ lines.append(f" 所属分类: {cat_path}")
|
|
|
|
|
+ lines.append("")
|
|
|
|
|
+
|
|
|
|
|
+ return ToolResult(
|
|
|
|
|
+ title=f"分类元素: category_id={category_id} → {total} 个",
|
|
|
|
|
+ output="\n".join(lines),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ except httpx.HTTPError as e:
|
|
|
|
|
+ return ToolResult(title="获取分类元素失败", output=f"HTTP 错误: {e}")
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.exception("get_category_elements error")
|
|
|
|
|
+ return ToolResult(title="获取分类元素失败", output=f"错误: {e}")
|