import { Fragment, useEffect, useRef } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import Hero from '../Hero'; import { AppDispatch } from '../store'; import ConversationBubble from './ConversationBubble'; import { addQuery, fetchAnswer, selectQueries, selectStatus, updateQuery, } from './conversationSlice'; import Send from './../assets/send.svg'; import Spinner from './../assets/spinner.svg'; import { FEEDBACK, Query } from './conversationModels'; import { sendFeedback } from './conversationApi'; export default function Conversation() { const queries = useSelector(selectQueries); const status = useSelector(selectStatus); const dispatch = useDispatch(); const endMessageRef = useRef(null); const inputRef = useRef(null); useEffect( () => endMessageRef?.current?.scrollIntoView({ behavior: 'smooth' }), [queries.length, queries[queries.length - 1]], ); const handleQuestion = (question: string) => { dispatch(addQuery({ prompt: question })); dispatch(fetchAnswer({ question })); }; const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => { const prevFeedback = query.feedback; dispatch(updateQuery({ index, query: { feedback } })); sendFeedback(query.prompt, query.response!, feedback).catch(() => dispatch(updateQuery({ index, query: { feedback: prevFeedback } })), ); }; const prepResponseView = (query: Query, index: number) => { let responseView; if (query.error) { responseView = ( ); } else if (query.response) { responseView = ( handleFeedback(query, feedback, index) } > ); } return responseView; }; const handlePaste = (e: React.ClipboardEvent) => { e.preventDefault(); const text = e.clipboardData.getData('text/plain'); document.execCommand('insertText', false, text); }; return (
{queries.length > 0 && (
{queries.map((query, index) => { return ( {prepResponseView(query, index)} ); })}
)} {queries.length === 0 && }
{ if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); if (inputRef.current?.textContent && status !== 'loading') { handleQuestion(inputRef.current.textContent); inputRef.current.textContent = ''; } } }} >
{status === 'loading' ? ( ) : (
{ if (inputRef.current?.textContent) { handleQuestion(inputRef.current.textContent); inputRef.current.textContent = ''; } }} src={Send} >
)}

This is a chatbot that uses the GPT-3, Faiss and LangChain to answer questions.

); }