From de8c86e152d0d0e84cd039178d3ea03b04421c14 Mon Sep 17 00:00:00 2001 From: Fraser Date: Fri, 31 Mar 2023 15:29:25 -0400 Subject: [PATCH] keep incrementing number number go up --- web/src/pages/index.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index 20511b3..80e3e03 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -24,7 +24,7 @@ type AssistantEntry = { role: "assistant"; content: string; display_content: string; - citations: Citation[]; + citations: Map; } type Entry = UserEntry | AssistantEntry; @@ -83,8 +83,8 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { return text.trim(); } i = parseInt(text) - 1; - if (i >= entry.citations.length) return `[${text}]`; - const citation = entry.citations[i]!; + if (!entry.citations.has(i)) return `[${text}]`; + const citation = entry.citations.get(i)!; return ( ); @@ -93,7 +93,7 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { }
    { // show citations - entry.citations.map((citation, i) => ( + Array.from(entry.citations.entries()).map(([i, citation]) => (
  • @@ -107,6 +107,7 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { const Home: NextPage = () => { const [ entries, setEntries ] = useState([]); + const [ runningIndex, setRunningIndex ] = useState(0); const search = async ( query: string, @@ -179,7 +180,7 @@ const Home: NextPage = () => { // figure out what citations are in the response, and map them appropriately const cite_map = new Map(); - let cite_count = 0; + let cite_count = runningIndex; // scan a regex for [x] over the response. If x isn't in the map, add it. const regex = /\[([a-z]+)\]/g; @@ -193,13 +194,15 @@ const Home: NextPage = () => { response_copy += response.slice(response_copy.length, match.index) + `[${cite_map.get(match[1]!)! + 1}]`; } + setRunningIndex(cite_count); + response = response_copy + response.slice(response_copy.length); // ----------------- create the ordered citation array ----------------- - const citations = new Array(); + const citations = new Map(); cite_map.forEach((value, key) => { - citations[value] = data.citations[key.charCodeAt(0) - 'a'.charCodeAt(0)]; + citations.set(value, data.citations[key.charCodeAt(0) - 'a'.charCodeAt(0)]); }); setEntries([...new_entries, {role: "assistant",