From 089cb5efd83a0458a4e1865c68698a0ab33c4530 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Fri, 24 May 2024 22:08:35 +0530 Subject: [PATCH 01/10] PSL-BUG-1745 Fixed --- .../frontend/src/components/Answer/Answer.tsx | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/code/frontend/src/components/Answer/Answer.tsx b/code/frontend/src/components/Answer/Answer.tsx index 26222761a..a00e45af5 100644 --- a/code/frontend/src/components/Answer/Answer.tsx +++ b/code/frontend/src/components/Answer/Answer.tsx @@ -39,16 +39,28 @@ export const Answer = ({ setChevronIsExpanded(isRefAccordionOpen); }, [isRefAccordionOpen]); + const usedChunkIds = new Set(); + + const getUniqueChunkID = (chunk_id: number): number =>{ + let uniqueID=chunk_id; + while(usedChunkIds.has(uniqueID)){ + break + } + usedChunkIds.add(uniqueID); + return uniqueID; + } + const createCitationFilepath = (citation: Citation, index: number, truncate: boolean = false) => { let citationFilename = ""; if (citation.filepath && citation.chunk_id != null) { + const uniqueChunkID = getUniqueChunkID(parseInt(citation.chunk_id)); if (truncate && citation.filepath.length > filePathTruncationLimit) { const citationLength = citation.filepath.length; - citationFilename = `${citation.filepath.substring(0, 20)}...${citation.filepath.substring(citationLength -20)} - Part ${parseInt(citation.chunk_id) + 1}`; + citationFilename = `${citation.filepath.substring(0, 20)}...${citation.filepath.substring(citationLength -20)} - Part ${uniqueChunkID + 1}`; } else { - citationFilename = `${citation.filepath} - Part ${parseInt(citation.chunk_id) + 1}`; + citationFilename = `${citation.filepath} - Part ${uniqueChunkID + 1}`; } } else { @@ -97,13 +109,13 @@ export const Answer = ({ onClick={handleChevronClick} iconName={chevronIsExpanded ? 'ChevronDown' : 'ChevronRight'} /> - + )} - + - {chevronIsExpanded && + {chevronIsExpanded &&
{parsedAnswer.citations.map((citation, idx) => { return ( From 3bcbc2f3e711d98b09fcec73ec61a35f8cd2d74a Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 27 May 2024 14:21:09 +0530 Subject: [PATCH 02/10] removed duplicate citation --- code/frontend/src/components/Answer/Answer.tsx | 12 ------------ code/frontend/src/components/Answer/AnswerParser.tsx | 9 +++++++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/code/frontend/src/components/Answer/Answer.tsx b/code/frontend/src/components/Answer/Answer.tsx index ab7140690..8cc50873d 100644 --- a/code/frontend/src/components/Answer/Answer.tsx +++ b/code/frontend/src/components/Answer/Answer.tsx @@ -39,22 +39,10 @@ export const Answer = ({ setChevronIsExpanded(isRefAccordionOpen); }, [isRefAccordionOpen]); - const usedChunkIds = new Set(); - - const getUniqueChunkID = (chunk_id: number): number =>{ - let uniqueID=chunk_id; - while(usedChunkIds.has(uniqueID)){ - break - } - usedChunkIds.add(uniqueID); - return uniqueID; - } - const createCitationFilepath = (citation: Citation, index: number, truncate: boolean = false) => { let citationFilename = ""; if (citation.filepath && citation.chunk_id != null) { - const uniqueChunkID = getUniqueChunkID(parseInt(citation.chunk_id)); if (truncate && citation.filepath.length > filePathTruncationLimit) { const citationLength = citation.filepath.length; citationFilename = `${citation.filepath.substring(0, 20)}...${citation.filepath.substring(citationLength -20)} - Part ${citation.chunk_id}`; diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 0717ec3e7..f7d3943c9 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -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 From 2e674ec1a1180e2b331aa0a088c4a3c80a4366b1 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 27 May 2024 14:28:28 +0530 Subject: [PATCH 03/10] added comment --- code/frontend/src/components/Answer/AnswerParser.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index f7d3943c9..2ae8c6151 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -9,6 +9,7 @@ type ParsedAnswer = { 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) => { return filteredCitations.some((c) => c.chunk_id === citation.chunk_id); }; From 4245ee03e19d01961d5f83448c4a521c30645a0e Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Thu, 30 May 2024 17:48:55 +0530 Subject: [PATCH 04/10] fixed comment --- code/frontend/src/components/Answer/AnswerParser.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 2ae8c6151..49695ffe3 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -20,6 +20,7 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { const lengthDocN = "[doc".length; + filteredCitations = [] as Citation[]; let citationReindex = 0; citationLinks?.forEach(link => { // Replacing the links/citations with number @@ -30,6 +31,9 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { citation.id = citationIndex; // original doc index to de-dupe citation.reindex_id = citationReindex.toString(); // reindex from 1 for display filteredCitations.push(citation); + }else if (!isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)){ + ++citationReindex + answerText= answerText.replaceAll(link, '') } }) From 17d749d73f530ecb3883e1bebd006f6aff227719 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Thu, 30 May 2024 18:59:58 +0530 Subject: [PATCH 05/10] modify code --- code/frontend/src/components/Answer/AnswerParser.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 49695ffe3..778865a38 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -32,7 +32,6 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { citation.reindex_id = citationReindex.toString(); // reindex from 1 for display filteredCitations.push(citation); }else if (!isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)){ - ++citationReindex answerText= answerText.replaceAll(link, '') } }) From e967a178b7e245a268a93ccdfcd6aa3299f18b19 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Thu, 30 May 2024 20:39:35 +0530 Subject: [PATCH 06/10] modify code --- code/frontend/src/components/Answer/AnswerParser.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 778865a38..31ec587d8 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -31,7 +31,7 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { citation.id = citationIndex; // original doc index to de-dupe citation.reindex_id = citationReindex.toString(); // reindex from 1 for display filteredCitations.push(citation); - }else if (!isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)){ + }else if (isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)){ answerText= answerText.replaceAll(link, '') } }) From 645d7f4256e6b0ab3b8fd0a70eacbfdea3eb8527 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 3 Jun 2024 15:43:45 +0530 Subject: [PATCH 07/10] replaced duplicate citation with original index --- .../src/components/Answer/AnswerParser.tsx | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 31ec587d8..9d2313aba 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -10,8 +10,8 @@ type ParsedAnswer = { 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) => { - return filteredCitations.some((c) => c.chunk_id === citation.chunk_id); +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 { @@ -22,17 +22,25 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { filteredCitations = [] as Citation[]; let citationReindex = 0; + let i =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 (!isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)) { + let citation = cloneDeep(answer.citations[i]) as Citation; + if (!isDuplicate(citation, 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 filteredCitations.push(citation); - }else if (isDuplicate(citation) && !filteredCitations.find((c) => c.id === citationIndex)){ - answerText= answerText.replaceAll(link, '') + i++ + }else{ + // Replacing duplicate citation with original index + filteredCitations.find((ct)=>{ + if (citation.chunk_id == ct.chunk_id){ + answerText= answerText.replaceAll(link, ` ^${ct.reindex_id}^`) + } + }) + i++ } }) From feb4b34762c65f00cd5162ef89d02a927e00a8a0 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 3 Jun 2024 18:54:38 +0530 Subject: [PATCH 08/10] modify code --- code/frontend/src/components/Answer/AnswerParser.tsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 9d2313aba..d01aa2a9a 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -22,17 +22,15 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { filteredCitations = [] as Citation[]; let citationReindex = 0; - let i =0 citationLinks?.forEach(link => { // Replacing the links/citations with number let citationIndex = link.slice(lengthDocN, link.length - 1); - let citation = cloneDeep(answer.citations[i]) as Citation; + let citation = cloneDeep(answer.citations[Number(citationIndex) - 1]) as Citation; if (!isDuplicate(citation, 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 filteredCitations.push(citation); - i++ }else{ // Replacing duplicate citation with original index filteredCitations.find((ct)=>{ @@ -40,7 +38,6 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { answerText= answerText.replaceAll(link, ` ^${ct.reindex_id}^`) } }) - i++ } }) From 595260c2d035bc3746274b0524f9224a97acafd6 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 3 Jun 2024 19:09:41 +0530 Subject: [PATCH 09/10] modify code --- code/frontend/src/components/Answer/AnswerParser.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index d01aa2a9a..6a679a613 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -33,11 +33,10 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { filteredCitations.push(citation); }else{ // Replacing duplicate citation with original index - filteredCitations.find((ct)=>{ - if (citation.chunk_id == ct.chunk_id){ - answerText= answerText.replaceAll(link, ` ^${ct.reindex_id}^`) - } - }) + let matchingCitation = filteredCitations.find((ct) => citation.chunk_id == ct.chunk_id); + if (matchingCitation) { + answerText= answerText.replaceAll(link, ` ^${matchingCitation.reindex_id}^`) + } } }) From 0499b4f3334362bff807e6251e7bcdd6ec31f472 Mon Sep 17 00:00:00 2001 From: Prashant-Microsoft Date: Mon, 3 Jun 2024 20:26:05 +0530 Subject: [PATCH 10/10] modify code --- code/frontend/src/components/Answer/AnswerParser.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/frontend/src/components/Answer/AnswerParser.tsx b/code/frontend/src/components/Answer/AnswerParser.tsx index 4fa53d9a6..4238ab8eb 100644 --- a/code/frontend/src/components/Answer/AnswerParser.tsx +++ b/code/frontend/src/components/Answer/AnswerParser.tsx @@ -35,7 +35,7 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer { // 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}^`) + answerText= answerText.replaceAll(link, ` ^${matchingCitation.reindex_id}^ `) } } })