Skip to content

~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.

6.5 KB
Raw Blame
"""
Standard citation handler - the original implementation.
"""

from datetime import date, datetime, time, timezone
from decimal import Decimal
from typing import Any, Dict, List, Union

from langchain_core.documents import Document

from ..security.log_sanitizer import sanitize_error_message, strip_control_chars
from .base_citation_handler import BaseCitationHandler


def _identity_text(value: Any) -> str:
    """Render scalar metadata; containers could hide secrets from the scrubber."""
    if value is None:
        return ""
    # datetime is a date, so date covers both; containers (dict, list, tuple,
    # set) stay on the placeholder path — that is the #7464 security fix.
    if isinstance(value, (str, int, float, Decimal, date, time)):
        return str(value)
    return "[non-text value omitted]"


class StandardCitationHandler(BaseCitationHandler):
    """Standard citation handler with detailed analysis."""

    def _format_sources(self, documents: List[Document]) -> str:
        """Keep source identities alongside the text used for synthesis."""
        sources = []
        for doc in documents:
            # Bound the title and URL and keep each on one line so they cannot
            # add identity lines; page_content passes through unchanged, as in
            # the base formatter. Preserve the original metadata.
            title = strip_control_chars(
                _identity_text(doc.metadata.get("title"))
            )
            title = sanitize_error_message(" ".join(title.split()))[:200]
            # Unlike the log-sink scrubber, this preserves query parameters
            # that identify sources (e.g. a video's ?v=), redacting credentials.
            url = strip_control_chars(
                _identity_text(doc.metadata.get("source"))
            )
            url = sanitize_error_message(" ".join(url.split()))
            # Scrub the whole URL first so userinfo containing "#" is caught,
            # then apply the query-parameter scrubber to fragment parameters.
            url, separator, fragment = url.partition("#")
            if separator:
                url += "#" + sanitize_error_message("?" + fragment)[1:]
            # Sanitize before truncating so a cut cannot hide a credential's
            # identifying suffix (e.g. the @ in URL userinfo).
            url = url[:2000]
            sources.append(
                f"[{doc.metadata['index']}] Title: {title}\n"
                f"URL: {url}\nContent: {doc.page_content}"
            )
        return "\n\n".join(sources)

    def analyze_initial(
        self, query: str, search_results: Union[str, List[Dict]]
    ) -> Dict[str, Any]:
        documents = self._create_documents(search_results)
        if not documents:
            return self._no_sources_response(query)
        formatted_sources = self._format_sources(documents)
        current_timestamp = datetime.now(timezone.utc).strftime(
            "%Y-%m-%d %H:%M"
        )

        output_prefix = self._get_output_instruction_prefix()

        prompt = f"""{output_prefix}Analyze the following information concerning the question and include citations using numbers in square brackets [1], [2], etc. When citing, use the source number provided at the start of each source.
The Title and URL lines identify each source for your reasoning; cite by number only — links are added automatically.

Question: {query}

Sources:
{formatted_sources}

Current time is {current_timestamp} UTC for verifying temporal references in sources.

Provide a detailed analysis with inline citations. Citations must only appear inline within your sentences (e.g. "According to the research [1], ..."). Do NOT create a bibliography, sources list, or list of citations at the end, as sources will be provided automatically. Never make up sources. Only write text relevant to the question.
"""

        response = self._invoke_with_streaming(prompt)
        return {"content": response, "documents": documents}

    def analyze_followup(
        self,
        question: str,
        search_results: Union[str, List[Dict]],
        previous_knowledge: str,
        nr_of_links: int,
    ) -> Dict[str, Any]:
        """Process follow-up analysis with citations."""
        documents = self._create_documents(
            search_results, nr_of_links=nr_of_links
        )
        # With previous knowledge present the LLM can still cite prior
        # sources legitimately; with neither, refuse to synthesize.
        if not documents and not (previous_knowledge or "").strip():
            return self._no_sources_response(question)
        formatted_sources = self._format_sources(documents)
        # Add fact-checking step
        fact_check_prompt = f"""Analyze these sources for factual consistency:
The Title and URL lines identify each source for your reasoning; cite by number only — links are added automatically.

1. Cross-reference major claims between sources
2. Identify and flag any contradictions
3. Verify basic facts (dates, company names, ownership)
4. Note when sources disagree

Previous Knowledge:
{previous_knowledge}

New Sources:
{formatted_sources}

        Return any inconsistencies or conflicts found."""
        if self.is_fact_checking_enabled():
            fact_check_response = self._invoke_text(fact_check_prompt)
        else:
            fact_check_response = ""

        current_timestamp = datetime.now(timezone.utc).strftime(
            "%Y-%m-%d %H:%M"
        )

        output_prefix = self._get_output_instruction_prefix()

        prompt = f"""{output_prefix}Using the previous knowledge and new sources, answer the question. Include citations using numbers in square brackets [1], [2], etc. When citing, use the source number provided at the start of each source. Reflect information from sources critically.
The Title and URL lines identify each source for your reasoning; cite by number only — links are added automatically.

Previous Knowledge:
{previous_knowledge}

Question: {question}

New Sources:
{formatted_sources}

Current time is {current_timestamp} UTC for verifying temporal references in sources.

Reflect information from sources critically based on: {fact_check_response}. Never invent sources.
Provide a detailed answer with inline citations. Citations must only appear inline within your sentences (e.g. "According to [1], ..."). Do NOT create a bibliography, sources list, or list of citations at the end, as sources will be provided automatically."""

        response = self._invoke_with_streaming(prompt)

        return {"content": response, "documents": documents}