File size: 7,885 Bytes
91c95f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# Express.js Integration Guide
## Quick Setup for Express Applications
### 1. Environment Variables
Add to your `.env` file or environment secrets:
```bash
# Hugging Face Spaces URL (use hyphens, not underscores)
HF_SPACE_URL=https://jatinautonomouslabs-research-ai-assistant-api.hf.space
# Alternative if hyphens don't work
# HF_SPACE_URL=https://jatinautonomouslabs-research_ai_assistant_api.hf.space
```
### 2. Express Code Example
```javascript
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
// Get HF Space URL from environment
const HF_SPACE_URL = process.env.HF_SPACE_URL ||
'https://jatinautonomouslabs-research-ai-assistant-api.hf.space';
// Health check function
async function checkHFHealth() {
try {
const response = await axios.get(`${HF_SPACE_URL}/api/health`, {
timeout: 5000
});
return {
healthy: true,
status: response.data.status,
orchestratorReady: response.data.orchestrator_ready
};
} catch (error) {
console.error('HF Space health check failed:', error.message);
return {
healthy: false,
error: error.message
};
}
}
// Initialize HF Space connection
async function initializeHFConnection() {
console.log('[Init] Using HuggingFace Spaces for AI backend...');
console.log('[Init] Checking Flask API connection...');
console.log(`[HFSpaces] Flask API: ${HF_SPACE_URL}`);
const health = await checkHFHealth();
if (health.healthy && health.orchestratorReady) {
console.log('[Init] ✅ Flask API healthy and ready');
return true;
} else {
console.log('[Init] ✗ Flask API unhealthy:', health.error || 'Orchestrator not ready');
console.log('[Init] Make sure your HF Space is running');
console.log(`[Init] URL: ${HF_SPACE_URL}`);
return false;
}
}
// Chat endpoint that proxies to HF Space
app.post('/api/chat', async (req, res) => {
try {
const { message, session_id, user_id, history } = req.body;
// Validate input
if (!message || typeof message !== 'string' || message.trim().length === 0) {
return res.status(400).json({
success: false,
error: 'Message is required and must be a non-empty string'
});
}
// Filter out CSS/HTML content (basic check)
if (message.includes('{') && message.includes('}') && message.includes('color:')) {
console.warn('[Chat] Detected potential CSS/HTML content, filtering...');
return res.status(400).json({
success: false,
error: 'Please send text messages only, not code or CSS'
});
}
// Call HF Space API
const response = await axios.post(
`${HF_SPACE_URL}/api/chat`,
{
message: message.trim(),
session_id: session_id,
user_id: user_id || 'anonymous',
history: history || []
},
{
timeout: 30000, // 30 second timeout
headers: {
'Content-Type': 'application/json'
}
}
);
res.json(response.data);
} catch (error) {
console.error('[Chat] Error:', error.message);
if (error.response) {
// HF Space returned an error
res.status(error.response.status).json({
success: false,
error: error.response.data.error || error.response.data.message || 'HF Space API error',
details: error.response.data
});
} else if (error.code === 'ECONNREFUSED' || error.code === 'ETIMEDOUT') {
// Connection error
res.status(503).json({
success: false,
error: 'HF Space is not reachable. Please check if the space is running.',
url: HF_SPACE_URL
});
} else {
// Other errors
res.status(500).json({
success: false,
error: 'Internal server error',
message: error.message
});
}
}
});
// Start server
const PORT = process.env.PORT || 5000;
async function startServer() {
// Check HF Space connection on startup
const hfReady = await initializeHFConnection();
if (!hfReady) {
console.warn('[Init] ⚠ HF Space not reachable');
console.warn('[Init] Check HF_SPACE_URL in Secrets');
console.warn('[Init] Make sure your HF Space is running');
}
app.listen(PORT, () => {
console.log(`[express] serving on port ${PORT}`);
});
}
startServer();
```
### 3. Testing the Connection
```javascript
// Test script
const axios = require('axios');
async function testConnection() {
const HF_SPACE_URL = process.env.HF_SPACE_URL ||
'https://jatinautonomouslabs-research-ai-assistant-api.hf.space';
console.log('Testing HF Space connection...');
console.log(`URL: ${HF_SPACE_URL}`);
// Test 1: Root endpoint
try {
const root = await axios.get(`${HF_SPACE_URL}/`, { timeout: 5000 });
console.log('✅ Root endpoint:', root.status, root.data);
} catch (e) {
console.log('❌ Root endpoint failed:', e.message);
}
// Test 2: Health endpoint
try {
const health = await axios.get(`${HF_SPACE_URL}/api/health`, { timeout: 5000 });
console.log('✅ Health endpoint:', health.status, health.data);
} catch (e) {
console.log('❌ Health endpoint failed:', e.message);
}
// Test 3: Chat endpoint
try {
const chat = await axios.post(
`${HF_SPACE_URL}/api/chat`,
{ message: 'Hello, test message' },
{ timeout: 10000 }
);
console.log('✅ Chat endpoint:', chat.status, chat.data.message);
} catch (e) {
console.log('❌ Chat endpoint failed:', e.message);
}
}
testConnection();
```
### 4. Common Issues and Solutions
#### Issue: 404 Not Found
**Causes:**
- Space is still building (wait 5-15 minutes)
- Wrong URL format (use hyphens, not underscores)
- Space is not public
**Solutions:**
1. Check Space status: https://huggingface.co/spaces/JatinAutonomousLabs/Research_AI_Assistant_API
2. Verify URL format: Use `-` (hyphens) not `_` (underscores)
3. Test both URL formats:
```bash
# With hyphens (recommended)
curl https://jatinautonomouslabs-research-ai-assistant-api.hf.space/
# With underscores (if hyphens don't work)
curl https://jatinautonomouslabs-research_ai_assistant_api.hf.space/
```
#### Issue: 503 Service Unavailable
**Cause:** Space is running but API is initializing
**Solution:** Wait 30-60 seconds and retry
#### Issue: CSS/HTML Content in Messages
**Solution:** Add input validation in your Express app:
```javascript
function isValidMessage(message) {
// Check for CSS patterns
if (message.includes('{') && message.includes('}') &&
(message.includes('color:') || message.includes('background:'))) {
return false;
}
// Check for HTML tags
if (/<[^>]+>/.test(message)) {
return false;
}
return true;
}
// Use in your endpoint
if (!isValidMessage(message)) {
return res.status(400).json({
success: false,
error: 'Please send text messages only, not code or markup'
});
}
```
### 5. Environment Variables Checklist
Make sure these are set in your Express app's environment:
```bash
# Required
HF_SPACE_URL=https://jatinautonomouslabs-research-ai-assistant-api.hf.space
# Optional
PORT=5000
NODE_ENV=production
```
### 6. Monitoring and Logging
Add logging to track HF Space connectivity:
```javascript
// Periodic health check
setInterval(async () => {
const health = await checkHFHealth();
if (!health.healthy) {
console.warn('[Monitor] HF Space is down:', health.error);
}
}, 60000); // Check every minute
```
---
## Quick Reference
**Correct URL Format:**
```
https://jatinautonomouslabs-research-ai-assistant-api.hf.space
```
**Endpoints:**
- `GET /` - API information
- `GET /api/health` - Health check
- `POST /api/chat` - Chat endpoint
**Test Command:**
```bash
curl https://jatinautonomouslabs-research-ai-assistant-api.hf.space/api/health
```
|