From 51e05e3948e7519b60ff806c1a5e84ba70313438 Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 20 May 2023 21:01:58 -0700 Subject: [PATCH] Just display docs if QA fails --- web/src/components/icons/icons.tsx | 8 ++ .../search/SearchResultsDisplay.tsx | 120 ++++++++++-------- web/src/components/search/SearchSection.tsx | 59 +++++---- web/src/components/search/types.tsx | 5 +- 4 files changed, 116 insertions(+), 76 deletions(-) diff --git a/web/src/components/icons/icons.tsx b/web/src/components/icons/icons.tsx index 226801a4b..8b5c79542 100644 --- a/web/src/components/icons/icons.tsx +++ b/web/src/components/icons/icons.tsx @@ -9,6 +9,7 @@ import { Notebook, Key, Trash, + Info, } from "@phosphor-icons/react"; interface IconProps { @@ -66,3 +67,10 @@ export const GoogleDriveIcon = ({ }: IconProps) => { return ; }; + +export const InfoIcon = ({ + size = "16", + className = defaultTailwindCSS, +}: IconProps) => { + return ; +}; diff --git a/web/src/components/search/SearchResultsDisplay.tsx b/web/src/components/search/SearchResultsDisplay.tsx index 65379824a..3da368d07 100644 --- a/web/src/components/search/SearchResultsDisplay.tsx +++ b/web/src/components/search/SearchResultsDisplay.tsx @@ -1,7 +1,8 @@ import React from "react"; -import { Quote, Document } from "./types"; +import { Quote, Document, SearchResponse } from "./types"; import { getSourceIcon } from "../source"; import { LoadingAnimation } from "../Loading"; +import { InfoIcon } from "../icons/icons"; const removeDuplicateDocs = (documents: Document[]) => { const seen = new Set(); @@ -19,33 +20,35 @@ const removeDuplicateDocs = (documents: Document[]) => { }; interface SearchResultsDisplayProps { - answer: string | null; - quotes: Record | null; - documents: Document[] | null; + searchResponse: SearchResponse | null; isFetching: boolean; } export const SearchResultsDisplay: React.FC = ({ - answer, - quotes, - documents, + searchResponse, isFetching, }) => { - if (!answer) { - if (isFetching) { - return ( -
-
- -
-
- ); - } + if (!searchResponse) { return null; } - if (answer === null) { - return
Unable to find an answer
; + if (isFetching) { + return ( +
+
+ +
+
+ ); + } + + const { answer, quotes, documents } = searchResponse; + if (answer === null && documents === null && quotes === null) { + return ( +
+ Something went wrong, please try again. +
+ ); } const dedupedQuotes: Quote[] = []; @@ -61,38 +64,53 @@ export const SearchResultsDisplay: React.FC = ({ return ( <> -
-
-

AI Answer

-
-

{answer}

+ {answer && ( +
+
+

AI Answer

+
+

{answer}

+ + {quotes !== null && ( + <> +

Sources

+ {isFetching && dedupedQuotes.length === 0 ? ( + + ) : ( + + )} + + )} +
+ )} + + {!answer && !isFetching && ( +
+ +
+ GPT hurt itself in its confusion :( +
+
+ )} - {quotes !== null && ( - <> -

Sources

- {isFetching && dedupedQuotes.length === 0 ? ( - - ) : ( - - )} - - )} -
{/* Only display docs once we're done fetching to avoid distracting from the AI answer*/} {!isFetching && documents && documents.length > 0 && (
@@ -103,7 +121,7 @@ export const SearchResultsDisplay: React.FC = ({ .slice(0, 7) .map((doc) => (
= () => { - const [answer, setAnswer] = useState(""); - const [quotes, setQuotes] = useState | null>(null); - const [documents, setDocuments] = useState(null); + const [searchResponse, setSearchResponse] = useState( + null + ); const [isFetching, setIsFetching] = useState(false); return ( @@ -139,26 +142,36 @@ export const SearchSection: React.FC<{}> = () => { { setIsFetching(true); - setAnswer(null); - setQuotes(null); - setDocuments(null); - searchRequestStreamed(query, setAnswer, setQuotes, setDocuments).then( - ({ quotes }) => { - setIsFetching(false); - // if no quotes were given, set to empty object so that the SearchResultsDisplay - // component knows that the search was successful but no quotes were found - if (!quotes) { - setQuotes({}); - } - } - ); + setSearchResponse({ + answer: null, + quotes: null, + documents: null, + }); + searchRequestStreamed( + query, + (answer) => + setSearchResponse((prevState) => ({ + ...(prevState || initialSearchResponse), + answer, + })), + (quotes) => + setSearchResponse((prevState) => ({ + ...(prevState || initialSearchResponse), + quotes, + })), + (documents) => + setSearchResponse((prevState) => ({ + ...(prevState || initialSearchResponse), + documents, + })) + ).then(() => { + setIsFetching(false); + }); }} />
diff --git a/web/src/components/search/types.tsx b/web/src/components/search/types.tsx index 0eaa34991..4ca89de6a 100644 --- a/web/src/components/search/types.tsx +++ b/web/src/components/search/types.tsx @@ -17,6 +17,7 @@ export interface Document { } export interface SearchResponse { - answer: string; - quotes: Record; + answer: string | null; + quotes: Record | null; + documents: Document[] | null; }