|
| 1 | +# Authenticated Chat Example |
| 2 | + |
| 3 | +This example demonstrates how to create a chat interface with user authentication using Ragbits Chat. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🔐 **User Authentication**: Login/logout with username/password |
| 8 | +- 👤 **Role-based Access**: Different user roles (admin, moderator, user) with specific capabilities |
| 9 | +- 🛡️ **Secure Sessions**: Session-based authentication with Bearer tokens |
| 10 | +- 📊 **Personalized Responses**: User-specific chat responses based on profile and roles |
| 11 | +- 🔄 **Live Updates**: Role-specific live updates during processing |
| 12 | +- 📚 **User Context**: Reference documents with user profile information |
| 13 | +- 🎨 **UI Customization**: Custom welcome messages, headers, and branding |
| 14 | +- 📝 **Feedback System**: Like/dislike forms with custom Pydantic models |
| 15 | +- ⚙️ **User Settings**: Configurable user preferences (e.g., language selection) |
| 16 | + |
| 17 | +## Files |
| 18 | + |
| 19 | +- `authenticated_chat.py` - Main authenticated chat implementation |
| 20 | +- `README_authenticated.md` - This documentation |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +### 1. Start the Server |
| 25 | + |
| 26 | +#### Full Authentication Support (Recommended) |
| 27 | +```bash |
| 28 | +# From the examples/chat directory - includes login/logout endpoints |
| 29 | +uv run python authenticated_chat.py |
| 30 | +``` |
| 31 | + |
| 32 | +#### Alternative Methods |
| 33 | +```bash |
| 34 | +# From project root |
| 35 | +python examples/chat/authenticated_chat.py |
| 36 | + |
| 37 | +# Via CLI with authentication support |
| 38 | +uv run ragbits api run examples.chat.authenticated_chat:MyAuthenticatedChat --auth examples.chat.authenticated_chat:get_auth_backend |
| 39 | +``` |
| 40 | + |
| 41 | +The server will start at `http://127.0.0.1:8000` with a web interface. |
| 42 | + |
| 43 | +**Note**: When using the CLI, include the `--auth` flag with the authentication backend factory function to enable full authentication features including login/logout endpoints. |
| 44 | + |
| 45 | +### 2. Test Users |
| 46 | + |
| 47 | +The example includes these test users: |
| 48 | + |
| 49 | +| Username | Password | Roles | Description | |
| 50 | +|-----------|-----------|--------------------------|---------------------| |
| 51 | +| `admin` | `admin123` | admin, moderator, user | System administrator | |
| 52 | +| `moderator` | `mod123` | moderator, user | Community moderator | |
| 53 | +| `alice` | `alice123` | user | Regular user | |
| 54 | +| `bob` | `bob123` | user | Regular user | |
| 55 | + |
| 56 | +### 3. Authentication Workflow |
| 57 | + |
| 58 | +#### Web Interface |
| 59 | +1. Open `http://127.0.0.1:8000` in your browser |
| 60 | +2. Use the login form with any test user credentials |
| 61 | +3. Start chatting after successful authentication |
| 62 | + |
| 63 | +#### API Endpoints |
| 64 | +1. **Login**: `POST /api/auth/login` |
| 65 | + ```json |
| 66 | + { |
| 67 | + "username": "admin", |
| 68 | + "password": "admin123" |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +2. **Chat**: `POST /api/chat` (with Bearer token) |
| 73 | + ```bash |
| 74 | + Authorization: Bearer <session_id> |
| 75 | + ``` |
| 76 | + |
| 77 | +3. **Logout**: `POST /api/auth/logout` |
| 78 | + ```json |
| 79 | + { |
| 80 | + "session_id": "<session_id>" |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +## Testing |
| 85 | + |
| 86 | +You can test the authentication functionality using: |
| 87 | + |
| 88 | +### Web Interface Testing |
| 89 | +1. Start the server: `python examples/chat/authenticated_chat.py` |
| 90 | +2. Open `http://127.0.0.1:8000` in your browser |
| 91 | +3. Use the login form with any of the test user credentials |
| 92 | +4. Test different roles and their specific features |
| 93 | + |
| 94 | +### API Testing with curl |
| 95 | +```bash |
| 96 | +# Login |
| 97 | +curl -X POST http://127.0.0.1:8000/api/auth/login \ |
| 98 | + -H "Content-Type: application/json" \ |
| 99 | + -d '{"username": "admin", "password": "admin123"}' |
| 100 | + |
| 101 | +# Chat (replace <session_id> with the session_id from login response) |
| 102 | +curl -X POST http://127.0.0.1:8000/api/chat \ |
| 103 | + -H "Content-Type: application/json" \ |
| 104 | + -H "Authorization: Bearer <session_id>" \ |
| 105 | + -d '{"message": "Hello!"}' |
| 106 | + |
| 107 | +# Logout |
| 108 | +curl -X POST http://127.0.0.1:8000/api/auth/logout \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + -d '{"session_id": "<session_id>"}' |
| 111 | +``` |
| 112 | + |
| 113 | +## Example Chat Interactions |
| 114 | + |
| 115 | +### As Admin User |
| 116 | +``` |
| 117 | +💬 You: What admin features are available? |
| 118 | +🤖 Bot: As an administrator, you have full system access including: |
| 119 | +- User management and permissions |
| 120 | +- System configuration and monitoring |
| 121 | +- Content moderation capabilities |
| 122 | +- Administrative dashboards and reports |
| 123 | +``` |
| 124 | + |
| 125 | +### As Regular User |
| 126 | +``` |
| 127 | +💬 You: Tell me about my profile |
| 128 | +🤖 Bot: Hello Alice Johnson! You're logged in as 'alice' with user role. |
| 129 | +Your profile shows you're part of the Marketing department. |
| 130 | +``` |
| 131 | + |
| 132 | +## Architecture |
| 133 | +### ListAuthBackend |
| 134 | +- In-memory user storage with hashed passwords |
| 135 | +- Session management with expiration |
| 136 | +- User roles and metadata support |
| 137 | +- Suitable for development and small deployments |
| 138 | + |
| 139 | +### Role-based Features |
| 140 | +- **Admin**: Full system access, admin-specific live updates, special admin profile images |
| 141 | +- **Moderator**: Content moderation features with policy checks and content guidelines |
| 142 | +- **User**: Standard chat features with personalized responses and user-specific context |
| 143 | + |
| 144 | +## Customization |
| 145 | + |
| 146 | +### Adding New Users |
| 147 | +Edit the `get_auth_backend()` factory function in `authenticated_chat.py`: |
| 148 | + |
| 149 | +```python |
| 150 | +users = [ |
| 151 | + { |
| 152 | + "username": "newuser", |
| 153 | + "password": "newpass123", |
| 154 | + |
| 155 | + "full_name": "New User", |
| 156 | + "roles": ["user"], |
| 157 | + "metadata": {"department": "Engineering"} |
| 158 | + } |
| 159 | +] |
| 160 | +``` |
| 161 | + |
| 162 | + |
| 163 | +### Role-specific Responses |
| 164 | +Modify the `chat()` method to customize responses based on user roles: |
| 165 | + |
| 166 | +```python |
| 167 | +# Get user info from context |
| 168 | +user_info = context.state.get("authenticated_user") if context else None |
| 169 | +user_roles = user_info.roles if user_info else [] |
| 170 | + |
| 171 | +if "admin" in user_roles: |
| 172 | + yield self.create_text_response("🔧 Admin-specific content...") |
| 173 | +elif "moderator" in user_roles: |
| 174 | + yield self.create_text_response("🛡️ Moderator-specific content...") |
| 175 | +``` |
| 176 | + |
| 177 | +### UI Customization |
| 178 | + |
| 179 | +The example demonstrates UI customization features: |
| 180 | + |
| 181 | +```python |
| 182 | +from ragbits.chat.interface.ui_customization import HeaderCustomization, UICustomization |
| 183 | + |
| 184 | +ui_customization = UICustomization( |
| 185 | + header=HeaderCustomization( |
| 186 | + title="🔐 Authenticated Ragbits Chat", |
| 187 | + subtitle="by deepsense.ai - Secure Chat Experience", |
| 188 | + logo="🛡️" |
| 189 | + ), |
| 190 | + welcome_message="🔐 **Welcome to Authenticated Ragbits Chat!**\n\n..." |
| 191 | +) |
| 192 | +``` |
| 193 | + |
| 194 | +### Feedback Configuration |
| 195 | + |
| 196 | +Custom feedback forms using Pydantic models: |
| 197 | + |
| 198 | +```python |
| 199 | +from ragbits.chat.interface.forms import FeedbackConfig |
| 200 | + |
| 201 | +feedback_config = FeedbackConfig( |
| 202 | + like_enabled=True, |
| 203 | + like_form=LikeFormExample, # Custom Pydantic model |
| 204 | + dislike_enabled=True, |
| 205 | + dislike_form=DislikeFormExample, # Custom Pydantic model |
| 206 | +) |
| 207 | +``` |
| 208 | + |
| 209 | +## Security Notes |
| 210 | + |
| 211 | +- Passwords are hashed using bcrypt in `ListAuthBackend` |
| 212 | +- Sessions have configurable expiration times |
| 213 | +- Bearer tokens are used for API authentication |
| 214 | +- CORS is configured for web interface access |
| 215 | +- State signatures prevent tampering with conversation state |
0 commit comments