LPXian/Graph-News.AI
0
1from langchain_core.messages import HumanMessage2from app.core.llm import get_llm3from app.graph.state import GraphState4 5def summarizer_node(state: GraphState) -> GraphState:6 """7 Summarizes individual articles into concise bullet points.8 """9 print("--- SUMMARIZER NODE ---")10 llm = get_llm()11 articles = state.get("clean_articles", [])[:15] # Limit to 15 articles to avoid token issues12 13 summarized_items = []14 15 # Improved structured summarization prompt16 prompt = """Summarize the following AI news articles. 17 For each article, provide a one-sentence technical summary.18 You MUST preserve the URL.19 Return the result in this exact format for each article:20 - [SUMMARY] summary_text_here | [TITLE] article_title_here | [URL] url_here21 """22 23 article_text = ""24 for i, art in enumerate(articles):25 # Ensure we fall back to a string if 'link' is missing, but it should be there.26 link = art.get('link') or art.get('url') or "NO_LINK_AVAILABLE"27 article_text += f"Article {i+1}:\nTitle: {art.get('title')}\nLink: {link}\nContent: {art.get('summary', '')[:400]}\n\n"28 29 response = llm.invoke([HumanMessage(content=prompt + article_text)])30 31 # Extract token usage if available32 tokens = response.usage_metadata.get("total_tokens", 0) if hasattr(response, "usage_metadata") else 033 34 # Extract the lines and return as state.35 summarized_items = response.content.strip().split('\n')36 37 print(f"Processed summaries with explicit URLs. Tokens: {tokens}")38 return {39 "summarized_items": summarized_items,40 "total_tokens": state.get("total_tokens", 0) + tokens41 }42 