aigc_channel.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. """
  2. AIGC-Channel 平台实现(9 个中文平台)
  3. 后端:aigc-channel.aiddit.com
  4. 平台:xhs / gzh / sph / github / toutiao / douyin / bili / zhihu / weibo
  5. """
  6. import json
  7. from typing import Any, Dict, List, Optional
  8. import httpx
  9. from agent.tools.models import ToolResult
  10. from agent.tools.utils.image import build_image_grid, encode_base64, load_images
  11. from agent.tools.builtin.content.registry import (
  12. PlatformDef, ParamSpec, register_platform,
  13. )
  14. BASE_URL = "http://aigc-channel.aiddit.com/aigc/channel"
  15. DEFAULT_TIMEOUT = 60.0
  16. # ── 平台注册 ──
  17. _XHS_SEARCH_PARAMS = {
  18. "sort_type": ParamSpec(
  19. values=["综合排序", "最新发布", "最多点赞"],
  20. default="综合排序",
  21. ),
  22. "publish_time": ParamSpec(
  23. values=["不限", "近1天", "近7天", "近30天"],
  24. default="不限",
  25. ),
  26. "content_type": ParamSpec(
  27. values=["不限", "图文", "视频", "文章"],
  28. default="不限",
  29. ),
  30. "filter_note_range": ParamSpec(
  31. values=["不限", "1分钟以内", "1-5分钟", "5分钟以上"],
  32. default="不限",
  33. note="仅视频内容生效",
  34. ),
  35. }
  36. _COMMON_CONTENT_TYPE = {
  37. "content_type": ParamSpec(
  38. values=["视频", "图文"],
  39. default="",
  40. note="留空不限",
  41. ),
  42. }
  43. # 9 个中文平台定义
  44. _AIGC_PLATFORMS = [
  45. PlatformDef(id="xhs", name="小红书", aliases=["RED", "xiaohongshu"], search_params=_XHS_SEARCH_PARAMS, supports_suggest=True),
  46. PlatformDef(id="gzh", name="公众号", aliases=["微信公众号", "wechat"], search_params=_COMMON_CONTENT_TYPE),
  47. PlatformDef(id="sph", name="视频号", aliases=["微信视频号"], search_params=_COMMON_CONTENT_TYPE),
  48. PlatformDef(id="github", name="GitHub", aliases=["gh"], search_params=_COMMON_CONTENT_TYPE),
  49. PlatformDef(id="toutiao", name="头条", aliases=["今日头条", "toutiao"], search_params=_COMMON_CONTENT_TYPE, supports_suggest=True),
  50. PlatformDef(id="douyin", name="抖音", aliases=["TikTok"], search_params=_COMMON_CONTENT_TYPE, supports_suggest=True),
  51. PlatformDef(id="bili", name="B站", aliases=["哔哩哔哩", "bilibili"], search_params=_COMMON_CONTENT_TYPE, supports_suggest=True),
  52. PlatformDef(id="zhihu", name="知乎", aliases=[], search_params=_COMMON_CONTENT_TYPE, supports_suggest=True),
  53. PlatformDef(id="weibo", name="微博", aliases=["sina"], search_params=_COMMON_CONTENT_TYPE),
  54. ]
  55. # suggest API 额外支持 wx(微信搜一搜),但它不是搜索平台
  56. _SUGGEST_ONLY_CHANNELS = {"wx": "微信"}
  57. # ── 搜索实现 ──
  58. async def search(
  59. platform_id: str,
  60. keyword: str,
  61. max_count: int = 20,
  62. cursor: str = "",
  63. extras: Optional[Dict[str, Any]] = None,
  64. ) -> ToolResult:
  65. """AIGC-Channel 统一搜索"""
  66. extras = extras or {}
  67. if platform_id == "xhs":
  68. payload = {
  69. "type": platform_id,
  70. "keyword": keyword,
  71. "cursor": cursor,
  72. "content_type": extras.get("content_type", "不限"),
  73. "sort_type": extras.get("sort_type", "综合排序"),
  74. "publish_time": extras.get("publish_time", "不限"),
  75. "filter_note_range": extras.get("filter_note_range", "不限"),
  76. }
  77. else:
  78. payload = {
  79. "type": platform_id,
  80. "keyword": keyword,
  81. "cursor": cursor or "0",
  82. "max_count": max_count,
  83. "content_type": extras.get("content_type", ""),
  84. }
  85. try:
  86. async with httpx.AsyncClient(timeout=DEFAULT_TIMEOUT) as client:
  87. response = await client.post(
  88. f"{BASE_URL}/data",
  89. json=payload,
  90. headers={"Content-Type": "application/json"},
  91. )
  92. response.raise_for_status()
  93. data = response.json()
  94. except httpx.HTTPStatusError as e:
  95. return ToolResult(title="搜索失败", output="", error=f"HTTP {e.response.status_code}: {e.response.text}")
  96. except Exception as e:
  97. return ToolResult(title="搜索失败", output="", error=str(e))
  98. posts = data.get("data", [])
  99. # 构建概览摘要
  100. summary_list = []
  101. # 动态导入评价模块
  102. try:
  103. from examples.process_pipeline.script.evaluate_source_quality import SourceQualityEvaluator
  104. evaluator = SourceQualityEvaluator()
  105. except ImportError:
  106. evaluator = None
  107. for idx, post in enumerate(posts, 1):
  108. body = post.get("body_text", "") or ""
  109. title = post.get("title") or body[:20] or ""
  110. score_info = {}
  111. if evaluator:
  112. try:
  113. eval_res = evaluator.evaluate_post(post)
  114. score_info = {
  115. "quality_score": eval_res["total_score"],
  116. "quality_grade": eval_res["grade"]
  117. }
  118. post["_quality_score"] = eval_res["total_score"]
  119. post["_quality_grade"] = eval_res["grade"]
  120. except Exception:
  121. pass
  122. summary_item = {
  123. "index": idx,
  124. "title": title,
  125. "body_text": body[:100] + ("..." if len(body) > 100 else ""),
  126. "like_count": post.get("like_count"),
  127. "comment_count": post.get("comment_count"),
  128. "channel": post.get("channel"),
  129. "link": post.get("link"),
  130. "content_type": post.get("content_type"),
  131. }
  132. summary_item.update(score_info)
  133. summary_list.append(summary_item)
  134. # 封面拼图
  135. images = []
  136. try:
  137. collage_obj = await _build_collage(posts)
  138. if collage_obj:
  139. images.append(collage_obj)
  140. except Exception as e:
  141. import logging
  142. logging.getLogger(__name__).warning("Error generating collage: %s", e)
  143. return ToolResult(
  144. title=f"搜索: {keyword} ({platform_id})",
  145. output=json.dumps({"data": summary_list}, ensure_ascii=False, indent=2),
  146. long_term_memory=f"Searched '{keyword}' on {platform_id}, {len(posts)} results. Use content_detail to view full details.",
  147. images=images,
  148. metadata={"posts": posts}, # 完整数据传给上层缓存
  149. )
  150. # ── 详情实现(从缓存获取,不需要额外 HTTP) ──
  151. MAX_DETAIL_IMAGES = 10 # detail 中保留的图片总数上限(含拼图)
  152. KEEP_INDIVIDUAL = 8 # 单张图片保留数量;剩余图片合并为 1 张拼图
  153. async def _build_images_collage(urls: List[str]) -> Optional[Dict[str, Any]]:
  154. """将一组图片 URL 拼成单张网格图"""
  155. if not urls:
  156. return None
  157. loaded = await load_images(urls)
  158. valid_images = [img for (_, img) in loaded if img is not None]
  159. if not valid_images:
  160. return None
  161. grid = build_image_grid(images=valid_images, labels=None)
  162. import io
  163. buf = io.BytesIO()
  164. grid.save(buf, format="PNG")
  165. img_bytes = buf.getvalue()
  166. try:
  167. from agent.tools.builtin.file.image_cdn import _upload_bytes_to_oss
  168. import hashlib
  169. md5_hash = hashlib.md5(img_bytes).hexdigest()[:12]
  170. filename = f"collage_detail_{md5_hash}.png"
  171. cdn_url = await _upload_bytes_to_oss(img_bytes, filename)
  172. return {"type": "url", "url": cdn_url}
  173. except Exception as e:
  174. import logging
  175. logging.getLogger(__name__).warning("Failed to upload detail collage to CDN: %s", e)
  176. b64, _ = encode_base64(grid, format="PNG")
  177. return {"type": "base64", "media_type": "image/png", "data": b64}
  178. async def detail(post: Dict[str, Any], extras: Optional[Dict[str, Any]] = None) -> ToolResult:
  179. """返回单条帖子的完整内容"""
  180. title = post.get("title") or post.get("body_text", "")[:30] or "无标题"
  181. img_urls = [u for u in post.get("images", []) if u]
  182. images = []
  183. if len(img_urls) > MAX_DETAIL_IMAGES:
  184. # 保留前 KEEP_INDIVIDUAL 张原图,剩余拼成 1 张网格图
  185. for u in img_urls[:KEEP_INDIVIDUAL]:
  186. images.append({"type": "url", "url": u})
  187. collage = await _build_images_collage(img_urls[KEEP_INDIVIDUAL:])
  188. if collage:
  189. images.append(collage)
  190. else:
  191. for u in img_urls:
  192. images.append({"type": "url", "url": u})
  193. output_json = json.dumps(post, ensure_ascii=False, indent=2)
  194. output_text = (
  195. output_json
  196. + "\n\n---\n请基于以上内容,从信息完整度、内容质量和实用价值三个角度,给出一句简短的内容评价。"
  197. )
  198. return ToolResult(
  199. title=f"详情: {title}",
  200. output=output_text,
  201. long_term_memory=f"Viewed detail: {title}",
  202. images=images,
  203. )
  204. # ── 建议词实现 ──
  205. async def suggest(channel: str, keyword: str) -> ToolResult:
  206. """获取搜索建议词"""
  207. try:
  208. async with httpx.AsyncClient(timeout=DEFAULT_TIMEOUT) as client:
  209. response = await client.post(
  210. f"{BASE_URL}/suggest",
  211. json={"type": channel, "keyword": keyword},
  212. headers={"Content-Type": "application/json"},
  213. )
  214. response.raise_for_status()
  215. data = response.json()
  216. except Exception as e:
  217. return ToolResult(title="建议词获取失败", output="", error=str(e))
  218. suggestion_count = sum(len(item.get("list", [])) for item in data.get("data", []))
  219. return ToolResult(
  220. title=f"建议词: {keyword} ({channel})",
  221. output=json.dumps(data, ensure_ascii=False, indent=2),
  222. long_term_memory=f"Got {suggestion_count} suggestions for '{keyword}' on {channel}",
  223. )
  224. # ── 拼图辅助 ──
  225. async def _build_collage(posts: List[Dict[str, Any]]) -> Optional[str]:
  226. """封面图网格拼图"""
  227. urls, titles = [], []
  228. for post in posts:
  229. imgs = post.get("images", [])
  230. if imgs and imgs[0]:
  231. urls.append(imgs[0])
  232. base_title = post.get("title", "") or ""
  233. score = post.get("_quality_score")
  234. if score is not None:
  235. title_with_score = f"[{score}分] {base_title}"
  236. else:
  237. title_with_score = base_title
  238. titles.append(title_with_score)
  239. if not urls:
  240. return None
  241. loaded = await load_images(urls)
  242. valid_images, valid_labels = [], []
  243. for (_, img), title in zip(loaded, titles):
  244. if img is not None:
  245. valid_images.append(img)
  246. valid_labels.append(title)
  247. if not valid_images:
  248. return None
  249. grid = build_image_grid(images=valid_images, labels=valid_labels)
  250. import io
  251. buf = io.BytesIO()
  252. grid.save(buf, format="PNG")
  253. img_bytes = buf.getvalue()
  254. # 尝试上传到 CDN,替换冗长的 base64
  255. try:
  256. from agent.tools.builtin.file.image_cdn import _upload_bytes_to_oss
  257. import hashlib
  258. md5_hash = hashlib.md5(img_bytes).hexdigest()[:12]
  259. filename = f"collage_search_{md5_hash}.png"
  260. cdn_url = await _upload_bytes_to_oss(img_bytes, filename)
  261. return {"type": "url", "url": cdn_url}
  262. except Exception as e:
  263. import logging
  264. logging.getLogger(__name__).warning("Failed to upload collage to CDN: %s", e)
  265. # 降级:还是用 base64 但可能会超长
  266. b64, _ = encode_base64(grid, format="PNG")
  267. return {"type": "base64", "media_type": "image/png", "data": b64}
  268. # ── 注册所有 AIGC 平台 ──
  269. def _register_all():
  270. for p in _AIGC_PLATFORMS:
  271. p.search_impl = search
  272. p.detail_impl = detail
  273. if p.supports_suggest:
  274. p.suggest_impl = suggest
  275. p.suggest_channels = [p.id]
  276. register_platform(p)
  277. # wx 只有 suggest,没有搜索
  278. # suggest 调用时 channel 传 "wx",但不注册为独立平台
  279. _register_all()