This is the frontend for the RAG (Retrieval-Augmented Generation) News Chatbot. It provides a user-friendly chat interface for users to interact with the backend API, ask questions about news articles, view responses, and manage their chat sessions.
- Overview
- Features
- Tech Stack & Justifications
- Project Structure
- Core Functionality & Flow
- Setup and Running Instructions (Local Development)
- Interacting with the Backend API
- Deployment (Vercel)
- Potential Improvements
The frontend application is built using React and styled with Tailwind CSS. It allows users to:
- Send messages to the chatbot backend.
- View a stream of conversation messages (user and bot).
- See contextually relevant answers generated by the RAG pipeline and Gemini LLM.
- Manage chat sessions, including starting new chats, viewing recent chat history (client-side), and clearing sessions.
- Experience a modern, responsive, and dark-themed user interface.
- Real-time Chat Interface: Displays user queries and bot responses in a familiar chat bubble format.
- Message Input: Allows users to type and send messages (supports Enter to send).
- Markdown Rendering: Bot responses containing Markdown are rendered correctly for better readability (e.g., bold text, lists).
- Session Management (Client-Side Tracking):
- Automatically generates and uses a
session_id
for conversations. - Persists the current
session_id
and a list of recent chat sessions inlocalStorage
. - "New Chat" functionality to start a fresh conversation (clears current view and session on backend).
- Sidebar to display and switch between recent chat sessions (loads history from backend).
- Ability to delete sessions from the sidebar (clears from
localStorage
and backend).
- Automatically generates and uses a
- Loading & Error States: Provides visual feedback when the bot is processing a request or if an error occurs.
- Responsive Design: Adapts to various screen sizes (desktop, mobile).
- Theme Toggle: Includes a basic dark/light theme toggle.
- Core Framework: React (with Hooks)
- Justification: A popular and powerful JavaScript library for building dynamic user interfaces with a component-based architecture. Hooks enable functional components with state and lifecycle features.
- Styling: Tailwind CSS
- Justification: A utility-first CSS framework that allows for rapid UI development and easy customization, resulting in a modern and maintainable design.
- Icons: Lucide React
- Justification: A comprehensive and well-designed SVG icon library, easy to integrate with React and Tailwind CSS.
- Markdown Rendering:
react-markdown
- Justification: To properly display formatted responses from the LLM which may include Markdown syntax.
- API Communication:
fetch
API (native browser API)- Justification: Built-in to modern browsers, sufficient for making REST API calls to the backend.
- Build Tool: Vite (or Create React App if used)
- Justification (Vite): Fast development server, optimized builds, modern JavaScript tooling.
- Deployment Platform: Vercel
- Justification: Excellent platform for deploying static sites and React applications, offering easy Git integration, CI/CD, and global CDN.
A typical React project structure (e.g., created with Vite):
my-news-chatbot-frontend/├── public/ # Static assets│ └── ...├── src/│ ├── assets/ # Images, custom fonts, etc. (if any)│ ├── components/ # Reusable UI components (e.g., MessageBubble.jsx - if separated)│ ├── App.jsx # Main application component containing UI logic│ ├── index.css # Global styles and Tailwind directives│ └── main.jsx # Entry point of the React application (or index.js for CRA)├── .env.local # Local environment variables (e.g., API base URL for dev) - NOT COMMITTED├── .gitignore├── index.html # Main HTML file├── package.json # Project dependencies and scripts├── tailwind.config.js # Tailwind CSS configuration├── postcss.config.js # PostCSS configuration└── vite.config.js # Vite configuration (if using Vite)(Note: The provided App.jsx
contains MessageBubble
as an inner component for simplicity in the example, but it could be separated into src/components/
)
- Initialization:
- On load,
App.jsx
attempts to retrieve the last activesession_id
and a list of recent session metadata fromlocalStorage
. - If a session exists, its history is fetched from the backend (
/history/:sessionId
). - If no session information is found, a new
session_id
is generated using a helper function (uuidv4
), a welcome message is displayed, and this new session is added to thelocalStorage
list of sessions.
- On load,
- Sending a Message:
- The user types a message in the input field.
- On send, the
handleSendMessage
function is triggered. - The user's message is added to the local
messages
state for immediate display. - An API call (
fetch
) is made to the backend's/chat
endpoint, including theuser_message
and currentsession_id
. - A loading state is activated.
- Receiving a Response:
- The backend processes the request (RAG pipeline, Gemini call, Redis update).
- The backend returns a JSON response containing
bot_response
,session_id
, andretrieved_context_count
. - The bot's response is added to the
messages
state. - The
session_id
state is updated if the backend assigned a new one (though typically it uses the one sent). - The session metadata (name derived from the first user message, timestamp) in
localStorage
is updated viaupdateStoredSessions
. - The loading state is deactivated.
- Displaying Messages:
- The
messages
array (React state) is mapped to renderMessageBubble
components. - Bot responses containing Markdown are rendered as formatted HTML using
react-markdown
. - The chat window auto-scrolls to the latest message using
messagesEndRef.current?.scrollIntoView()
.
- The
- Session Management UI:
- New Chat: The "New Chat" button calls
handleResetSession
. This function effectively starts a new session by callingstartNewSession()
, which generates a new client-sidesession_id
, updateslocalStorage
, clears the current message view, and also makes an API call to the backend's/clear_session/:oldSessionId
endpoint to clear the history for the previously active session from Redis. - Recent Chats Sidebar: Displays sessions stored in
localStorage
(managed bychatHistorySessions
state, sorted by recency). Clicking a session sets it as thecurrentSessionId
and callsfetchChatHistory(selectedSessionId)
to load its messages from the backend's/history/:sessionId
endpoint. - Delete Chat: Clicking the delete icon next to a session in the sidebar calls
handleDeleteSessionFromSidebar
. This removes the session metadata fromlocalStorage
andchatHistorySessions
state, and also makes an API call to/clear_session/:sessionIdToDelete
on the backend to remove its history from Redis. If the deleted session was the active one, a new chat session is started.
- New Chat: The "New Chat" button calls
- Node.js (e.g., v18.x or v20.x) and npm/yarn.
- A running instance of the backend API (see backend
README.md
). The default frontend configuration expects the backend to be running onhttp://localhost:8000
.
For local development, the API_BASE_URL
is hardcoded in App.jsx
as http://localhost:8000
.
For deployment (e.g., to Vercel), you would typically configure an environment variable:
- If using Vite, create a
.env
file in the project root:VITE_API_BASE_URL=[http://your-deployed-backend-url.com](http://your-deployed-backend-url.com)
And access it in code as import.meta.env.VITE_API_BASE_URL.If using Create React App, it would be REACT_APP_API_BASE_URL in a .env file.Installation & RunningClone the repository (if you haven't already).Navigate to the frontend project directory:cd your-frontend-project-name Install dependencies:npm install
Run the development server:npm run dev # For Vite