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

26 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +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 # Focus only on the findings/report content, not the query 

44 if not findings: 

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

46 return None 

47 

48 # Use the COMPLETE findings - no character limit 

49 findings_preview = findings 

50 logger.debug( 

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

52 ) 

53 

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

55 

56Research Findings: 

57{findings_preview} 

58 

59Requirements: 

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

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

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

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

64- Focus on the most impactful findings from the report 

65- Use semicolons or commas to separate multiple major events 

66- No quotes or punctuation at start/end 

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

68 

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

70 

71 response = llm.invoke(prompt) 

72 headline = response.content.strip() 

73 

74 # Clean up the generated headline 

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

76 

77 # Validate the headline 

78 if headline: 

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

80 return headline 

81 

82 except Exception as e: 

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

84 

85 return None