Update serve.js
Browse files
serve.js
CHANGED
|
@@ -1,61 +1,31 @@
|
|
| 1 |
-
// server.js
|
| 2 |
-
import { CIQClient } from '@ninebit/ciq';
|
| 3 |
import express from 'express';
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import multer from 'multer';
|
| 7 |
-
import fs from 'fs';
|
| 8 |
-
|
| 9 |
-
// Load env vars from .env or Hugging Face secrets
|
| 10 |
-
dotenv.config();
|
| 11 |
|
| 12 |
const app = express();
|
| 13 |
-
const port =
|
| 14 |
-
const apiKey = '99edded2-a45d-456e-bf49-af2c9b1ccd2f'; // π For testing only
|
| 15 |
-
const client = new CIQClient(apiKey);
|
| 16 |
|
| 17 |
-
//
|
| 18 |
-
|
| 19 |
-
app.use(express.urlencoded({ extended: true }));
|
| 20 |
|
| 21 |
-
|
| 22 |
-
const uploadDir = 'uploads';
|
| 23 |
-
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir);
|
| 24 |
-
const storage = multer.diskStorage({
|
| 25 |
-
destination: (req, file, cb) => cb(null, uploadDir),
|
| 26 |
-
filename: (req, file, cb) => cb(null, file.originalname)
|
| 27 |
-
});
|
| 28 |
-
const upload = multer({ storage });
|
| 29 |
|
| 30 |
-
/
|
| 31 |
-
|
| 32 |
-
try {
|
| 33 |
-
const filePath = req.file.path;
|
| 34 |
-
const result = await client.ingestFile(filePath);
|
| 35 |
-
res.json({ success: true, result });
|
| 36 |
-
} catch (err) {
|
| 37 |
-
console.error(err);
|
| 38 |
-
res.status(500).json({ error: 'Ingestion failed' });
|
| 39 |
-
}
|
| 40 |
});
|
| 41 |
|
| 42 |
-
// Query endpoint
|
| 43 |
app.post('/query', async (req, res) => {
|
|
|
|
|
|
|
| 44 |
try {
|
| 45 |
-
|
| 46 |
const result = await client.ragQuery(query);
|
| 47 |
res.json({ result });
|
| 48 |
-
} catch (
|
| 49 |
-
|
| 50 |
-
res.status(500).json({ error: 'Query failed' });
|
| 51 |
}
|
| 52 |
});
|
| 53 |
|
| 54 |
-
// Health check
|
| 55 |
-
app.get('/', (req, res) => {
|
| 56 |
-
res.send('π CIQ Chatbot backend is running!');
|
| 57 |
-
});
|
| 58 |
-
|
| 59 |
app.listen(port, () => {
|
| 60 |
-
console.log(`
|
| 61 |
});
|
|
|
|
|
|
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import { CIQClient } from '@ninebit/ciq';
|
| 3 |
+
import bodyParser from 'body-parser';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
+
const port = 7860;
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
const apiKey = '99edded2-a45d-456e-bf49-af2c9b1ccd2f'; // or use process.env.API_KEY
|
| 9 |
+
const client = new CIQClient(apiKey);
|
|
|
|
| 10 |
|
| 11 |
+
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
app.get('/', (req, res) => {
|
| 14 |
+
res.send('π CIQ Chatbot backend is running!');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
});
|
| 16 |
|
|
|
|
| 17 |
app.post('/query', async (req, res) => {
|
| 18 |
+
const { filePath, query } = req.body;
|
| 19 |
+
|
| 20 |
try {
|
| 21 |
+
if (filePath) await client.ingestFile(filePath);
|
| 22 |
const result = await client.ragQuery(query);
|
| 23 |
res.json({ result });
|
| 24 |
+
} catch (error) {
|
| 25 |
+
res.status(500).json({ error: error.message });
|
|
|
|
| 26 |
}
|
| 27 |
});
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
app.listen(port, () => {
|
| 30 |
+
console.log(`Server running on http://localhost:${port}`);
|
| 31 |
});
|