Skip to content

Commit fd81b69

Browse files
authored
Bugfix: Citation Parsing Issue (Version 2) (#2056)
* Citation issue fix * Citation issue fix version2 * Use startsWith * Update comments
1 parent 819d05e commit fd81b69

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

app/frontend/src/components/Answer/Answer.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export const Answer = ({
4343
showSpeechOutputBrowser
4444
}: Props) => {
4545
const followupQuestions = answer.context?.followup_questions;
46-
const messageContent = answer.message.content;
47-
const parsedAnswer = useMemo(() => parseAnswerToHtml(messageContent, isStreaming, onCitationClicked), [answer]);
46+
const parsedAnswer = useMemo(() => parseAnswerToHtml(answer, isStreaming, onCitationClicked), [answer]);
4847
const { t } = useTranslation();
4948
const sanitizedAnswerHtml = DOMPurify.sanitize(parsedAnswer.answerHtml);
5049

app/frontend/src/components/Answer/AnswerParser.tsx

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
import { renderToStaticMarkup } from "react-dom/server";
2-
import { getCitationFilePath } from "../../api";
2+
import { ChatAppResponse, getCitationFilePath } from "../../api";
33

44
type HtmlParsedAnswer = {
55
answerHtml: string;
66
citations: string[];
77
};
88

9-
export function parseAnswerToHtml(answer: string, isStreaming: boolean, onCitationClicked: (citationFilePath: string) => void): HtmlParsedAnswer {
9+
// Function to validate citation format and check if dataPoint starts with possible citation
10+
function isCitationValid(contextDataPoints: any, citationCandidate: string): boolean {
11+
const regex = /^[^\s]+\.[a-zA-Z0-9]+/;
12+
if (!regex.test(citationCandidate)) {
13+
return false;
14+
}
15+
16+
// Check if contextDataPoints is an object with a text property that is an array
17+
let dataPointsArray: string[];
18+
if (Array.isArray(contextDataPoints)) {
19+
dataPointsArray = contextDataPoints;
20+
} else if (contextDataPoints && Array.isArray(contextDataPoints.text)) {
21+
dataPointsArray = contextDataPoints.text;
22+
} else {
23+
return false;
24+
}
25+
26+
const isValidCitation = dataPointsArray.some(dataPoint => dataPoint.startsWith(citationCandidate));
27+
28+
if (!isValidCitation) {
29+
return false;
30+
}
31+
32+
return true;
33+
}
34+
35+
export function parseAnswerToHtml(answer: ChatAppResponse, isStreaming: boolean, onCitationClicked: (citationFilePath: string) => void): HtmlParsedAnswer {
36+
const contextDataPoints = answer.context.data_points;
1037
const citations: string[] = [];
1138

12-
// trim any whitespace from the end of the answer after removing follow-up questions
13-
let parsedAnswer = answer.trim();
39+
// Trim any whitespace from the end of the answer after removing follow-up questions
40+
let parsedAnswer = answer.message.content.trim();
1441

1542
// Omit a citation that is still being typed during streaming
1643
if (isStreaming) {
@@ -34,6 +61,11 @@ export function parseAnswerToHtml(answer: string, isStreaming: boolean, onCitati
3461
return part;
3562
} else {
3663
let citationIndex: number;
64+
65+
if (!isCitationValid(contextDataPoints, part)) {
66+
return `[${part}]`;
67+
}
68+
3769
if (citations.indexOf(part) !== -1) {
3870
citationIndex = citations.indexOf(part) + 1;
3971
} else {

0 commit comments

Comments
 (0)