-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
78 lines (68 loc) · 2.83 KB
/
test.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
72
73
74
75
76
77
78
const {
FunctionDeclarationSchemaType,
HarmBlockThreshold,
HarmCategory,
VertexAI
} = require('@google-cloud/vertexai');
const project = 'hooli-8a670';
const location = 'us-central1';
const textModel = 'gemini-1.0-pro';
const visionModel = 'gemini-1.0-pro-vision';
const vertexAI = new VertexAI({project: project, location: location});
// Instantiate Gemini models
const generativeModel = vertexAI.getGenerativeModel({
model: textModel,
// The following parameters are optional
// They can also be passed to individual content generation requests
safetySettings: [{category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE}],
generationConfig: {maxOutputTokens: 10},
});
const generativeVisionModel = vertexAI.getGenerativeModel({
model: visionModel,
});
const generativeModelPreview = vertexAI.preview.getGenerativeModel({
model: textModel,
});
const generateContent = async (text) => {
const request = {
contents: [
{
role: 'system',
parts: [
{
text: `
You are an agent of an app that receives texts explaining the daily activities of a person (like diary entries),
your job is to get the activities that have something to do with being eco-friendly (and give
wether they are good for the environment or bad) and provide and answer in JSON format following
the template below containing the activities with their caractheristics and a message either giving
kudos or encouraging improvement and future challenges the user can do to improve and be more eco-friendly:
{
"activities": [
{
"activity": "activity name",
"activity_description": "activity description",
"feedback_message": "feedback message",
"eco_friendly": true/false,
}
],
"future_challenges": [
{
"challenge": "challenge name",
"challenge_description": "challenge description",
"challenge_dificulty": "0/1/2",
}
],
}`
}
]
},
{role: 'user', parts: [{text: text}]}
],
};
const result = await generativeModel.generateContent(request);
const response = result.response;
console.log('Response: ', JSON.stringify(response));
console.log('Response: ', response.candidates[0].content.parts[0].text);
return JSON.stringify(response.candidates[0].content.parts[0].text);
};
module.exports = { generateContent };