Skip to content

skydev9293/Clinical-ChatBot

Repository files navigation

Clinical AI ChatBot

A specialized clinical AI chatbot application powered by RAG (Retrieval Augmented Generation) technology. This application provides evidence-based medical information to healthcare professionals.

Features

  • RAG-Powered Responses: Uses Retrieval Augmented Generation for accurate, context-aware answers
  • Clinical Document Processing: Upload and index clinical guidelines, research papers, and medical documents
  • Real-time Chat Interface: Professional, responsive chat UI built with Next.js and Material-UI
  • Conversation Management: Maintain conversation context across multiple interactions
  • Vector Database: Pinecone integration for efficient document retrieval
  • Comprehensive Testing: Full test coverage for backend services

Technology Stack

Backend

  • Python 3.10+
  • FastAPI: Modern, fast web framework
  • LangChain: LLM orchestration and RAG implementation
  • Pinecone: Vector database for document embeddings
  • OpenAI GPT-4: Large language model for response generation
  • PyPDF: PDF document processing

Frontend

  • Next.js 14: React framework with TypeScript
  • Material-UI (MUI): Professional UI component library
  • Zustand: Lightweight state management
  • Axios: HTTP client for API communication
  • React Markdown: Markdown rendering for AI responses

Database

  • Pinecone: Cloud-based vector database

Project Structure

Clinical-ChatBot/
├── backend/
│   ├── app/
│   │   ├── api/
│   │   │   └── routes/
│   │   │       ├── chat.py          # Chat endpoints
│   │   │       ├── documents.py     # Document management
│   │   │       └── health.py        # Health checks
│   │   ├── models/
│   │   │   └── schemas.py           # Pydantic models
│   │   ├── services/
│   │   │   ├── pinecone_service.py  # Vector DB operations
│   │   │   ├── document_processor.py # Document processing
│   │   │   └── rag_engine.py        # RAG implementation
│   │   ├── config.py                # Configuration
│   │   └── main.py                  # FastAPI app
│   ├── tests/
│   │   ├── test_rag_engine.py
│   │   ├── test_document_processor.py
│   │   └── test_api_chat.py
│   ├── requirements.txt
│   └── .env.example
├── frontend/
│   ├── src/
│   │   ├── components/
│   │   │   ├── ChatContainer.tsx    # Main chat UI
│   │   │   ├── ChatMessage.tsx      # Message component
│   │   │   ├── ChatInput.tsx        # Input component
│   │   │   └── DocumentUpload.tsx   # Upload component
│   │   ├── pages/
│   │   │   ├── _app.tsx
│   │   │   ├── _document.tsx
│   │   │   └── index.tsx
│   │   ├── services/
│   │   │   └── api.ts               # API client
│   │   ├── store/
│   │   │   └── chatStore.ts         # State management
│   │   ├── theme/
│   │   │   └── theme.ts             # MUI theme
│   │   └── types/
│   │       └── index.ts             # TypeScript types
│   ├── package.json
│   ├── tsconfig.json
│   └── next.config.js
└── README.md

Setup Instructions

Prerequisites

  • Python 3.10 or higher
  • Node.js 18 or higher
  • OpenAI API key
  • Pinecone account and API key

Backend Setup

  1. Navigate to the backend directory:

    cd backend
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Create .env file from example:

    cp .env.example .env
  5. Configure environment variables in .env:

    OPENAI_API_KEY=your_openai_api_key
    PINECONE_API_KEY=your_pinecone_api_key
    PINECONE_ENVIRONMENT=your_pinecone_environment
    PINECONE_INDEX_NAME=clinical-chatbot
    SECRET_KEY=your_secret_key
  6. Run the backend server:

    python -m app.main

    The API will be available at http://localhost:8000

Frontend Setup

  1. Navigate to the frontend directory:

    cd frontend
  2. Install dependencies:

    npm install
  3. Create .env.local file:

    cp .env.local.example .env.local
  4. Configure environment variables in .env.local:

    NEXT_PUBLIC_API_URL=http://localhost:8000
  5. Run the development server:

    npm run dev

    The application will be available at http://localhost:3000

Running Tests

Backend Tests

cd backend
pytest

Run tests with coverage:

pytest --cov=app --cov-report=html

Frontend Tests

cd frontend
npm test

API Documentation

Once the backend is running, visit:

  • Swagger UI: http://localhost:8000/api/docs
  • ReDoc: http://localhost:8000/api/redoc

Key API Endpoints

Chat Endpoints

  • POST /api/chat/message - Send a chat message
  • GET /api/chat/history/{conversation_id} - Get conversation history
  • DELETE /api/chat/conversation/{conversation_id} - Clear conversation

Document Endpoints

  • POST /api/documents/upload - Upload PDF document
  • POST /api/documents/upload-text - Upload text content
  • GET /api/documents/stats - Get index statistics

Health Endpoints

  • GET /api/health - Health check
  • GET /api/health/ping - Simple connectivity check

Usage Guide

Uploading Documents

  1. Use the document upload interface in the frontend
  2. Select a PDF file containing clinical information
  3. The system will process and index the document
  4. Documents are chunked and embedded for retrieval

Chatting

  1. Type your clinical question in the chat input
  2. Toggle "Use RAG" to enable/disable document retrieval
  3. The AI will respond with evidence-based information
  4. Sources are displayed when RAG is enabled

Conversation Management

  • Conversations are automatically tracked with unique IDs
  • Clear conversations using the trash icon
  • Conversation history is maintained for context

Configuration

Backend Configuration

Key settings in backend/app/config.py:

  • CHUNK_SIZE: Size of document chunks (default: 1000)
  • CHUNK_OVERLAP: Overlap between chunks (default: 200)
  • MAX_CONTEXT_LENGTH: Maximum context for RAG (default: 4000)
  • TEMPERATURE: LLM temperature (default: 0.7)

Frontend Configuration

Key settings in frontend/src/theme/theme.ts:

  • Theme colors and typography
  • Component styling
  • Material-UI customization

Security Considerations

  • Never commit .env files
  • Use strong secret keys in production
  • Implement proper authentication for production deployments
  • Validate all user inputs
  • Rate limit API endpoints
  • Use HTTPS in production

Production Deployment

Backend

  1. Set APP_ENV=production in .env
  2. Use a production-grade WSGI server (e.g., Gunicorn)
  3. Set up proper logging
  4. Configure CORS for your frontend domain

Frontend

  1. Build the production bundle:
    npm run build
  2. Start the production server:
    npm start
  3. Or deploy to Vercel, Netlify, etc.

Troubleshooting

Common Issues

  1. Pinecone connection errors

    • Verify API key and environment
    • Check index name matches configuration
  2. OpenAI API errors

    • Verify API key is valid
    • Check account has sufficient credits
  3. Frontend can't connect to backend

    • Verify NEXT_PUBLIC_API_URL is correct
    • Check CORS settings in backend

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

This project is for educational and research purposes.

Disclaimer

This AI assistant provides clinical information for reference only. Always consult qualified healthcare providers for medical decisions. This system is not a substitute for professional medical advice, diagnosis, or treatment.

Support

For issues or questions, please create an issue in the repository.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published