-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (61 loc) · 2.53 KB
/
index.js
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
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8888;
const { GoogleGenerativeAI } = require("@google/generative-ai");
app.post('/api/text', async (req, res) => {
if (req.method === 'POST' && req.headers['content-type'] === 'application/json') {
let data = '';
req.on('data', chunk => {
data += chunk;
});
req.on('end', () => {
try {
const jsonData = JSON.parse(data);
const apiKey = jsonData.apiKey;
const textPrompt = jsonData.textPrompt;
if (apiKey && textPrompt) {
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-pro", "safetySettings": [
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE"
}
]
});
const result = model.generateContent(textPrompt).then(result => {
return result.response.text();
}).then(text => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: text }));
})
}
else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid JSON data');
}
} catch (error) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid JSON data');
}
});
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Unsupported method or content type');
}
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});