Skip to content

Commit

Permalink
Reuse citation numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Sep 30, 2023
1 parent 718b177 commit 0a5f995
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const Home: NextPage = () => {
const [entries, setEntries] = useState<Entry[]>([]);
const [current, setCurrent] = useState<CurrentSearch>();
const [sessionId, setSessionId] = useState()
const [citations, setCitations] = useState<Citation>([])

// [state, ready to save to localstorage]
const [mode, setMode] = useState<[Mode, boolean]>(["default", false]);
Expand All @@ -76,15 +77,32 @@ const Home: NextPage = () => {
}
};

const updateCitations = (current: CurrentSearch) => {
const citations = Array.from(current.citationsMap.values());
if (citations.some(c => !c.index)) {
let index = 1;
citations.forEach((c) => {c.index = index++});
setCurrent(current)
}

const updateCitations = (allCitations: Citation[], current: CurrentSearch) => {
const entryCitations = Array.from(current.citationsMap.values());
if (!entryCitations.some(c => !c.index)) {
// All of the entries citations have indexes, so there weren't any changes since the last check
return
}

// Get a mapping of all known citations, so as to reuse them if they appear again
const citationsMapping = Object.fromEntries(allCitations.map(c => ([c.title + c.url, c.index])));

entryCitations.forEach(
(c) => {
const hash = c.title + c.url;
if (!citationsMapping[hash]) {
c.index = allCitations.length + 1;
allCitations.push(c);
} else {
c.index = citationsMapping[hash];
}
}
)
setCitations(allCitations)
setCurrent(current)
}

const search = async (
query: string,
query_source: "search" | "followups",
Expand Down Expand Up @@ -127,7 +145,7 @@ const Home: NextPage = () => {
last_entry = <p>Loading: Waiting for LLM...</p>;
break;
case "streaming":
updateCitations(current)
updateCitations(citations, current)
last_entry = <AssistantEntry entry={current} />;
break;
case "followups":
Expand Down

0 comments on commit 0a5f995

Please sign in to comment.