Mieszko Makuch commited on
Commit
2a7186b
·
1 Parent(s): 2fb8653

Deploy LibreChat to Hugging Face Spaces

Browse files

- Add Dockerfile for LibreChat deployment
- Add environment configuration (secrets removed)
- Add setup documentation
- Configure for external MongoDB Atlas
- Set up for Hugging Face Spaces port 7860

Files changed (5) hide show
  1. .env.production +75 -0
  2. .gitignore +35 -0
  3. Dockerfile +49 -0
  4. README.md +44 -1
  5. SETUP_GUIDE.md +161 -0
.env.production ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment Configuration for LibreChat on Hugging Face Spaces
2
+
3
+
4
+ # Server Configuration
5
+ HOST=0.0.0.0
6
+ PORT=7860
7
+ SERVER_HOST=localhost
8
+
9
+ # MongoDB Configuration (Use MongoDB Atlas)
10
+ # Set MONGO_URI in Hugging Face Secrets
11
+ MONGO_URI=
12
+
13
+ # Redis Configuration (Optional - Use Redis Cloud)
14
+ USE_REDIS=false
15
+ # REDIS_URI=redis://YOUR_REDIS_HOST:6379
16
+ # REDIS_USERNAME=your_redis_username
17
+ # REDIS_PASSWORD=your_redis_password
18
+
19
+ # File Upload Configuration
20
+ UPLOAD_DIR=/data/uploads
21
+ LOGS_DIR=/data/logs
22
+ IMAGES_DIR=/data/images
23
+
24
+ # JWT Secret (Generate a secure random string)
25
+ # Set JWT_SECRET in Hugging Face Secrets
26
+ JWT_SECRET=
27
+
28
+ # Encryption Keys (Generate secure random strings)
29
+ # Set CREDS_KEY and CREDS_IV in Hugging Face Secrets
30
+ CREDS_KEY=
31
+ CREDS_IV=
32
+
33
+ # App Configuration
34
+ APP_TITLE=LibreChat
35
+ ALLOW_REGISTRATION=true
36
+ ALLOW_SOCIAL_LOGIN=false
37
+
38
+ # Search Configuration (Disable Meilisearch for simplicity)
39
+ SEARCH=false
40
+
41
+ # RAG Configuration (Disable for simplicity)
42
+ RAG_API_URL=
43
+
44
+ # Model Configuration (Add your API keys)
45
+ # OpenAI
46
+ OPENAI_API_KEY=
47
+ # OPENAI_MODELS=gpt-3.5-turbo,gpt-4
48
+
49
+ # Anthropic
50
+ ANTHROPIC_API_KEY=
51
+ # ANTHROPIC_MODELS=claude-2,claude-instant
52
+
53
+ # Google
54
+ GOOGLE_KEY=
55
+ # GOOGLE_MODELS=gemini-pro
56
+
57
+ # Azure OpenAI
58
+ # AZURE_API_KEY=
59
+ # AZURE_OPENAI_API_INSTANCE_NAME=
60
+ # AZURE_OPENAI_API_VERSION=
61
+
62
+ # Other providers...
63
+ # Check LibreChat documentation for more providers
64
+
65
+ # Security
66
+ SESSION_EXPIRY=1000 * 60 * 60 * 24 * 7 # 7 days
67
+ REFRESH_TOKEN_EXPIRY=1000 * 60 * 60 * 24 * 7 # 7 days
68
+
69
+ # Logging
70
+ DEBUG_LOGGING=false
71
+ DEBUG_CONSOLE=false
72
+
73
+ # File Service Configuration
74
+ FILE_SIZE_LIMIT=20
75
+ FIREBASE_UPLOAD_SIZE_LIMIT=20
.gitignore ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment files with secrets
2
+ .env
3
+ .env.local
4
+ .env.*.local
5
+
6
+ # Data directories
7
+ data/
8
+ uploads/
9
+ logs/
10
+ images/
11
+
12
+ # OS files
13
+ .DS_Store
14
+ Thumbs.db
15
+
16
+ # IDE files
17
+ .vscode/
18
+ .idea/
19
+ *.swp
20
+ *.swo
21
+
22
+ # Temporary files
23
+ *.tmp
24
+ *.temp
25
+ *.log
26
+
27
+ # Node modules (if testing locally)
28
+ node_modules/
29
+
30
+ # Build artifacts
31
+ dist/
32
+ build/
33
+
34
+ # Local testing files
35
+ docker-compose.local.yml
Dockerfile ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:20-slim
2
+
3
+ # Install dependencies
4
+ RUN apt-get update && apt-get install -y \
5
+ git \
6
+ python3 \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # Create non-root user (Hugging Face requires UID 1000)
11
+ RUN useradd -m -u 1000 -s /bin/bash user
12
+
13
+ # Set working directory
14
+ WORKDIR /app
15
+
16
+ # Clone LibreChat repository
17
+ RUN git clone https://github.com/danny-avila/LibreChat.git . && \
18
+ chown -R user:user /app
19
+
20
+ # Switch to non-root user
21
+ USER user
22
+
23
+ # Install dependencies
24
+ RUN npm ci
25
+
26
+ # Build the application
27
+ RUN npm run build
28
+
29
+ # Create necessary directories with proper permissions
30
+ RUN mkdir -p /app/uploads /app/logs /app/images && \
31
+ chmod 755 /app/uploads /app/logs /app/images
32
+
33
+ # Copy environment configuration
34
+ COPY --chown=user:user .env.production /app/.env
35
+
36
+ # Expose the port (Hugging Face Spaces default)
37
+ EXPOSE 7860
38
+
39
+ # Set environment variables for Hugging Face Spaces
40
+ ENV HOST=0.0.0.0
41
+ ENV PORT=7860
42
+ ENV NODE_ENV=production
43
+
44
+ # Health check
45
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
46
+ CMD node -e "require('http').get('http://localhost:7860/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); })"
47
+
48
+ # Start the application
49
+ CMD ["npm", "start"]
README.md CHANGED
@@ -8,4 +8,47 @@ pinned: false
8
  short_description: librechat docker
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  short_description: librechat docker
9
  ---
10
 
11
+ # LibreChat on Hugging Face Spaces
12
+
13
+ Deploy your own instance of LibreChat on Hugging Face Spaces with external database support.
14
+
15
+ ## Quick Start
16
+
17
+ 1. **Fork/Duplicate this Space**
18
+ 2. **Set up MongoDB Atlas** (free tier)
19
+ - Create account at [MongoDB Atlas](https://www.mongodb.com/cloud/atlas)
20
+ - Create a free M0 cluster
21
+ - Get your connection string
22
+ 3. **Configure Secrets** in Space Settings:
23
+ - `MONGO_URI`: Your MongoDB connection string
24
+ - `JWT_SECRET`: Random 32+ character string
25
+ - `CREDS_KEY`: Random 32 character string
26
+ - `CREDS_IV`: Random 16 character string
27
+ - Add your AI provider API keys (e.g., `OPENAI_API_KEY`)
28
+ 4. **Enable Persistent Storage** (optional but recommended)
29
+
30
+ ## Features
31
+
32
+ - Multi-model AI chat interface (OpenAI, Anthropic, Google, etc.)
33
+ - User authentication and management
34
+ - Conversation history
35
+ - File uploads (with persistent storage)
36
+ - External database support
37
+
38
+ ## Documentation
39
+
40
+ - [Setup Guide](./SETUP_GUIDE.md) - Detailed deployment instructions
41
+ - [LibreChat Docs](https://www.librechat.ai/docs) - Official documentation
42
+ - [Configuration Reference](https://huggingface.co/docs/hub/spaces-config-reference) - Hugging Face Spaces
43
+
44
+ ## Architecture
45
+
46
+ - **Frontend + Backend**: Single Docker container
47
+ - **Database**: MongoDB Atlas (external)
48
+ - **Cache**: Redis Cloud (optional, external)
49
+ - **Storage**: Hugging Face persistent storage for uploads
50
+
51
+ ## Support
52
+
53
+ For issues specific to this Hugging Face deployment, please open an issue in this Space.
54
+ For LibreChat issues, visit the [main repository](https://github.com/danny-avila/LibreChat).
SETUP_GUIDE.md ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LibreChat Deployment on Hugging Face Spaces
2
+
3
+ This guide explains how to deploy LibreChat on Hugging Face Spaces using Docker.
4
+
5
+ ## Architecture Overview
6
+
7
+ - **Single Container**: Frontend and backend run in one Docker container
8
+ - **External Database**: MongoDB Atlas (cloud-hosted)
9
+ - **External Cache** (Optional): Redis Cloud
10
+ - **File Storage**: Uses Hugging Face Spaces `/data` directory (requires persistent storage upgrade)
11
+
12
+ ## Prerequisites
13
+
14
+ 1. **MongoDB Atlas Account** (Free tier available)
15
+ - Sign up at: https://www.mongodb.com/cloud/atlas
16
+
17
+ 2. **Redis Cloud Account** (Optional, free tier available)
18
+ - Sign up at: https://redis.com/try-free/
19
+
20
+ 3. **AI Model API Keys** (At least one)
21
+ - OpenAI: https://platform.openai.com/
22
+ - Anthropic: https://console.anthropic.com/
23
+ - Google: https://makersuite.google.com/app/apikey
24
+ - Others as needed
25
+
26
+ ## Setup Steps
27
+
28
+ ### 1. Set up MongoDB Atlas
29
+
30
+ 1. Create a new project in MongoDB Atlas
31
+ 2. Create a free cluster (M0 Sandbox)
32
+ 3. Create a database user with password
33
+ 4. Add IP whitelist: `0.0.0.0/0` (allows access from anywhere)
34
+ 5. Get your connection string from "Connect" button
35
+ - Format: `mongodb+srv://username:password@cluster.mongodb.net/librechat?retryWrites=true`
36
+
37
+ ### 2. Configure Environment Variables
38
+
39
+ Edit `.env.production` file:
40
+
41
+ ```bash
42
+ # Required: MongoDB connection
43
+ MONGO_URI=mongodb+srv://YOUR_USERNAME:YOUR_PASSWORD@YOUR_CLUSTER.mongodb.net/librechat?retryWrites=true
44
+
45
+ # Required: Security keys (generate random strings)
46
+ JWT_SECRET=<generate-32-char-random-string>
47
+ CREDS_KEY=<generate-32-char-random-string>
48
+ CREDS_IV=<generate-16-char-random-string>
49
+
50
+ # Required: At least one AI provider API key
51
+ OPENAI_API_KEY=sk-...
52
+ # or
53
+ ANTHROPIC_API_KEY=sk-ant-...
54
+ # or other providers
55
+ ```
56
+
57
+ ### 3. Deploy to Hugging Face Spaces
58
+
59
+ 1. Create a new Space on Hugging Face
60
+ 2. Choose "Docker" as the SDK
61
+ 3. Upload these files:
62
+ - `Dockerfile`
63
+ - `.env.production`
64
+ - `README.md` (already configured)
65
+
66
+ 4. Set up Secrets in Space Settings:
67
+ - Add sensitive environment variables as secrets
68
+ - They'll override values in `.env.production`
69
+
70
+ ### 4. Enable Persistent Storage (Recommended)
71
+
72
+ In your Space settings:
73
+ - Enable "Persistent storage" upgrade
74
+ - This ensures uploaded files and logs persist across restarts
75
+
76
+ ## Configuration Options
77
+
78
+ ### Basic Setup (Minimum Requirements)
79
+ - MongoDB Atlas (external)
80
+ - One AI provider API key
81
+ - JWT and encryption keys
82
+
83
+ ### Advanced Setup (Optional)
84
+ - Redis Cloud for caching
85
+ - Multiple AI providers
86
+ - Custom branding
87
+ - Authentication providers
88
+
89
+ ### Disabled Features (for simplicity)
90
+ - Meilisearch (search functionality)
91
+ - RAG API (vector database features)
92
+ - Local MongoDB/Redis instances
93
+
94
+ ## Environment Variables Reference
95
+
96
+ Key variables to configure:
97
+
98
+ ```bash
99
+ # Database
100
+ MONGO_URI= # MongoDB Atlas connection string
101
+
102
+ # Caching (optional)
103
+ USE_REDIS=false # Set to true if using Redis
104
+ REDIS_URI= # Redis Cloud connection string
105
+
106
+ # Security
107
+ JWT_SECRET= # 32+ character random string
108
+ CREDS_KEY= # 32 character string
109
+ CREDS_IV= # 16 character string
110
+
111
+ # AI Providers (configure as needed)
112
+ OPENAI_API_KEY=
113
+ ANTHROPIC_API_KEY=
114
+ GOOGLE_KEY=
115
+ # ... other providers
116
+
117
+ # App Settings
118
+ ALLOW_REGISTRATION=true # Allow new user signups
119
+ APP_TITLE=LibreChat # Custom app title
120
+ ```
121
+
122
+ ## Limitations
123
+
124
+ 1. **No Built-in Databases**: Hugging Face Spaces isn't suitable for running MongoDB/Redis
125
+ 2. **Persistent Storage**: Requires paid upgrade for file persistence
126
+ 3. **Single Container**: Can't use docker-compose multi-service setup
127
+ 4. **Port Restrictions**: Must use port 7860 (Hugging Face default)
128
+
129
+ ## Troubleshooting
130
+
131
+ ### Container Won't Start
132
+ - Check logs in Hugging Face Spaces
133
+ - Verify MongoDB connection string
134
+ - Ensure all required environment variables are set
135
+
136
+ ### Can't Connect to MongoDB
137
+ - Verify IP whitelist includes `0.0.0.0/0`
138
+ - Check username/password are correct
139
+ - Ensure connection string format is correct
140
+
141
+ ### Files Don't Persist
142
+ - Enable persistent storage in Space settings
143
+ - Verify upload directories use `/data` prefix
144
+
145
+ ## Security Considerations
146
+
147
+ 1. **Use Secrets**: Store sensitive data in Hugging Face Secrets, not in files
148
+ 2. **MongoDB Security**: In production, restrict IP whitelist to specific IPs
149
+ 3. **Strong Keys**: Generate cryptographically secure random strings for JWT/encryption
150
+ 4. **HTTPS**: Hugging Face Spaces provides HTTPS by default
151
+
152
+ ## Next Steps
153
+
154
+ After deployment:
155
+ 1. Access your Space URL
156
+ 2. Create an admin account (first user)
157
+ 3. Configure additional settings in the UI
158
+ 4. Test AI model integrations
159
+
160
+ For advanced configuration, refer to LibreChat documentation:
161
+ https://www.librechat.ai/docs