llm_enrich.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. from typing import Any, Dict
  3. from news_mcp.config import NEWS_ENTITY_BLACKLIST
  4. from news_mcp.llm import call_extraction, call_summary
  5. def _filter_entities(entities, blacklist=None):
  6. banned = set(x.strip().lower() for x in (blacklist if blacklist is not None else NEWS_ENTITY_BLACKLIST))
  7. out = []
  8. for ent in entities or []:
  9. key = str(ent).strip().lower()
  10. if not key or key in banned:
  11. continue
  12. out.append(ent)
  13. return out
  14. async def classify_cluster_groq(cluster: Dict[str, Any]) -> Dict[str, Any]:
  15. parsed = await call_extraction(cluster)
  16. out = dict(cluster)
  17. out.update({
  18. "topic": parsed.get("topic", cluster.get("topic")),
  19. "entities": _filter_entities(parsed.get("entities", [])),
  20. "sentiment": parsed.get("sentiment", "neutral"),
  21. "sentimentScore": parsed.get("sentimentScore"),
  22. "keywords": parsed.get("keywords", []),
  23. })
  24. return out
  25. async def summarize_cluster_groq(cluster: Dict[str, Any]) -> Dict[str, Any]:
  26. parsed = await call_summary(cluster)
  27. return parsed