~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Headline generation utilities for news items.
Uses LLM to generate concise, meaningful headlines from long queries and findings.
"""
from typing import Any, Optional
from loguru import logger
from ...utilities.json_utils import get_llm_response_text
def generate_headline(
query: str,
findings: str = "",
max_length: int = 100,
settings_snapshot: Optional[dict] = None,
) -> str:
"""
Generate a concise headline from a query and optional findings.
The synchronous entry point uses ``invoke`` without running the async
core on a temporary event loop. Callers already on an event loop can
await the corresponding ``_async`` entry point. Resource cleanup uses
the shared close helper; it is separate from invocation dispatch.
Args:
query: The search query or research question
findings: Optional findings/content to help generate better headline
max_length: Maximum length for the headline
settings_snapshot: Optional settings snapshot so the LLM call
picks up the active egress policy. Background callers
should pass this through; without it ``get_llm`` takes its
snapshot-less path, which fails closed with
``PolicyDeniedError`` for any non-local provider, so
headline generation fails rather than reaching a cloud LLM.
Returns:
A concise headline string
"""
# Always try LLM generation first for dynamic headlines based on actual content
llm_headline = _generate_with_llm(
query, findings, max_length, settings_snapshot
)
return _finalize_headline(llm_headline)
async def generate_headline_async(
query: str,
findings: str = "",
max_length: int = 100,
settings_snapshot: Optional[dict] = None,
) -> str:
"""Async core of generate_headline: one awaited ainvoke (#5854).
For callers that already run on an event loop. Applies the same
failure sentinel as :func:`generate_headline` via
:func:`_finalize_headline`.
"""
llm_headline = await _generate_with_llm_async(
query, findings, max_length, settings_snapshot
)
return _finalize_headline(llm_headline)
def _finalize_headline(llm_headline: Optional[str]) -> str:
"""Apply the failure sentinel shared by both generate_headline paths."""
# No fallback - if LLM fails, indicate failure
if llm_headline:
return llm_headline
return "[Headline generation failed]"
def _build_headline_prompt(findings: str) -> Optional[str]:
"""Build the headline prompt, or None when there is nothing to summarize."""
# Focus only on the findings/report content, not the query
if not findings:
logger.debug("No findings provided for headline generation")
return None
# Use the COMPLETE findings - no character limit
findings_preview = findings
logger.debug(f"Generating headline with {len(findings)} chars of findings")
return f"""Generate a comprehensive news headline that captures the key events from the research report below.
Research Findings:
{findings_preview}
Requirements:
- Include MULTIPLE major events if several important things happened (e.g., "Earthquake Strikes California While Wildfires Rage; Global Markets Tumble Amid Political Tensions")
- Capture as much important information as possible in the headline
- Be specific about locations, impacts, and key details
- Professional news headline style but can be longer to include more information
- Focus on the most impactful findings from the report
- Use semicolons or commas to separate multiple major events
- No quotes or punctuation at start/end
- Base the headline ONLY on the actual findings in the report
Generate only the headline text, nothing else."""
def _clean_headline(response: Any) -> Optional[str]:
"""Clean and validate the headline the LLM returned."""
headline = get_llm_response_text(response).strip()
# Clean up the generated headline
headline = headline.strip("\"'.,!?")
# Validate the headline
if headline:
logger.debug(f"Generated headline: {headline}")
return headline
return None
def _generate_with_llm(
query: str,
findings: str,
max_length: int,
settings_snapshot: Optional[dict] = None,
) -> Optional[str]:
"""Generate headline using LLM (sync path)."""
try:
from ...config.llm_config import get_llm
# Use the configured model for headline generation
llm = get_llm(temperature=0.3, settings_snapshot=settings_snapshot)
try:
prompt = _build_headline_prompt(findings)
if prompt is None:
return None
response = llm.invoke(prompt)
return _clean_headline(response)
finally:
from ...utilities.resource_utils import safe_close
safe_close(llm, "headline LLM")
except Exception as e:
logger.debug(f"LLM headline generation failed: {e}")
return None
async def _generate_with_llm_async(
query: str,
findings: str,
max_length: int,
settings_snapshot: Optional[dict] = None,
) -> Optional[str]:
"""Generate headline using LLM (async path).
Mirrors :func:`_generate_with_llm`; only the LLM call line differs.
"""
try:
from ...config.llm_config import get_llm
# Use the configured model for headline generation
llm = get_llm(temperature=0.3, settings_snapshot=settings_snapshot)
try:
prompt = _build_headline_prompt(findings)
if prompt is None:
return None
response = await llm.ainvoke(prompt)
return _clean_headline(response)
finally:
from ...utilities.resource_utils import safe_close
safe_close(llm, "headline LLM")
except Exception as e:
logger.debug(f"LLM headline generation failed: {e}")
return None