{"id":31124186,"url":"https://github.com/genesisblock3301/langchain-chatbot-fastapi","last_synced_at":"2025-09-17T19:38:29.140Z","repository":{"id":312504072,"uuid":"1047716807","full_name":"GenesisBlock3301/langchain-chatbot-fastAPI","owner":"GenesisBlock3301","description":"An LLM-powered conversational chatbot built with LangChain. This bot can have multi-turn conversations and remembers previous interactions. Serves as a foundation for more advanced conversational AI applications such as RAG-based chatbots and agent-driven bots.","archived":false,"fork":false,"pushed_at":"2025-09-01T05:30:34.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-01T07:40:57.653Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GenesisBlock3301.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-08-31T04:05:00.000Z","updated_at":"2025-09-01T05:30:38.000Z","dependencies_parsed_at":"2025-09-01T07:40:59.336Z","dependency_job_id":null,"html_url":"https://github.com/GenesisBlock3301/langchain-chatbot-fastAPI","commit_stats":null,"previous_names":["genesisblock3301/langchain-chatbot","genesisblock3301/langchain-chatbot-fastapi"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/GenesisBlock3301/langchain-chatbot-fastAPI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenesisBlock3301%2Flangchain-chatbot-fastAPI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenesisBlock3301%2Flangchain-chatbot-fastAPI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenesisBlock3301%2Flangchain-chatbot-fastAPI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenesisBlock3301%2Flangchain-chatbot-fastAPI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GenesisBlock3301","download_url":"https://codeload.github.com/GenesisBlock3301/langchain-chatbot-fastAPI/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenesisBlock3301%2Flangchain-chatbot-fastAPI/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275650066,"owners_count":25503216,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-17T02:00:09.119Z","response_time":84,"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":[],"created_at":"2025-09-17T19:38:25.556Z","updated_at":"2025-09-17T19:38:29.130Z","avatar_url":"https://github.com/GenesisBlock3301.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI Chatbot with MongoDB and LangGraph\n\nA professional-grade conversational AI chatbot built with FastAPI, MongoDB, and LangGraph, featuring persistent conversation history and real-time message streaming.\n\n## 🚀 Features\n\n- **Conversational Memory**: Persistent chat history stored in MongoDB\n- **Multi-user Support**: Separate conversation threads for different users\n- **Real-time Streaming**: Token-by-token response streaming for better UX\n- **Professional Architecture**: Clean separation of concerns with modular design\n- **RESTful API**: FastAPI-powered endpoints for easy integration\n- **State Management**: Custom state handling with LangGraph\n- **Database Integration**: MongoDB for scalable conversation persistence\n\n## 📋 Prerequisites\n\n- Python 3.8 or higher\n- MongoDB instance (local or cloud)\n- API keys for your chosen LLM provider\n\n## 🛠️ Installation\n\n1. **Clone the repository**\n   ```bash\n   git clone \u003cyour-repository-url\u003e\n   cd \u003cyour-project-name\u003e\n   ```\n\n2. **Create a virtual environment**\n   ```bash\n   python -m venv venv\n   source venv/bin/activate  # On Windows: venv\\Scripts\\activate\n   ```\n\n3. **Install dependencies**\n   ```bash\n   pip install -r requirements.txt\n   ```\n\n4. **Set up environment variables**\n   Create a `.env` file in the root directory:\n   ```env\n   MONGODB_URL=mongodb://localhost:27017\n   DATABASE_NAME=chatbot_db\n   COLLECTION_NAME=conversations\n   LLM_API_KEY=your_llm_api_key_here\n   ```\n\n5. **Start MongoDB**\n   - For local MongoDB: Ensure MongoDB service is running\n   - For MongoDB Atlas: Use your connection string in MONGODB_URL\n\n## 🏃‍♂️ Running the Application\n\n### Development Mode\n```bash\nuvicorn main:app --reload --host 0.0.0.0 --port 8000\n```\n\n\n## 📚 API Documentation\n\nOnce the server is running, visit:\n- **Interactive API Docs**: http://localhost:8000/docs\n- **ReDoc Documentation**: http://localhost:8000/redoc\n\n### Main Endpoints\n\n#### Start/Continue Conversation\n```http\nPOST /chat\nContent-Type: application/json\n{\n  \"user_id\": \"3\",\n  \"thread_id\": \"thread_3\",\n  \"text\": \"who am I\"\n}\n```\n\n\n#### Get Conversation History\n```http\nGET /conversation/{thread_id}\n```\n\n#### Delete Conversation\n```http\nDELETE /conversation/{thread_id}\n```\n\n## 🏗️ Project Structure\n\n```\nlangchain-chatbot/\n├── app/                    # All application code\n│   ├── main.py             # FastAPI application and route handlers\n│   ├── db.py               # MongoDB configuration and connection\n│   ├── chatstate.py        # Custom state management and LangGraph setup\n│   └── __init__.py         # Makes it a Python package\n├── tests/                  # Unit and integration tests\n│   └── test_main.py\n├── requirements.txt        # Python dependencies (outside app folder)\n├── .env                    # Environment variables (outside app folder)\n└── README.md \n```\n\n### File Descriptions\n\n- **`main.py`**: Contains the FastAPI application, route definitions, and business logic for handling chat requests\n- **`db.py`**: Database configuration, MongoDB connection setup, and database utility functions\n- **`chatstate.py`**: Custom state schema definition, LangGraph workflow configuration, and model integration\n- **`requirements.txt`**: All Python package dependencies needed for the project\n\n## 🔧 Configuration\n\n### Environment Variables\n\n| Variable | Description | Default |\n|----------|-------------|---------|\n| `MONGODB_URL` | MongoDB connection string | `mongodb://localhost:27017` |\n| `DATABASE_NAME` | Database name for storing conversations | `chatbot_db` |\n| `COLLECTION_NAME` | Collection name for conversation documents | `conversations` |\n| `LLM_API_KEY` | API key for your LLM provider | Required |\n\n### MongoDB Setup\n\nThe application automatically creates the necessary database and collections. Ensure your MongoDB instance is accessible and the user has read/write permissions.\n\n\n## 📊 Features in Detail\n\n### Conversation Memory\n- Persistent storage of all chat interactions\n- Thread-based conversation isolation\n- Automatic message history trimming to manage a context window\n\n### State Management\n- Custom state schema with message history and metadata\n- LangGraph-powered conversation flow\n- Efficient state persistence and retrieval\n\n\n### Common Issues\n\n1. **MongoDB Connection Error**\n   - Verify MongoDB is running\n   - Check connection string in `.env`\n   - Ensure network connectivity\n\n2. **LLM API Rate Limits**\n   - Verify API key is correct\n   - Check your API provider's rate limits\n   - Implement retry logic if needed\n\n3. **Memory Issues with Long Conversations**\n   - Adjust message trimming parameters in `chatstate.py`\n   - Monitor token usage in long conversations\n\n## 🔒 Security Considerations\n\n- Store API keys securely in environment variables\n- Implement proper authentication for production use\n- Sanitize user inputs before processing\n- Set up proper CORS policies for web clients\n- Use HTTPS in production environments\n\n## 🚀 Deployment\n\n### Production Deployment Checklist\n\n- [ ] Set up production MongoDB instance\n- [ ] Configure environment variables securely\n- [ ] Set up reverse proxy (nginx/Apache)\n- [ ] Enable HTTPS with SSL certificates\n- [ ] Configure logging and monitoring\n- [ ] Set up automated backups for MongoDB\n- [ ] Implement rate limiting\n- [ ] Set up health checks\n\n\n\n## 🔗 Related Resources\n\n- [FastAPI Documentation](https://fastapi.tiangolo.com/)\n- [LangChain Documentation](https://python.langchain.com/)\n- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/)\n- [MongoDB Documentation](https://docs.mongodb.com/)\n\n---\n\nBuilt with ❤️ using FastAPI, LangGraph, and MongoDB","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenesisblock3301%2Flangchain-chatbot-fastapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgenesisblock3301%2Flangchain-chatbot-fastapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenesisblock3301%2Flangchain-chatbot-fastapi/lists"}