import React, { useState, useRef, useEffect } from 'react'; import { Box, Paper, TextField, Button, Typography, CircularProgress, Divider, List, ListItem, ListItemText, Collapse, IconButton } from '@mui/material'; import SendIcon from '@mui/icons-material/Send'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ExpandLessIcon from '@mui/icons-material/ExpandLess'; import axios from 'axios'; const Message = ({ content, isUser, context }) => { const [expanded, setExpanded] = useState(false); return ( {content} {context && context.length > 0 && ( Context used for this answer: {context.map((item, index) => ( 100 ? '...' : '')} secondary={`Relevance: ${(item[1] * 100).toFixed(2)}%`} /> ))} )} ); }; const ChatInterface = ({ fileName, isProcessing }) => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSend = async () => { if (!input.trim()) return; const userMessage = input.trim(); setInput(''); setMessages(prev => [...prev, { content: userMessage, isUser: true }]); setLoading(true); try { const response = await axios.post('http://localhost:8000/ask', { question: userMessage }); setMessages(prev => [ ...prev, { content: response.data.response, isUser: false, context: response.data.context } ]); } catch (error) { console.error('Error asking question:', error); setMessages(prev => [ ...prev, { content: 'Sorry, there was an error processing your question. Please try again.', isUser: false } ]); } finally { setLoading(false); } }; const handleKeyPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; return ( Chat about {fileName} {messages.length === 0 ? ( Ask a question about the document to get started ) : ( messages.map((message, index) => ( )) )}
setInput(e.target.value)} onKeyPress={handleKeyPress} disabled={loading || isProcessing} multiline maxRows={3} sx={{ mr: 1 }} /> {loading ? : } ); }; export default ChatInterface;