diff --git a/.env.example b/.env.example index 77bb5630e..778ad5519 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ OPENAI_API_KEY= -# Update these with your Supabase details from your project settings > API +# Update these with your Supabase details from your project settings > API and dashboard settings PINECONE_API_KEY= PINECONE_ENVIRONMENT= - +PINECONE_INDEX_NAME= diff --git a/package.json b/package.json index 914013743..99d74b40f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "gpt4-langchain-pdf-chatbot", "version": "0.1.0", "private": true, + "engines": { + "node": ">=18" + }, "license": "MIT", "author": "Mayooear", "type": "module", diff --git a/pages/index.tsx b/pages/index.tsx index 90bb65f78..93cea1eec 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,4 +1,4 @@ -import { useRef, useState, useEffect, useMemo } from 'react'; +import { useRef, useState, useEffect, useMemo, useCallback } from 'react'; import Layout from '@/components/layout'; import styles from '@/styles/Home.module.css'; import { Message } from '@/types/chat'; @@ -18,6 +18,7 @@ export default function Home() { const [query, setQuery] = useState(''); const [loading, setLoading] = useState(false); const [sourceDocs, setSourceDocs] = useState([]); + const [error, setError] = useState(null); const [messageState, setMessageState] = useState<{ messages: Message[]; pending?: string; @@ -47,6 +48,8 @@ export default function Home() { async function handleSubmit(e: any) { e.preventDefault(); + setError(null); + if (!query) { alert('Please input a question'); return; @@ -118,18 +121,22 @@ export default function Home() { }); } catch (error) { setLoading(false); + setError('An error occurred while fetching the data. Please try again.'); console.log('error', error); } } //prevent empty submissions - const handleEnter = (e: any) => { - if (e.key === 'Enter' && query) { - handleSubmit(e); - } else if (e.key == 'Enter') { - e.preventDefault(); - } - }; + const handleEnter = useCallback( + (e: any) => { + if (e.key === 'Enter' && query) { + handleSubmit(e); + } else if (e.key == 'Enter') { + e.preventDefault(); + } + }, + [query], + ); const chatMessages = useMemo(() => { return [ @@ -146,6 +153,13 @@ export default function Home() { ]; }, [messages, pending, pendingSourceDocs]); + //scroll to bottom of chat + useEffect(() => { + if (messageListRef.current) { + messageListRef.current.scrollTop = messageListRef.current.scrollHeight; + } + }, [chatMessages]); + return ( <> @@ -209,7 +223,7 @@ export default function Home() { className="flex-col" > {message.sourceDocs.map((doc, index) => ( -
+

Source {index + 1}

@@ -297,9 +311,14 @@ export default function Home() {
+ {error && ( +
+

{error}

+
+ )} -