forked from ym-empat/multimodal-rag-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (63 loc) · 2.43 KB
/
app.js
File metadata and controls
86 lines (63 loc) · 2.43 KB
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
const express = require('express');
const dotenv = require('dotenv');
const multer = require('multer');
const { embedText, embedImage, embedVideo } = require('./vertex');
const { upsertToIndex, search } = require('./pinecone');
// Load environment variables
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
// Multer setup (store files in memory)
const upload = multer({ storage: multer.memoryStorage() });
app.use(express.json());
// Root route
app.get('/', (req, res) => {
res.status(200).json({ message: 'OK' });
});
app.post('/upload', upload.single('content'), async (req, res) => {
const file = req.file;
const text = req.body.content;
const filename = file?.originalname || 'text_input';
try {
if(file) {
const mime = file.mimetype;
if (mime.startsWith('image/')) {
console.log('embedding image');
const vector = await embedImage(file);
await upsertToIndex({ vectors: [vector], type: 'image', filename });
} else if (mime.startsWith('video/')) {
console.log('embedding video');
const vectors = await embedVideo(file);
await upsertToIndex({ vectors, type: 'video', filename });
} else {
return res.status(400).json({ error: 'Unsupported file type' });
}
} else if (text) {
console.log('text');
const vector = await embedText(text);
await upsertToIndex({ vectors: [vector], type: 'text', filename: 'text_input' });
}
res.status(200).json({ message: 'OK' });
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post('/search/text', async (req, res) => {
const { query } = req.body;
if (!query || typeof query !== 'string') {
return res.status(400).json({ error: 'Invalid or missing "query" field in JSON body' });
}
try {
const vector = await embedText(query);
const results = await search(vector, 3);
res.status(200).json({ results });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Search failed' });
}
});
// Start server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});