-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.js
46 lines (40 loc) · 1.73 KB
/
cloud.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
// index.js
exports.yourCloudFunction = async (req, res) => {
require("dotenv").config(); // Ensure your environment variables are loaded
const OpenAI = require("openai");
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
try {
// Assuming the user's message is sent in the request body as 'userMessage'
const userMessage = req.body.userMessage;
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo-16k",
messages: [
{
role: "system",
content:
'You are a Chrome Extension that generates privacy warnings in different categories by analyzing an EULA. JSON Response Example:\n\n{\n "warnings": [\n {\n "warning_code": 1,\n "category": "Location",\n "description": "This application tracks location."\n },\n {\n "warning_code": 2,\n "category": "Personal Data Access",\n "description": "Will have access to personal data - Image, SMS etc."\n }\n ]\n}',
},
{
role: "user",
content: userMessage,
},
],
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
// Extracting generated warnings from the response
const generatedWarnings = response.choices.map(
(choice) => choice.message.content
);
res.status(200).send(response.choices[0].message.content);
// console.log(response);
// console.log(response.choices[0].message.content);
} catch (error) {
res.status(500).send("Error generating warnings: " + error); // Set an appropriate HTTP status code for errors, such as 500 Internal Server Error
}
};