Performance: Source citation in RAG responses
This concept affects the response generation speed and user experience by adding source references in retrieval-augmented generation (RAG) outputs.
const { answer, sources } = await ragModel.generateAnswerWithSources(query); // includes source citationsconst response = await ragModel.generateAnswer(query); // no source citation included| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No source citation | Minimal DOM nodes | 0 reflows | Low paint cost | [OK] Good |
| With source citation | More DOM nodes for citations | 1-2 reflows | Moderate paint cost | [!] OK |
True to receive source documents in RetrievalQA?return_source_documents.print(sources[0].metadata['source']) output if return_source_documents=True?
qa = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
result = qa({"query": "What is AI?"})
sources = result['source_documents']
print(sources[0].metadata['source'])metadata['source'] prints the origin of the first document, not the answer or full content.return_source_documents=True but get a KeyError when accessing result['source_documents']. What is the likely cause?run method returns only the answer string, not a dict with sources.qa({'query': '...'}) which returns a dict including source_documents.qa({'query': question}) returns a dict with answer and source documents.source_documents to show each source's metadata and a snippet of content for clear citation.Pass the test and this pattern moves unknown