Citations
A retrieval-grounded reply without citations is a hallucination waiting to happen. The user cannot tell which parts came from your corpus and which parts the model invented. Add document ids to the response so every factual claim can be traced back to a source.
from dataclasses import dataclass
from typing import List
@dataclass
class RetrievedDoc:
doc_id: str
text: str
score: float
class RetrievalLayer:
def search_with_ids(self, query: str, k: int = 3) -> List[RetrievedDoc]:
if not self.available or self._collection is None:
return []
res = self._collection.query(
query_texts=[query],
n_results=k,
include=["documents", "distances"],
)
ids = res.get("ids", [[]])[0]
docs = res.get("documents", [[]])[0]
distances = res.get("distances", [[]])[0]
return [
RetrievedDoc(doc_id=i, text=t, score=1.0 - d)
for i, t, d in zip(ids, docs, distances)
]Every retrieved passage carries its doc_id and a similarity score. The synthesis step uses the text for grounding and includes the ids in the final response so the user can click through.
AI prompt: Try it: citation-style synthesis prompt
Loading practice…
Quiz: Quiz
Loading practice…