perplexity-clone / tools /citation_tool.py
Naveen-2007's picture
Perplexity AI Clone - Full Production Version with 8 Modes
b02630d
import re
from typing import List, Dict
class CitationTool:
"""Extracts [1], [2]… indices from answer and maps to sources."""
_pattern = re.compile(r"\[(\d+)\]")
def extract_indices(self, answer: str) -> List[int]:
return sorted({int(m.group(1)) for m in self._pattern.finditer(answer)})
def attach_sources(self, answer: str, sources: List[Dict]) -> List[Dict]:
used = self.extract_indices(answer)
mapped: List[Dict] = []
for idx in used:
if 1 <= idx <= len(sources):
mapped.append(sources[idx - 1])
return mapped