Skip to content

fix: Remove duplicated citation #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 3, 2024
9 changes: 7 additions & 2 deletions code/frontend/src/components/Answer/AnswerParser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ type ParsedAnswer = {
markdownFormatText: string;
};

let filteredCitations = [] as Citation[];

const isDuplicate = (citation: Citation) => {
return filteredCitations.some((c) => c.chunk_id === citation.chunk_id);
};

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[];
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)) {
if (!isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)) {
answerText = answerText.replaceAll(link, ` ^${++citationReindex}^ `);
citation.id = citationIndex; // original doc index to de-dupe
citation.reindex_id = citationReindex.toString(); // reindex from 1 for display
Expand Down
Loading