AiTutor is a web application that allows users to upload PDFs and chat with their documents using AI. The application processes PDF documents, splits them into chunks, and uses vector embeddings to retrieve relevant content when answering user questions.
- 📄 PDF document upload and viewing
- 💬 AI-powered chat interface for document Q&A
- 🔍 Vector search for semantic retrieval of document content
- 🔄 Conversation history and management
- 🔐 User authentication system
- ⚙️ OpenAI API key integration
- Frontend: Next.js, React, TailwindCSS
- Backend: Next.js API routes
- Database: PostgreSQL with pgvector extension
- AI Integration: OpenAI GPT-4 API, LangChain
- Authentication: Custom session-based auth
- Storage: Vercel Blob Storage for PDFs
- Node.js v18+
- Docker and Docker Compose
- OpenAI API key
- Vercel Blob Storage account or another blob storage solution (optional for production)
git clone https://github.com/CruiseDevice/ai-tutor
cd ai-tutor
npm install
Start the PostgreSQL container:
docker-compose up -d
Create a .env
file in the root of your project:
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL=""
NODE_ENV=""
BLOB_READ_WRITE_TOKEN=""
Generate Prisma client:
npx prisma generate
Create database tables:
npx prisma db push
Start the development server:
npm run dev
The application should now be running at http://localhost:3000.
- Register a new account at
/register
- Navigate to API Settings and add your OpenAI API key
- Return to the dashboard to upload and chat with PDFs
/app
: Next.js application routes and components/api
: Backend API routes/components
: React components/auth
: Authentication related files
/lib
: Utility libraries/auth.ts
: Authentication utility functions/db.ts
: Database client setup/pgvector.ts
: Vector database utilities
-
Authentication
/api/auth/register
: User registration/api/auth/login
: User login/api/auth/logout
: User logout/api/auth/verify-session
: Verify user session/api/auth/user
: Get current user information
-
Document Management
/api/documents
: Upload and create new documents/api/documents/process
: Process and index uploaded documents
-
Conversation Management
/api/conversations
: List all conversations/api/conversations/[id]
: Get a specific conversation
-
Chat
/api/chat
: Send a message and get AI responses
- Document Upload: User uploads a PDF through the interface
- Document Processing:
- PDF is stored in Blob Storage
- Document is split into chunks
- Each chunk gets a vector embedding using OpenAI's embedding model
- Chunks and embeddings are stored in PostgreSQL
- Chatting with Document:
- User sends a question
- System finds relevant chunks using vector similarity search
- OpenAI's GPT model generates a response using the relevant document chunks
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License.
If you're having trouble connecting to the PostgreSQL database:
- Check that the Docker container is running:
docker ps
- Verify the connection string in your
.env
file - Try connecting using a PostgreSQL client like pgAdmin or psql
If the AI chat is not working properly:
- Check that you've added your OpenAI API key in the application settings
- Verify that your API key has sufficient credits
- Check the application logs for any API-related errors
If PDFs aren't being processed correctly:
- Check that the PDF is not password-protected
- Verify that the PDF size is under the upload limit (10MB)
- Check the application logs for processing errors