diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 8d248673d..4238ab8eb 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -7,23 +7,36 @@ type ParsedAnswer = { markdownFormatText: string; }; +let filteredCitations = [] as Citation[]; + +// Define a function to check if a citation with the same Chunk_Id already exists in filteredCitations +const isDuplicate = (citation: Citation,citationIndex:string) => { + return filteredCitations.some((c) => c.chunk_id === citation.chunk_id) && !filteredCitations.find((c) => c.id === citationIndex) ; +}; + export function parseAnswer(answer: AskResponse): ParsedAnswer { let answerText = answer.answer; const citationLinks = answerText.match(/\[(doc\d\d?\d?)]/g); const lengthDocN = "[doc".length; - let filteredCitations = [] as Citation[]; + filteredCitations = [] as Citation[]; let citationReindex = 0; citationLinks?.forEach(link => { // Replacing the links/citations with number let citationIndex = link.slice(lengthDocN, link.length - 1); let citation = cloneDeep(answer.citations[Number(citationIndex) - 1]) as Citation; - if (!filteredCitations.find((c) => c.id === citationIndex) && citation !== undefined) { + if (!isDuplicate(citation, citationIndex) && citation !== undefined) { answerText = answerText.replaceAll(link, ` ^${++citationReindex}^ `); citation.id = citationIndex; // original doc index to de-dupe citation.reindex_id = citationReindex.toString(); // reindex from 1 for display filteredCitations.push(citation); + }else{ + // Replacing duplicate citation with original index + let matchingCitation = filteredCitations.find((ct) => citation.chunk_id == ct.chunk_id); + if (matchingCitation) { + answerText= answerText.replaceAll(link, ` ^${matchingCitation.reindex_id}^ `) + } } })