A powerful AI-powered chat application that combines Retrieval Augmented Generation (RAG) with web search capabilities. Upload PDF documents and ask questions to get intelligent answers based on your documents and real-time web information.
- Upload PDF documents through an intuitive drag-and-drop interface
- Automatic document parsing and text extraction
- Intelligent chunking and vectorization for efficient retrieval
- RAG Answers: Get answers based on your uploaded PDF documents using LangChain's vector store
- Web Search Answers: Get real-time information from the web using SerpAPI integration
- Compare both answers side-by-side for comprehensive understanding
- Voice Input: Use speech recognition to ask questions hands-free
- Voice Output: Listen to answers with text-to-speech functionality
- Chat Mode: Toggle between text and voice interaction modes
- Clean, responsive interface built with Ant Design
- Real-time conversation history display
- Loading states and error handling
- Fixed chat input for easy access
- React 18: Modern React with hooks and functional components
- Ant Design: Beautiful and professional UI components
- React Speech Recognition: Browser-based speech-to-text
- Speak-TTS: Text-to-speech synthesis
- Axios: HTTP client for API communication
- Express.js: Fast and minimalist web framework
- LangChain: Powerful framework for building LLM applications
- OpenAI API: GPT models for intelligent question answering
- Model Context Protocol (MCP): Protocol for connecting AI models with external tools
- SerpAPI: Web search integration
- PDF-Parse: PDF document parsing
- Multer: File upload handling
- Vector Stores: Memory-based vector storage for semantic search
- Embeddings: OpenAI embeddings for document vectorization
- Retrieval Augmented Generation (RAG): Combines retrieval and generation for accurate answers
- Text Splitting: Recursive character text splitting for optimal chunking
agentai/
βββ src/ # React frontend
β βββ components/
β β βββ ChatComponent.js # Main chat interface with voice support
β β βββ PdfUploader.js # PDF upload component
β β βββ RenderQA.js # Q&A display component
β βββ App.js # Main application component
β βββ index.js # React entry point
β
βββ server/ # Express backend
β βββ server.js # Express server setup
β βββ chat.js # RAG implementation with LangChain
β βββ chat-mcp.js # MCP client for web search
β βββ mcp-server.js # MCP server for SerpAPI integration
β βββ uploads/ # PDF storage directory
β
βββ public/ # Static assets
βββ package.json # Frontend dependencies
-
PDF Upload Flow:
User β PdfUploader β Express /upload β Multer β server/uploads/ -
Question Answering Flow:
User Question β ChatComponent β Express /chat ββ chat.js (RAG from PDF) ββ chat-mcp.js (Web Search) β Combined Response β User -
RAG Pipeline:
PDF β PDFLoader β TextSplitter β Embeddings β VectorStore β Retriever β LLM β Answer -
MCP Web Search Flow:
Query β MCP Client β MCP Server β SerpAPI β Search Results β LLM β Summary
- Node.js (v16 or higher)
- npm or yarn
- OpenAI API key
- SerpAPI key (optional, for web search)
git clone https://github.com/leo-Zhizhu/AgentAI.git
cd AgentAIFrontend:
npm installBackend:
cd server
npm install
cd ..Create a .env file in the server/ directory:
cd server
cp ../.env.example .envEdit .env and add your API keys:
OPENAI_API_KEY=your_openai_api_key_here
SERPAPI_KEY=your_serpapi_key_here
PORT=5001Option 1: Run both frontend and backend together
npm run devOption 2: Run separately
Terminal 1 (Backend):
npm run serverTerminal 2 (Frontend):
npm startThe application will be available at:
- Frontend: http://localhost:3000
- Backend: http://localhost:5001
Follow the installation steps above. The application runs in development mode with hot-reloading.
Build Frontend:
npm run buildRun Production Server:
# Install production dependencies
npm install --production
# Set NODE_ENV
export NODE_ENV=production
# Start server
cd server
npm startFrontend (Vercel):
- Connect your GitHub repository to Vercel
- Set build command:
npm run build - Set output directory:
build - Add environment variable:
REACT_APP_DOMAIN=your_backend_url
Backend (Railway/Heroku):
- Create a new project and connect your repository
- Set root directory to
server/ - Add environment variables:
OPENAI_API_KEYSERPAPI_KEYPORT(usually auto-set by platform)
- Deploy
Create a Dockerfile in the root:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
COPY server/package*.json ./server/
RUN npm install && cd server && npm install
COPY . .
RUN npm run build
EXPOSE 5001
CMD ["node", "server/server.js"]- Build the frontend:
npm run build - Serve the
build/directory with a static file server (nginx, Apache) - Run the backend as a Node.js service (PM2, systemd)
- Configure reverse proxy to route API calls to backend
Ensure these are set in your production environment:
OPENAI_API_KEY: RequiredSERPAPI_KEY: Required for web searchNODE_ENV: Set toproductionPORT: Backend port (default: 5001)REACT_APP_DOMAIN: Frontend environment variable pointing to backend URL
- Never commit
.envfiles or API keys to version control - The
.gitignorefile is configured to exclude sensitive files - Uploaded PDFs are stored locally and should be secured in production
- Consider implementing authentication and rate limiting for production use
- Upload a PDF: Drag and drop or click to upload a PDF document
- Ask Questions: Type your question in the search box or use voice input
- View Answers: See both RAG (from PDF) and web search answers
- Voice Mode: Toggle "Chat Mode" to enable voice interaction
- Conversation History: View all previous questions and answers
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
- LangChain for the powerful RAG framework
- OpenAI for GPT models
- Ant Design for beautiful UI components
- Model Context Protocol for tool integration
Note: Make sure to keep your API keys secure and never expose them in client-side code or public repositories.