Coverage for src / local_deep_research / news / utils / headline_generator.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:55 +0000

1""" 

2Headline generation utilities for news items. 

3Uses LLM to generate concise, meaningful headlines from long queries and findings. 

4""" 

5 

6from typing import Optional 

7from loguru import logger 

8 

9 

10def generate_headline( 

11 query: str, findings: str = "", max_length: int = 100 

12) -> str: 

13 """ 

14 Generate a concise headline from a query and optional findings. 

15 

16 Args: 

17 query: The search query or research question 

18 findings: Optional findings/content to help generate better headline 

19 max_length: Maximum length for the headline 

20 

21 Returns: 

22 A concise headline string 

23 """ 

24 # Always try LLM generation first for dynamic headlines based on actual content 

25 llm_headline = _generate_with_llm(query, findings, max_length) 

26 if llm_headline: 

27 return llm_headline 

28 

29 # No fallback - if LLM fails, indicate failure 

30 return "[Headline generation failed]" 

31 

32 

33def _generate_with_llm( 

34 query: str, findings: str, max_length: int 

35) -> Optional[str]: 

36 """Generate headline using LLM.""" 

37 try: 

38 from ...config.llm_config import get_llm 

39 

40 # Use the configured model for headline generation 

41 llm = get_llm(temperature=0.3) 

42 

43 try: 

44 # Focus only on the findings/report content, not the query 

45 if not findings: 

46 logger.debug("No findings provided for headline generation") 

47 return None 

48 

49 # Use the COMPLETE findings - no character limit 

50 findings_preview = findings 

51 logger.debug( 

52 f"Generating headline with {len(findings)} chars of findings" 

53 ) 

54 

55 prompt = f"""Generate a comprehensive news headline that captures the key events from the research report below. 

56 

57Research Findings: 

58{findings_preview} 

59 

60Requirements: 

61- Include MULTIPLE major events if several important things happened (e.g., "Earthquake Strikes California While Wildfires Rage; Global Markets Tumble Amid Political Tensions") 

62- Capture as much important information as possible in the headline 

63- Be specific about locations, impacts, and key details 

64- Professional news headline style but can be longer to include more information 

65- Focus on the most impactful findings from the report 

66- Use semicolons or commas to separate multiple major events 

67- No quotes or punctuation at start/end 

68- Base the headline ONLY on the actual findings in the report 

69 

70Generate only the headline text, nothing else.""" 

71 

72 response = llm.invoke(prompt) 

73 headline: str = str(response.content).strip() 

74 

75 # Clean up the generated headline 

76 headline = headline.strip("\"'.,!?") 

77 

78 # Validate the headline 

79 if headline: 

80 logger.debug(f"Generated headline: {headline}") 

81 return headline 

82 finally: 

83 from ...utilities.resource_utils import safe_close 

84 

85 safe_close(llm, "headline LLM") 

86 

87 except Exception as e: 

88 logger.debug(f"LLM headline generation failed: {e}") 

89 

90 return None