import { useRef, useState, useEffect } from 'react'; import Layout from '@/components/layout'; import styles from '@/styles/Home.module.css'; import { Message } from '@/types/chat'; import Image from 'next/image'; import ReactMarkdown from 'react-markdown'; import LoadingDots from '@/components/ui/LoadingDots'; import { Document } from 'langchain/document'; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from '@/components/ui/accordion'; // require('dotenv') .config() // import {run} from '@/scripts/ingest-data'; export default function Home() { // run(); //console.log(process.env.OPENAI_API_KEY) const [query, setQuery] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [messageState, setMessageState] = useState<{ messages: Message[]; pending?: string; history: [string, string][]; pendingSourceDocs?: Document[]; }>({ messages: [ { message: 'Hi, what would you like to learn about this legal case?', type: 'apiMessage', }, ], history: [], }); const { messages, history } = messageState; const messageListRef = useRef(null); const textAreaRef = useRef(null); useEffect(() => { textAreaRef.current?.focus(); }, []); //handle form submission async function handleSubmit(e: any) { e.preventDefault(); setError(null); if (!query) { alert('Please input a question'); return; } const question = query.trim(); setMessageState((state) => ({ ...state, messages: [ ...state.messages, { type: 'userMessage', message: question, }, ], })); setLoading(true); setQuery(''); try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ question, history, }), }); const data = await response.json(); console.log('data', data); if (data.error) { setError(data.error); } else { setMessageState((state) => ({ ...state, messages: [ ...state.messages, { type: 'apiMessage', message: data.text, sourceDocs: data.sourceDocuments, }, ], history: [...state.history, [question, data.text]], })); } console.log('messageState', messageState); setLoading(false); //scroll to bottom messageListRef.current?.scrollTo(0, messageListRef.current.scrollHeight); } 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(); } }; return ( <>

Chat With Your Legal Docs

{messages.map((message, index) => { let icon; let className; if (message.type === 'apiMessage') { icon = ( AI ); className = styles.apimessage; } else { icon = ( Me ); // The latest message sent by the user will be animated while waiting for a response className = loading && index === messages.length - 1 ? styles.usermessagewaiting : styles.usermessage; } return ( <>
{icon}
{message.message}
{message.sourceDocs && (
{message.sourceDocs.map((doc, index) => (

Source {index + 1}

{doc.pageContent}

Source: {doc.metadata.source}

))}
)} ); })}