rag.core.citations¶

Citation helpers for retrieved LlamaIndex nodes.

Attributes¶

Classes¶

FileMetadataKey

LlamaIndex / loader metadata keys that may hold a file path or name.

PageMetadataKey

LlamaIndex / pypdf metadata keys that may hold a page number.

Citation

A single retrieved source used to ground an answer.

Functions¶

citations_from_nodes(→ list[Citation])

Convert LlamaIndex NodeWithScore objects into citations.

format_citations(→ str)

Render citations as a markdown bullet list.

Module Contents¶

class rag.core.citations.FileMetadataKey[source]¶

Bases: enum.StrEnum

LlamaIndex / loader metadata keys that may hold a file path or name.

FILE_NAME = 'file_name'[source]¶
FILENAME = 'filename'[source]¶
FILE_PATH = 'file_path'[source]¶
SOURCE = 'source'[source]¶
class rag.core.citations.PageMetadataKey[source]¶

Bases: enum.StrEnum

LlamaIndex / pypdf metadata keys that may hold a page number.

PAGE_LABEL = 'page_label'[source]¶
PAGE = 'page'[source]¶
PAGE_NUMBER = 'page_number'[source]¶
rag.core.citations.UNKNOWN_FILE_NAME: Final = 'unknown'[source]¶
rag.core.citations.DEFAULT_SNIPPET_CHARS: Final = 280[source]¶
class rag.core.citations.Citation[source]¶

A single retrieved source used to ground an answer.

Variables:
  • file_name (str) – PDF file name (or path basename).

  • page (int or None) – 1-based page number when available.

  • score (float or None) – Retriever score when available.

  • snippet (str) – Short text excerpt from the chunk.

file_name: str[source]¶
page: int | None[source]¶
score: float | None[source]¶
snippet: str[source]¶
rag.core.citations.citations_from_nodes(*, nodes: collections.abc.Sequence[Any], max_snippet_chars: int = DEFAULT_SNIPPET_CHARS) list[Citation][source]¶

Convert LlamaIndex NodeWithScore objects into citations.

Parameters:
  • nodes (Sequence) – Retrieved nodes (typically NodeWithScore).

  • max_snippet_chars (int) – Max characters for each snippet.

Returns:

Ordered citations matching retrieval order.

Return type:

list of Citation

rag.core.citations.format_citations(*, citations: collections.abc.Sequence[Citation]) str[source]¶

Render citations as a markdown bullet list.

Parameters:

citations (Sequence of Citation) – Citations to format.

Returns:

Markdown string (empty when there are no citations).

Return type:

str