{"id":49499715,"url":"https://github.com/ammanrizwan/rag-chatbot-using-ollama-langchain","last_synced_at":"2026-05-01T12:03:38.442Z","repository":{"id":315598575,"uuid":"1060098808","full_name":"AmmanRizwan/rag-chatbot-using-ollama-langchain","owner":"AmmanRizwan","description":"A sophisticated Retrieval-Augmented Generation (RAG) chatbot that combines local document processing with web search capabilities. Built with FastAPI backend and React TypeScript frontend.","archived":false,"fork":false,"pushed_at":"2025-09-19T15:33:06.000Z","size":466,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-19T15:36:54.885Z","etag":null,"topics":["docker","dockerfile","fastapi","python","rag-chatbot","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AmmanRizwan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-19T11:36:30.000Z","updated_at":"2025-09-19T15:33:10.000Z","dependencies_parsed_at":"2025-09-19T23:19:11.376Z","dependency_job_id":null,"html_url":"https://github.com/AmmanRizwan/rag-chatbot-using-ollama-langchain","commit_stats":null,"previous_names":["ammanrizwan/rag-chatbot-using-ollama-langchain"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/AmmanRizwan/rag-chatbot-using-ollama-langchain","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmmanRizwan%2Frag-chatbot-using-ollama-langchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmmanRizwan%2Frag-chatbot-using-ollama-langchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmmanRizwan%2Frag-chatbot-using-ollama-langchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmmanRizwan%2Frag-chatbot-using-ollama-langchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AmmanRizwan","download_url":"https://codeload.github.com/AmmanRizwan/rag-chatbot-using-ollama-langchain/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmmanRizwan%2Frag-chatbot-using-ollama-langchain/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32495949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","dockerfile","fastapi","python","rag-chatbot","typescript"],"created_at":"2026-05-01T12:03:37.418Z","updated_at":"2026-05-01T12:03:38.433Z","avatar_url":"https://github.com/AmmanRizwan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AI RAG Chatbot using Ollama \u0026 LangChain\n\nA sophisticated Retrieval-Augmented Generation (RAG) chatbot that combines local document processing with web search capabilities. Built with FastAPI backend and React TypeScript frontend.\n\n## Features\n\n- **RAG Implementation**: Process and query local documents using vector similarity search\n- **PDF Document Upload**: Extract and process text from PDF documents\n- **Web Search Integration**: Uses DuckDuckGo for real-time information retrieval\n- **Streaming Responses**: Real-time streaming chat responses using Server-Sent Events (SSE)\n- **Hybrid Information Sources**: Combines local document knowledge with web search results\n- **Modern UI**: React TypeScript frontend with Tailwind CSS styling\n- **Markdown Support**: Full markdown rendering for rich text responses\n\n## Architecture\n\n### Backend (Python FastAPI)\n- **Vector Store**: ChromaDB for document embeddings and similarity search\n- **Embeddings**: Ollama's `nomic-embed-text` model for text embeddings\n- **LLM**: Configurable Ollama model (default: `gemma2:2b`)\n- **Document Processing**: PyPDF2 for PDF text extraction\n- **Text Splitting**: LangChain's CharacterTextSplitter for document chunking\n- **Web Search**: DuckDuckGo integration for external information\n\n### Frontend (React TypeScript)\n- **Real-time Chat**: Server-Sent Events for streaming responses\n- **File Upload**: Drag-and-drop PDF document upload\n- **Markdown Rendering**: Rich text display with react-markdown\n- **Modern UI**: Tailwind CSS with custom components\n\n## Algorithm Overview\n\nThe chatbot follows this intelligent workflow:\n\n1. **Document Processing**:\n   - PDFs are uploaded and text is extracted using PyPDF2\n   - Text is chunked into manageable pieces (1000 chars, 200 overlap)\n   - Chunks are embedded using `nomic-embed-text` and stored in ChromaDB\n\n2. **Query Processing**:\n   - User questions are embedded and compared against document vectors\n   - Similarity search retrieves relevant document chunks (threshold: 0.7)\n   - Web search is performed for complementary information\n\n3. **Response Generation**:\n   - Context is built from both local documents and web search results\n   - Custom prompt template guides the LLM to cite sources appropriately\n   - Response is streamed back to the user with source attribution\n\n4. **Source Attribution**:\n   - Clearly indicates information from uploaded documents vs. web search\n   - Displays conflicting information when sources disagree\n   - Provides transparency about information sources\n\n## Installation\n\n### Prerequisites\n- Python 3.8+\n- Node.js 16+\n- [Ollama](https://ollama.ai/) installed and running\n\n### Backend Setup\n\n1. Clone the repository:\n```bash\ngit clone \u003crepository-url\u003e\ncd rag-chatbot-using-ollama-langchain\n```\n\n2. Create a virtual environment:\n```bash\npython -m venv venv\nsource venv/bin/activate  # On Windows: venv\\Scripts\\activate\n```\n\n3. Install Python dependencies:\n```bash\npip install -r requirements.txt\n```\n\n4. Set up environment variables:\n```bash\ncp .env.example .env\n# Edit .env with your configuration\n```\n\n5. Pull the required Ollama models:\n```bash\nollama pull gemma2:2b\nollama pull nomic-embed-text\n```\n\n### Frontend Setup\n\n1. Navigate to the chat directory:\n```bash\ncd chat\n```\n\n2. Install dependencies:\n```bash\nnpm install\n```\n\n3. Set up environment variables:\n```bash\ncp .env.example .env\n# Edit .env if using Supabase features\n```\n\n## Usage\n\n### Start the Backend Server\n\n```bash\n# From the root directory\npython main.py\n```\n\nThe API will be available at `http://localhost:8000`\n\n### Start the Frontend\n\n```bash\n# From the chat directory\nnpm run dev\n```\n\nThe frontend will be available at `http://localhost:5173`\n\n## API Endpoints\n\n### POST `/upload`\nUpload and process PDF documents.\n\n**Request**: Multipart form data with PDF file\n**Response**: \n```json\n{\n  \"message\": \"Successfully processed and added filename.pdf\",\n  \"document_count\": 5\n}\n```\n\n### POST `/chat`\nSend a question and receive streaming response.\n\n**Request**:\n```json\n{\n  \"question\": \"What is LangChain?\"\n}\n```\n\n**Response**: Server-Sent Events stream with:\n- `token` events: Streaming response tokens\n- `sources` events: Source attribution\n- `done` event: End of response\n\n### GET `/`\nHealth check endpoint.\n\n## Configuration\n\n### Environment Variables\n\n**Root `.env`**:\n```env\nMODEL_NAME=gemma2:2b\nPORT=8000\n```\n\n### Model Configuration\n\nYou can change the Ollama model by updating the `MODEL_NAME` in your `.env` file:\n- `gemma2:2b` (lightweight, fast)\n- `llama3:8b` (more capable, slower)\n- `mistral:7b` (balanced performance)\n\n## Dependencies\n\n### Backend\n- `fastapi`: Web framework\n- `uvicorn`: ASGI server\n- `langchain-ollama`: Ollama integration\n- `langchain-community`: Community tools and integrations\n- `chromadb`: Vector database\n- `pypdf2`: PDF processing\n- `python-dotenv`: Environment management\n\n### Frontend\n- `react`: UI framework\n- `typescript`: Type safety\n- `vite`: Build tool\n- `tailwindcss`: Styling\n- `react-markdown`: Markdown rendering\n- `lucide-react`: Icons\n\n## Project Structure\n\n```\n├── main.py                 # FastAPI backend server\n├── requirements.txt        # Python dependencies\n├── .env                   # Backend environment variables\n├── README.md              # Project documentation\n└── chat/                  # React frontend\n    ├── src/\n    │   ├── components/    # React components\n    │   ├── hooks/        # Custom React hooks\n    │   ├── lib/          # Utility functions\n    │   └── App.tsx       # Main application\n    ├── package.json      # Node.js dependencies\n    └── vite.config.ts    # Vite configuration\n```\n\n## Features in Detail\n\n### Intelligent Source Combination\nThe system intelligently combines information from multiple sources:\n- **Local Documents**: High relevance threshold (0.7) ensures quality matches\n- **Web Search**: Always performed for current/complementary information\n- **Conflict Resolution**: Acknowledges and explains source disagreements\n\n### Streaming Responses\nReal-time response streaming provides:\n- Immediate feedback to users\n- Better user experience for long responses\n- Source attribution after response completion\n\n### Document Processing Pipeline\nRobust document handling:\n- PDF text extraction with error handling\n- Intelligent text chunking (1000 chars, 200 overlap)\n- Vector embedding and storage\n- Efficient similarity search\n\n## Contributing\n\n1. Fork the repository\n2. Create a feature branch\n3. Make your changes\n4. Add tests if applicable\n5. Submit a pull request\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## Acknowledgments\n\n- [Ollama](https://ollama.ai/) for local LLM hosting\n- [LangChain](https://langchain.com/) for RAG framework\n- [ChromaDB](https://www.trychroma.com/) for vector storage\n- [DuckDuckGo](https://duckduckgo.com/) for web search API\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fammanrizwan%2Frag-chatbot-using-ollama-langchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fammanrizwan%2Frag-chatbot-using-ollama-langchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fammanrizwan%2Frag-chatbot-using-ollama-langchain/lists"}