Skip to content

Commit

Permalink
[refactor] Confirm parts are correct when citations have duplicate fi…
Browse files Browse the repository at this point in the history
…le names (#655)

Co-authored-by: Ian Seabock (Centific Technologies Inc) <[email protected]>
  • Loading branch information
iseabock and Ian Seabock (Centific Technologies Inc) committed Feb 27, 2024
1 parent 5679b31 commit 16c260c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions frontend/src/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type AskResponse = {
};

export type Citation = {
part_index?: number;
content: string;
id: string;
title: string | null;
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/Answer/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ export const Answer = ({
const createCitationFilepath = (citation: Citation, index: number, truncate: boolean = false) => {
let citationFilename = "";

if (citation.filepath && citation.chunk_id) {
if (citation.filepath) {
const part_i = citation.part_index ?? (citation.chunk_id ? parseInt(citation.chunk_id) + 1 : '');
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 ${part_i}`;
}
else {
citationFilename = `${citation.filepath} - Part ${parseInt(citation.chunk_id) + 1}`;
citationFilename = `${citation.filepath} - Part ${part_i}`;
}
}
else if (citation.filepath && citation.reindex_id) {
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/components/Answer/AnswerParser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ type ParsedAnswer = {
markdownFormatText: string;
};

const enumerateCitations = (citations: Citation[]) => {
const filepathMap = new Map();
for (const citation of citations) {
const { filepath } = citation;
let part_i = 1
if (filepathMap.has(filepath)) {
part_i = filepathMap.get(filepath) + 1;
}
filepathMap.set(filepath, part_i);
citation.part_index = part_i;
}
return citations;
}

export function parseAnswer(answer: AskResponse): ParsedAnswer {
let answerText = answer.answer;
const citationLinks = answerText.match(/\[(doc\d\d?\d?)]/g);
Expand All @@ -27,6 +41,7 @@ export function parseAnswer(answer: AskResponse): ParsedAnswer {
}
})

filteredCitations = enumerateCitations(filteredCitations);

return {
citations: filteredCitations,
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/x-icon" href="{{ favicon }}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }}</title>
<script type="module" crossorigin src="/assets/index-b152f819.js"></script>
<script type="module" crossorigin src="/assets/index-cb20ac21.js"></script>
<link rel="stylesheet" href="/assets/index-0774d4f6.css">
</head>
<body>
Expand Down

0 comments on commit 16c260c

Please sign in to comment.