# rag-qa-project **Repository Path**: svnspot/rag-qa-project ## Basic Information - **Project Name**: rag-qa-project - **Description**: No description available - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-24 - **Last Updated**: 2026-04-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
# ๐Ÿค– RAG Q&A System ### Production-Ready Retrieval-Augmented Generation with FastAPI & LangChain [![Python](https://img.shields.io/badge/Python-3.12+-blue.svg)](https://www.python.org/downloads/) [![FastAPI](https://img.shields.io/badge/FastAPI-0.109+-00a393.svg)](https://fastapi.tiangolo.com) [![LangChain](https://img.shields.io/badge/LangChain-0.3+-1C3C3C.svg)](https://python.langchain.com) [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) [![CI/CD](https://img.shields.io/badge/CI%2FCD-GitHub%20Actions-2088FF.svg)](https://github.com/features/actions) [![Docker](https://img.shields.io/badge/Docker-Ready-2496ED.svg)](https://www.docker.com/) [![AWS](https://img.shields.io/badge/AWS-App%20Runner-FF9900.svg)](https://aws.amazon.com/apprunner/) [Features](#-features) โ€ข [Quick Start](#-quick-start) โ€ข [API Docs](#-api-endpoints) โ€ข [Deployment](#-deployment) โ€ข [Contributing](#-contributing)
--- ## ๐Ÿ“– Overview A **production-ready** RAG (Retrieval-Augmented Generation) system that enables intelligent Q&A over your documents. Built with modern AI stack and battle-tested in production environments. ### ๐ŸŽฏ What is RAG? RAG combines the power of **retrieval** (finding relevant information) with **generation** (creating coherent answers) to provide accurate, context-aware responses to your questions based on your own documents. ### ๐ŸŒŸ Key Highlights - ๐Ÿš€ **Production Ready**: Docker + CI/CD + AWS deployment - ๐Ÿง  **Smart AI**: Powered by OpenAI GPT-4o & LangChain - ๐Ÿ“Š **Observable**: LangSmith integration for full tracing - โœ… **Evaluated**: RAGAS metrics for answer quality - ๐Ÿ”’ **Secure**: Non-root Docker, API validation, error handling - โšก **Fast**: Async operations, streaming responses - ๐Ÿ“ˆ **Scalable**: Cloud-native architecture --- ## โœจ Features ### ๐Ÿ“„ Document Management - โœ… Upload **PDF**, **TXT**, and **CSV** files - โœ… Automatic text extraction and chunking - โœ… Smart document splitting with overlap - โœ… Vector storage in Qdrant Cloud ### ๐Ÿ’ฌ Intelligent Q&A - โœ… Natural language questions - โœ… Context-aware answers - โœ… Source attribution (see which docs were used) - โœ… Streaming responses for real-time feedback - โœ… Multiple query modes (standard, search-only) ### ๐Ÿ” Observability & Quality - โœ… **LangSmith Tracing**: Full chain visibility, token tracking, cost analysis - โœ… **RAGAS Evaluation**: Faithfulness & answer relevancy metrics - โœ… **Structured Logging**: Comprehensive error tracking - โœ… **Health Checks**: Readiness & liveness endpoints ### ๐Ÿ› ๏ธ Developer Experience - โœ… **Auto-generated Swagger docs** at `/docs` - โœ… **Type-safe** Pydantic models - โœ… **Comprehensive tests** with 70%+ coverage - โœ… **Hot reload** in development - โœ… **CI/CD pipeline** with GitHub Actions --- ## ๐Ÿ—๏ธ Architecture ```mermaid graph LR A[๐Ÿ‘ค User] -->|Upload Docs| B[FastAPI] A -->|Ask Question| B B --> C[๐Ÿ“ Document Processor] C --> D[๐Ÿ”ข OpenAI Embeddings] D --> E[(Qdrant Vector DB)] B --> F[๐Ÿ” RAG Chain] F --> E F --> G[๐Ÿค– OpenAI GPT-4o] G --> H[๐Ÿ“Š RAGAS Evaluator] F --> I[๐Ÿ“ˆ LangSmith] H --> A ``` ### Tech Stack | Component | Technology | Purpose | |-----------|------------|---------| | ๐Ÿ Language | **Python 3.12** | Modern Python with type hints | | ๐Ÿš€ API Framework | **FastAPI** | High-performance async API | | ๐Ÿง  RAG Framework | **LangChain** | LLM orchestration | | ๐Ÿ—„๏ธ Vector DB | **Qdrant Cloud** | Semantic search | | ๐Ÿ”ข Embeddings | **text-embedding-3-small** | Document encoding | | ๐Ÿค– LLM | **GPT-4o-mini** | Answer generation | | ๐Ÿ“Š Evaluation | **RAGAS** | Quality metrics | | ๐Ÿ” Observability | **LangSmith** | Tracing & monitoring | | ๐Ÿณ Container | **Docker** | Containerization | | โ˜๏ธ Cloud | **AWS App Runner** | Serverless hosting | | ๐Ÿ”„ CI/CD | **GitHub Actions** | Automated pipeline | --- ## ๐Ÿš€ Quick Start ### Prerequisites - ๐Ÿ Python 3.12+ - ๐Ÿ”‘ OpenAI API key ([Get one](https://platform.openai.com/api-keys)) - ๐Ÿ—„๏ธ Qdrant Cloud account ([Sign up](https://cloud.qdrant.io)) ### 1๏ธโƒฃ Clone & Install ```bash # Clone repository git clone https://github.com/yourusername/rag-qa-project.git cd rag-qa-project # Install with UV (recommended) uv sync # Or with pip pip install -r requirements.txt ``` ### 2๏ธโƒฃ Configure Environment ```bash # Copy environment template cp .env.example .env # Edit with your credentials nano .env ``` **Required variables:** ```bash OPENAI_API_KEY=sk-proj-your-key-here QDRANT_URL=https://your-cluster.qdrant.io QDRANT_API_KEY=your-qdrant-api-key ``` **Optional - LangSmith Observability:** ```bash LANGCHAIN_TRACING_V2=true LANGCHAIN_API_KEY=lsv2_pt_your-key-here LANGCHAIN_PROJECT=rag-qa-production ``` ### 3๏ธโƒฃ Run Application ```bash # Development mode with hot reload uvicorn app.main:app --reload # Or using Python python -m app.main ``` ### 4๏ธโƒฃ Access API ๐ŸŒ **Swagger UI**: http://localhost:8000/docs ๐Ÿ“š **ReDoc**: http://localhost:8000/redoc ๐Ÿ” **Health Check**: http://localhost:8000/health --- ## ๐Ÿ“ API Endpoints ### Document Management | Endpoint | Method | Description | Example | |----------|--------|-------------|---------| | ๐Ÿ“ค `/documents/upload` | POST | Upload document | [See below](#upload-document) | | โ„น๏ธ `/documents/info` | GET | Get collection stats | `curl /documents/info` | | ๐Ÿ—‘๏ธ `/documents/collection` | DELETE | Delete all documents | `curl -X DELETE /documents/collection` | ### Query & Search | Endpoint | Method | Description | Features | |----------|--------|-------------|----------| | ๐Ÿ’ฌ `/query` | POST | Ask a question | Sources, Evaluation | | ๐ŸŒŠ `/query/stream` | POST | Streaming response | Real-time tokens | | ๐Ÿ” `/query/search` | POST | Search only | No generation | ### Health & Monitoring | Endpoint | Method | Description | |----------|--------|-------------| | โค๏ธ `/health` | GET | Basic health check | | โœ… `/health/ready` | GET | Readiness with DB status | --- ## ๐Ÿ’ก Usage Examples ### Upload a Document ```bash curl -X POST "http://localhost:8000/documents/upload" \ -H "Content-Type: multipart/form-data" \ -F "file=@research_paper.pdf" ``` **Response:** ```json { "message": "Document uploaded and processed successfully", "filename": "research_paper.pdf", "chunks_created": 42, "document_ids": ["uuid-1", "uuid-2", ...] } ``` ### Ask a Question ```bash curl -X POST "http://localhost:8000/query" \ -H "Content-Type: application/json" \ -d '{ "question": "What is the main contribution of this paper?", "include_sources": true, "enable_evaluation": false }' ``` **Response:** ```json { "question": "What is the main contribution of this paper?", "answer": "The main contribution is...", "sources": [ { "content": "Excerpt from page 3...", "metadata": {"source": "research_paper.pdf", "page": 3} } ], "processing_time_ms": 1234.5 } ``` ### Query with RAGAS Evaluation ```bash curl -X POST "http://localhost:8000/query" \ -H "Content-Type: application/json" \ -d '{ "question": "Summarize the methodology", "include_sources": true, "enable_evaluation": true }' ``` **Response includes evaluation scores:** ```json { "question": "Summarize the methodology", "answer": "The methodology involves...", "sources": [...], "processing_time_ms": 3456.7, "evaluation": { "faithfulness": 0.95, "answer_relevancy": 0.87, "evaluation_time_ms": 1200.5, "error": null } } ``` --- ## ๐Ÿณ Docker Deployment ### Using Docker Compose (Recommended) ```bash # Build and run docker-compose up --build # Run in detached mode docker-compose up -d # View logs docker-compose logs -f app # Stop docker-compose down ``` ### Using Docker Directly ```bash # Build image docker build -t rag-qa-system . # Run container docker run -p 8000:8000 --env-file .env rag-qa-system # Run with custom port docker run -p 3000:8000 --env-file .env rag-qa-system ``` --- ## โ˜๏ธ Deployment ### AWS App Runner (Serverless) Fully automated deployment with GitHub Actions: 1๏ธโƒฃ **Setup AWS Resources** ```bash # Create ECR repository aws ecr create-repository --repository-name rag-qa-system --region us-east-1 # Create IAM role for App Runner aws iam create-role --role-name AppRunnerECRAccessRole --assume-role-policy-document file://trust-policy.json ``` 2๏ธโƒฃ **Configure GitHub Secrets** Add these secrets in: `Settings โ†’ Secrets and variables โ†’ Actions` - `AWS_ACCESS_KEY_ID` - `AWS_SECRET_ACCESS_KEY` - `APP_RUNNER_ECR_ACCESS_ROLE_ARN` - `OPENAI_API_KEY` - `QDRANT_URL` - `QDRANT_API_KEY` - `LANGCHAIN_API_KEY` (optional) 3๏ธโƒฃ **Deploy** ```bash # Push to main branch git push origin main # GitHub Actions automatically: # โœ… Runs tests # โœ… Builds Docker image # โœ… Pushes to ECR # โœ… Deploys to App Runner ``` 4๏ธโƒฃ **Access Your API** Your app will be available at: `https://xxxxx.us-east-1.awsapprunner.com` ๐Ÿ“– **Full Guide**: See [AWS Deployment Guide](docs/AWS_DEPLOYMENT_GUIDE.md) --- ## ๐Ÿงช Testing ### Run All Tests ```bash # Install dev dependencies uv sync --extra dev # Run tests with coverage pytest --cov=app --cov-report=html # View coverage report open htmlcov/index.html ``` ### Run Specific Tests ```bash # Test query endpoints pytest tests/test_query.py -v # Test RAG chain pytest tests/test_rag_chain.py -v # Test RAGAS evaluator pytest tests/test_ragas_evaluator.py -v ``` ### Code Quality ```bash # Format code uv run black app/ tests/ # Lint code uv run ruff check app/ tests/ # Fix linting issues uv run ruff check app/ tests/ --fix # Type checking uv run mypy app/ ``` --- ## โš™๏ธ Configuration ### Environment Variables | Variable | Default | Description | |----------|---------|-------------| | **Required** ||| | `OPENAI_API_KEY` | - | OpenAI API key | | `QDRANT_URL` | - | Qdrant Cloud URL | | `QDRANT_API_KEY` | - | Qdrant API key | | **Document Processing** ||| | `COLLECTION_NAME` | `rag_documents` | Vector collection name | | `CHUNK_SIZE` | `1000` | Text chunk size | | `CHUNK_OVERLAP` | `200` | Chunk overlap tokens | | **AI Models** ||| | `EMBEDDING_MODEL` | `text-embedding-3-small` | OpenAI embedding model | | `LLM_MODEL` | `gpt-4o-mini` | OpenAI chat model | | `LLM_TEMPERATURE` | `0.0` | LLM temperature (0-2) | | **RAGAS Evaluation** ||| | `RAGAS_LLM_MODEL` | (uses `LLM_MODEL`) | Model for evaluation | | `RAGAS_TIMEOUT_SECONDS` | `30.0` | Evaluation timeout | | **LangSmith Observability** ||| | `LANGCHAIN_TRACING_V2` | `false` | Enable LangSmith tracing | | `LANGCHAIN_API_KEY` | - | LangSmith API key | | `LANGCHAIN_PROJECT` | `rag-qa-production` | Project name | | **API Settings** ||| | `API_HOST` | `0.0.0.0` | API host | | `API_PORT` | `8000` | API port | | `LOG_LEVEL` | `INFO` | Logging level | --- ## ๐Ÿ“Š Project Structure ``` rag-qa-project/ โ”œโ”€โ”€ ๐Ÿ“ app/ # Application code โ”‚ โ”œโ”€โ”€ main.py # FastAPI app entry โ”‚ โ”œโ”€โ”€ config.py # Configuration โ”‚ โ”œโ”€โ”€ ๐Ÿ“ api/ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ routes/ # API endpoints โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ health.py # Health checks โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ documents.py # Document management โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ query.py # Q&A endpoints โ”‚ โ”‚ โ””โ”€โ”€ schemas.py # Pydantic models โ”‚ โ”œโ”€โ”€ ๐Ÿ“ core/ # Business logic โ”‚ โ”‚ โ”œโ”€โ”€ document_processor.py # Doc loading & chunking โ”‚ โ”‚ โ”œโ”€โ”€ embeddings.py # Embedding service โ”‚ โ”‚ โ”œโ”€โ”€ vector_store.py # Qdrant operations โ”‚ โ”‚ โ”œโ”€โ”€ rag_chain.py # RAG orchestration โ”‚ โ”‚ โ””โ”€โ”€ ragas_evaluator.py # Quality evaluation โ”‚ โ””โ”€โ”€ ๐Ÿ“ utils/ โ”‚ โ””โ”€โ”€ logger.py # Logging setup โ”œโ”€โ”€ ๐Ÿ“ tests/ # Test suite โ”‚ โ”œโ”€โ”€ conftest.py # Pytest fixtures โ”‚ โ”œโ”€โ”€ test_query.py # API tests โ”‚ โ”œโ”€โ”€ test_rag_chain.py # RAG tests โ”‚ โ””โ”€โ”€ test_ragas_evaluator.py # Evaluation tests โ”œโ”€โ”€ ๐Ÿ“ .github/workflows/ # CI/CD pipelines โ”‚ โ”œโ”€โ”€ ci.yml # Continuous Integration โ”‚ โ””โ”€โ”€ deploy.yml # Deployment to AWS โ”œโ”€โ”€ ๐Ÿ“ docs/ # Documentation โ”‚ โ””โ”€โ”€ AWS_DEPLOYMENT_GUIDE.md # AWS setup guide โ”œโ”€โ”€ Dockerfile # Production container โ”œโ”€โ”€ docker-compose.yml # Local development โ”œโ”€โ”€ pyproject.toml # Project config (UV) โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ requirements-dev.txt # Dev dependencies โ”œโ”€โ”€ .env.example # Environment template โ”œโ”€โ”€ CLAUDE.md # Project instructions โ””โ”€โ”€ README.md # This file ``` --- ## ๐Ÿ”„ CI/CD Pipeline ### Continuous Integration (`.github/workflows/ci.yml`) Runs on every push and pull request: 1. โœ… **Code Quality** - Ruff linting - Black formatting check 2. โœ… **Testing** - Unit tests with pytest - 70%+ code coverage required - Coverage reports uploaded 3. โœ… **Docker Build** - Multi-stage build - Image size optimization - Container health check 4. โœ… **Security** - Bandit security scan - Dependency vulnerability check (Safety) ### Continuous Deployment (`.github/workflows/deploy.yml`) Deploys to AWS on push to `main`: 1. ๐Ÿ—๏ธ **Build & Push** - Build Docker image - Tag with commit SHA + latest - Push to AWS ECR 2. ๐Ÿš€ **Deploy** - Update App Runner service - Zero-downtime deployment - Auto-rollback on failure 3. โœ… **Verify** - Health check - Smoke tests --- ## ๐Ÿค Contributing We welcome contributions! Here's how you can help: ### ๐Ÿ› Report Bugs Found a bug? [Open an issue](https://github.com/yourusername/rag-qa-project/issues) with: - Clear description - Steps to reproduce - Expected vs actual behavior - Environment details ### ๐Ÿ’ก Suggest Features Have an idea? [Create a feature request](https://github.com/yourusername/rag-qa-project/issues/new) with: - Use case description - Proposed solution - Alternative approaches considered ### ๐Ÿ”ง Submit Pull Requests 1. Fork the repository 2. Create a feature branch (`git checkout -b feature/amazing-feature`) 3. Make your changes 4. Add tests for new functionality 5. Ensure all tests pass (`pytest`) 6. Format code (`black app/ tests/`) 7. Lint code (`ruff check app/ tests/`) 8. Commit changes (`git commit -m 'Add amazing feature'`) 9. Push to branch (`git push origin feature/amazing-feature`) 10. Open a Pull Request ### ๐Ÿ“œ Development Guidelines - โœ… Write tests for new features - โœ… Maintain >70% code coverage - โœ… Follow PEP 8 style guide - โœ… Add docstrings to functions - โœ… Update documentation - โœ… Keep commits atomic and descriptive --- ## ๐Ÿ“š Additional Resources ### Documentation - ๐Ÿ“– [FastAPI Docs](https://fastapi.tiangolo.com/) - ๐Ÿฆœ [LangChain Docs](https://python.langchain.com/) - ๐Ÿ—„๏ธ [Qdrant Docs](https://qdrant.tech/documentation/) - ๐Ÿค– [OpenAI API Docs](https://platform.openai.com/docs) - ๐Ÿ“Š [RAGAS Docs](https://docs.ragas.io/) - ๐Ÿ” [LangSmith Docs](https://docs.smith.langchain.com/) ### Tutorials & Guides - ๐ŸŽ“ [Building Production RAG Systems](https://www.anthropic.com/research/building-rag-systems) - ๐Ÿš€ [FastAPI Best Practices](https://github.com/zhanymkanov/fastapi-best-practices) - ๐Ÿณ [Docker Best Practices](https://docs.docker.com/develop/dev-best-practices/) --- ## ๐Ÿ“„ License This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details. ``` MIT License Copyright (c) 2025 RAG Q&A System Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. ``` --- ## ๐Ÿ™ Acknowledgments Built with amazing open-source tools: - ๐Ÿš€ [FastAPI](https://fastapi.tiangolo.com/) - Modern web framework - ๐Ÿฆœ [LangChain](https://python.langchain.com/) - LLM framework - ๐Ÿ—„๏ธ [Qdrant](https://qdrant.tech/) - Vector database - ๐Ÿค– [OpenAI](https://openai.com/) - AI models - ๐Ÿณ [Docker](https://www.docker.com/) - Containerization - โ˜๏ธ [AWS](https://aws.amazon.com/) - Cloud infrastructure - ๐Ÿ“Š [RAGAS](https://docs.ragas.io/) - RAG evaluation - ๐Ÿ” [LangSmith](https://smith.langchain.com/) - Observability Special thanks to all [contributors](https://github.com/yourusername/rag-qa-project/graphs/contributors)! --- ## ๐Ÿ’ฌ Support - ๐Ÿ“ง **Email**: your.email@example.com - ๐Ÿ’ฌ **Issues**: [GitHub Issues](https://github.com/yourusername/rag-qa-project/issues) - ๐Ÿ“– **Documentation**: [Wiki](https://github.com/yourusername/rag-qa-project/wiki) - ๐Ÿฆ **Twitter**: [@yourhandle](https://twitter.com/yourhandle) ---
### โญ Star this repo if you find it helpful! **Made with โค๏ธ by the RAG Q&A Team** [Report Bug](https://github.com/yourusername/rag-qa-project/issues) โ€ข [Request Feature](https://github.com/yourusername/rag-qa-project/issues) โ€ข [Contribute](CONTRIBUTING.md)