fixed lint issues

This commit is contained in:
Ofer Mendelevitch
2023-11-21 09:14:12 -08:00
parent 1810b8718b
commit a4c3250a67
2 changed files with 30 additions and 33 deletions
@@ -7,39 +7,32 @@ import fetch from 'node-fetch'
// functionality based on https://github.com/vectara/vectara-answer
const reorderCitations = (unorderedSummary: string) => {
const allCitations = unorderedSummary.match(/\[\d+\]/g) || [];
const uniqueCitations = [...new Set(allCitations)];
const citationToReplacement: { [key: string]: string } = {};
const allCitations = unorderedSummary.match(/\[\d+\]/g) || []
const uniqueCitations = [...new Set(allCitations)]
const citationToReplacement: { [key: string]: string } = {}
uniqueCitations.forEach((citation, index) => {
citationToReplacement[citation] = `[${index + 1}]`;
});
return unorderedSummary.replace(
/\[\d+\]/g,
(match) => citationToReplacement[match]
);
};
const applyCitationOrder = (
searchResults: any[],
unorderedSummary: string
) => {
const orderedSearchResults: any[] = [];
const allCitations = unorderedSummary.match(/\[\d+\]/g) || [];
const addedIndices = new Set<number>();
citationToReplacement[citation] = `[${index + 1}]`
})
return unorderedSummary.replace(/\[\d+\]/g, (match) => citationToReplacement[match])
}
const applyCitationOrder = (searchResults: any[], unorderedSummary: string) => {
const orderedSearchResults: any[] = []
const allCitations = unorderedSummary.match(/\[\d+\]/g) || []
const addedIndices = new Set<number>()
for (let i = 0; i < allCitations.length; i++) {
const citation = allCitations[i];
const index = Number(citation.slice(1, citation.length - 1)) - 1;
if (addedIndices.has(index)) continue;
orderedSearchResults.push(searchResults[index]);
addedIndices.add(index);
const citation = allCitations[i]
const index = Number(citation.slice(1, citation.length - 1)) - 1
if (addedIndices.has(index)) continue
orderedSearchResults.push(searchResults[index])
addedIndices.add(index)
}
return orderedSearchResults;
};
return orderedSearchResults
}
class VectaraChain_Chains implements INode {
label: string
@@ -325,9 +318,9 @@ class VectaraChain_Chains implements INode {
rawSummarizedText = result.responseSet[0].summary[0]?.text
let summarizedText = reorderCitations(rawSummarizedText);
let summaryResponses = applyCitationOrder(responses, rawSummarizedText);
let summarizedText = reorderCitations(rawSummarizedText)
let summaryResponses = applyCitationOrder(responses, rawSummarizedText)
const sourceDocuments: Document[] = summaryResponses.map(
(response: { text: string; metadata: Record<string, unknown>; score: number }) =>
new Document({