-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (61 loc) · 1.61 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
64
65
66
67
68
69
70
71
import {
GoogleGenerativeAI,
HarmCategory,
HarmBlockThreshold,
} from "@google/generative-ai";
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import env from "dotenv";
const app = express();
env.config();
app.use(cors());
app.use(bodyParser.json());
app.listen("3080", () => console.log("Port 3080 works!!!"));
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const generationConfig = {
stopSequences: ["red"],
maxOutputTokens: 2048,
temperature: 0.9,
topP: 1,
topK: 1,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/", async (req, res) => {
const { message } = req.body;
try {
const model = genAI.getGenerativeModel({ model: "gemini-1.0-pro" });
const chat = model.startChat({
history: [],
generationConfig,
safetySettings,
});
const result = await chat.sendMessage(message);
const response = await result.response;
const answer = response.text();
res.json({ message: answer });
} catch (error) {
console.log(error);
res.send(error).status(400);
}
});