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, } from './conversationSlice'; import Send from './../assets/send.svg'; import Spinner from './../assets/spinner.svg'; import { Query } from './conversationModels'; 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], ); const handleQuestion = (question: string) => { dispatch(addQuery({ prompt: question })); dispatch(fetchAnswer({ question })); }; const prepResponseView = (query: Query, index: number) => { let responseView; if (query.error) { responseView = ( ); } else if (query.response) { responseView = ( ); } return responseView; }; 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.

); }