| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- """
- 内容评估工具 - 评估内容质量和相关性
- """
- from typing import Dict, Any, List
- from agent.tools import tool, ToolResult, ToolContext
- @tool(description="评估内容质量和相关性")
- async def evaluate_content(
- content_items: List[Dict[str, Any]],
- keywords: List[str],
- ctx: ToolContext = None,
- ) -> ToolResult:
- """
- 评估内容质量和相关性
- Args:
- content_items: 内容列表
- keywords: 搜索关键词
- ctx: 工具上下文
- """
- evaluated_items = []
- for item in content_items:
- score = _calculate_quality_score(item, keywords)
- item["quality_score"] = score
- item["relevance_score"] = _calculate_relevance_score(item, keywords)
- evaluated_items.append(item)
- # 按质量分数排序
- evaluated_items.sort(key=lambda x: x["quality_score"], reverse=True)
- return ToolResult(
- title="内容评估完成",
- output=f"已评估 {len(evaluated_items)} 条内容",
- data={"evaluated_items": evaluated_items},
- )
- def _calculate_quality_score(item: Dict[str, Any], keywords: List[str]) -> float:
- """
- 计算内容质量分数
- 考虑因素:
- 1. 播放量、点赞数、评论数、分享数
- 2. 互动率(点赞/播放、评论/播放)
- 3. 发布时间(新鲜度)
- """
- stats = item.get("stats", {})
- views = stats.get("views", 0)
- likes = stats.get("likes", 0)
- comments = stats.get("comments", 0)
- shares = stats.get("shares", 0)
- # 基础分数:基于绝对数值
- base_score = (
- (views / 10000) * 0.3 +
- (likes / 1000) * 0.3 +
- (comments / 100) * 0.2 +
- (shares / 100) * 0.2
- )
- # 互动率加成
- engagement_rate = (likes + comments + shares) / max(views, 1)
- engagement_bonus = engagement_rate * 20
- # 总分
- total_score = min(base_score + engagement_bonus, 100)
- return round(total_score, 2)
- def _calculate_relevance_score(item: Dict[str, Any], keywords: List[str]) -> float:
- """
- 计算内容相关性分数
- 考虑因素:
- 1. 标题中关键词匹配度
- 2. 描述中关键词匹配度
- 3. 标签匹配度
- """
- title = item.get("title", "").lower()
- description = item.get("description", "").lower()
- tags = [t.lower() for t in item.get("tags", [])]
- score = 0.0
- keyword_count = len(keywords)
- for keyword in keywords:
- keyword_lower = keyword.lower()
- # 标题匹配(权重最高)
- if keyword_lower in title:
- score += 40 / keyword_count
- # 描述匹配
- if keyword_lower in description:
- score += 30 / keyword_count
- # 标签匹配
- if keyword_lower in tags:
- score += 30 / keyword_count
- return min(score, 100)
|