Skip to content

Perplexity-style AI chat with RAG, streaming responses, PDF citations, and generative UI. Built with Next.js 14 + FastAPI + LangChain.

Notifications You must be signed in to change notification settings

ysocrius/ai-citation-chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Search Chat with PDF Citation Viewer & Generative UI

A Perplexity-style AI chat application featuring real-time streaming responses, RAG-powered document retrieval, interactive PDF viewer with citations, and generative UI components.

🌟 Features

Core Functionality

  • πŸ€– Streaming AI Responses - Real-time token-by-token streaming with GPT-4o-mini
  • πŸ“„ RAG (Retrieval-Augmented Generation) - Context-aware responses from PDF documents
  • πŸ”— Interactive Citations - Clickable [1, p.2] citations that open PDF viewer
  • πŸ“š PDF Viewer - Smooth slide-in viewer with page navigation and search
  • 🎨 Generative UI - Dynamic components (FinancialCard) streamed with responses
  • πŸ” Tool Call Indicators - Visual feedback (πŸ” Searching... πŸ“„ Reading...)

Bonus Features

  • βœ… Dark Mode - Manual theme toggle
  • βœ… Responsive Design - Mobile-first, adaptive layouts
  • βœ… PDF Text Search - Highlight search terms in documents
  • βœ… Smooth Animations - Framer Motion for all transitions

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 β”‚         β”‚                 β”‚         β”‚                 β”‚
β”‚  Next.js 14     │◄─────────   FastAPI       │◄─────────   OpenAI        β”‚
β”‚  Frontend       β”‚  SSE    β”‚   Backend       β”‚  API    β”‚   GPT-4o-mini   β”‚
β”‚                 β”‚         β”‚                 β”‚         β”‚                 β”‚
β”‚  - Chat UI      β”‚         β”‚  - LangChain    β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚  - PDF Viewer   β”‚         β”‚  - PDF Extract  β”‚
β”‚  - State (Zustand)β”‚       β”‚  - Queue Mgmt   β”‚
β”‚                 β”‚         β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚   PyPDF2        β”‚
                            β”‚   PDF Files     β”‚
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Streaming Protocol (SSE)

// Backend sends:
data: {"type": "tool", "content": "πŸ” Searching documents..."}
data: {"type": "token", "content": "RAG combines"}
data: {"type": "ui", "component": "FinancialCard", "data": {...}}
data: [DONE]

// Frontend handles:
- Tool calls β†’ Display indicators
- Tokens β†’ Append to message
- UI β†’ Render components

πŸš€ Quick Start

Prerequisites

  • Node.js 18+
  • Python 3.11+
  • OpenAI API Key

Backend Setup

cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Create .env file
echo "OPENAI_API_KEY=your-api-key-here" > .env

# Run server
python -m uvicorn main:app --reload
# Server runs on http://localhost:8000

Frontend Setup

cd frontend

# Install dependencies
npm install

# Run development server
npm run dev
# App runs on http://localhost:3000

Access the App

Open http://localhost:3000 in your browser.

πŸ“¦ Project Structure

calQuity_1/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ agent.py              # LangGraph agent & streaming logic
β”‚   β”œβ”€β”€ main.py               # FastAPI app & SSE endpoint
β”‚   β”œβ”€β”€ queue_manager.py      # Asyncio semaphore queue
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── pdf.py           # PDF extraction & serving
β”‚   β”œβ”€β”€ assets/
β”‚   β”‚   └── sample.pdf       # Demo 3-page PDF
β”‚   └── requirements.txt
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ page.tsx         # Main chat page
β”‚   β”‚   β”œβ”€β”€ layout.tsx       # Root layout
β”‚   β”‚   └── globals.css      # Tailwind + theme
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatLayout.tsx    # Main chat container
β”‚   β”‚   β”‚   β”œβ”€β”€ MessageList.tsx   # Message rendering
β”‚   β”‚   β”‚   └── ChatInput.tsx     # Input field
β”‚   β”‚   β”œβ”€β”€ pdf/
β”‚   β”‚   β”‚   └── PDFViewer.tsx     # PDF viewer modal
β”‚   β”‚   └── gen-ui/
β”‚   β”‚       └── FinancialCard.tsx # Generative UI component
β”‚   β”œβ”€β”€ store/
β”‚   β”‚   └── chatStore.ts     # Zustand state management
β”‚   β”œβ”€β”€ next.config.ts       # API proxy config
β”‚   └── package.json
└── README.md

πŸ› οΈ Tech Stack

Frontend

Library Version Purpose
Next.js 14.2.24 React framework with App Router
TypeScript 5.x Type safety
Tailwind CSS 4.0.0 Utility-first styling
Framer Motion 11.x Smooth animations
Zustand 5.x Lightweight state management
Lucide React - Icon library
uuid - Message ID generation

Backend

Library Version Purpose
FastAPI 0.115.6 High-performance API framework
LangChain Latest LLM orchestration
LangChain-OpenAI Latest OpenAI integration
OpenAI Latest GPT-4o-mini API client
PyPDF2 Latest PDF text extraction
ReportLab Latest PDF generation
Python-dotenv Latest Environment variables

🎯 Design Decisions

1. Queue System: Asyncio Semaphore

Choice: In-memory asyncio.Semaphore (3 concurrent requests)

Rationale:

  • βœ… Simple, no external dependencies (Redis/Celery)
  • βœ… Perfect for demo/MVP
  • βœ… Fast response times
  • ❌ Not suitable for production scale (no persistence)

Alternative: Redis Queue for production deployments

2. RAG Implementation: Simple Context Injection

Choice: Extract full PDF text, inject first 4000 chars into prompt

Rationale:

  • βœ… Simple, fast for small documents
  • βœ… No vector DB setup required
  • βœ… Works well for demo (3-page PDF)
  • ❌ Doesn't scale to large documents

Alternative: Vector embeddings (FAISS/Chroma) for production

3. State Management: Zustand

Choice: Zustand over Redux/Context API

Rationale:

  • βœ… Minimal boilerplate
  • βœ… Simple API (useChatStore)
  • βœ… Perfect for this app size
  • βœ… TypeScript support

4. PDF Viewer: Browser Native

Choice: <iframe> with browser PDF viewer

Rationale:

  • βœ… Zero dependencies
  • βœ… Built-in search/navigation
  • βœ… #page=X and #search=term support
  • ❌ Limited customization

Alternative: react-pdf for more control

5. Streaming: Direct Model Streaming

Choice: model.astream() instead of LangGraph events

Rationale:

  • βœ… Simpler implementation
  • βœ… Direct token streaming
  • βœ… Fewer edge cases
  • ❌ Less visibility into intermediate steps

Trade-off: Simplified architecture over detailed observability

🎨 UI/UX Highlights

Animations

  • PDF Viewer: Spring-physics slide-in (300-400ms)
  • Messages: Fade + slide up on entrance
  • Tool Indicators: Pulsing dots
  • Streaming Cursor: Blue pulse effect
  • Dark Mode: Smooth color transitions

Responsive Breakpoints

  • Mobile (< 640px): Full-screen PDF, larger touch targets
  • Desktop (β‰₯ 768px): 55/45 chat/PDF split, condensed UI

πŸ”§ Environment Variables

Create .env in backend/ directory:

OPENAI_API_KEY=sk-proj-...your-key-here

πŸ› Known Limitations

  1. Single Document: Currently supports one PDF at a time
  2. No Vector Search: Simple text extraction, not semantic search
  3. In-Memory Queue: Not persistent across restarts
  4. Citation Format: Relies on LLM following format instructions

🚒 Deployment

Local Development

Both servers must run simultaneously:

# Terminal 1 - Backend
cd backend && python -m uvicorn main:app --reload

# Terminal 2 - Frontend  
cd frontend && npm run dev

Production Considerations

  • Add Docker/docker-compose
  • Implement Redis queue
  • Add vector embeddings
  • Enable multi-document support
  • Add rate limiting
  • Implement user authentication

πŸ“ License

MIT

πŸ™ Acknowledgments

About

Perplexity-style AI chat with RAG, streaming responses, PDF citations, and generative UI. Built with Next.js 14 + FastAPI + LangChain.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages