Implementation:ALwrity ALwrity CitationManager
| Knowledge Sources | |
|---|---|
| Domains | LinkedIn, Citation, Content_Quality |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Concrete tool for adding, validating, and formatting inline citations in grounded content, provided by the ALwrity citation subsystem.
Description
The CitationManager class handles citation lifecycle for grounded content generation. It recognises multiple citation patterns ([Source 1], [1], (Source 1), (1), Source 1, Ref. 1, Reference 1) via compiled regex. add_citations appends a formatted source list to content in the chosen style (brackets, parentheses, inline, numbered). validate_citations scans content for citations, computes coverage (fraction of sources cited), accuracy (fraction of valid citation numbers), and completeness (fraction of expected sources referenced), and returns a weighted validation score. generate_source_list formats the source list with titles, URLs, and excerpts.
Usage
Import this class when:
- Grounded content needs inline citations and a source list appended.
- Citation coverage and quality must be validated before publishing.
- Multiple citation styles (brackets, numbered, inline) must be supported.
Code Reference
Source Location
- Repository: ALwrity
- File: backend/services/citation/citation_manager.py (L28-200+)
Signature
class CitationManager:
"""Service for managing citations in grounded content."""
def __init__(self):
self.citation_patterns = [
r'\[Source (\d+)\]', r'\[(\d+)\]', r'\(Source (\d+)\)',
r'\((\d+)\)', r'Source (\d+)', r'Ref\. (\d+)', r'Reference (\d+)',
]
self.compiled_patterns = [re.compile(p, re.IGNORECASE) for p in self.citation_patterns]
def add_citations(
self,
content: str,
sources: List[Any],
citation_style: str = "brackets",
) -> str:
"""Add citations to content based on source information."""
def validate_citations(
self,
content: str,
sources: List[Any],
) -> Dict[str, Any]:
"""Validate citations in content for completeness and accuracy."""
def generate_source_list(
self,
sources: List[Any],
citation_style: str = "brackets",
) -> str:
"""Generate a formatted list of sources."""
Import
from services.citation import CitationManager
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| content | str | Yes | The content with citations to validate or augment |
| sources | List[Any] | Yes | Research sources (Dict or ResearchSource objects) |
| citation_style | str | No | brackets, parentheses, inline, numbered (default: brackets) |
Outputs
| Name | Type | Description |
|---|---|---|
| add_citations | str | Content with appended source list |
| validate_citations | Dict[str, Any] | total_sources, citations_found, citation_coverage, citation_quality, missing_citations, invalid_citations, validation_score |
| generate_source_list | str | Formatted source list string |
Usage Examples
Validating citation coverage
from services.citation import CitationManager
manager = CitationManager()
result = manager.validate_citations(
content="According to [Source 1], QLoRA reduces memory. [Source 3] confirms this.",
sources=[src1, src2, src3],
)
print(f"Coverage: {result['citation_coverage']}") # 0.667 (2 of 3 sources cited)
print(f"Validation score: {result['validation_score']}")
print(f"Missing citations: {result['missing_citations']}") # [2]
Adding a source list
content_with_sources = manager.add_citations(
content=post_content,
sources=research_sources,
citation_style="brackets",
)
# content_with_sources now has a formatted "**Sources:**" list appended