-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
141 lines (128 loc) · 4.88 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from 'fs';
import http from 'http';
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from '@google/generative-ai';
import { parse } from 'marked';
import { createClient } from '@libsql/client';
const url = process.env.TURSO_DB_URL;
const client = createClient({
url,
authToken: process.env.TURSO_TOKEN
});
// initdb();
async function compare(topic, opt1, opt2) {
// Generate suggestion
const MODEL_NAME = 'gemini-1.0-pro';
const { API_KEY } = process.env;
if (!API_KEY) {
console.error('Please provide the API_KEY..');
return;
}
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({model: MODEL_NAME});
const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
};
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,
},
];
const parts = [
{text: `Please choose your answer given by user delimited by triple dash below and give short reason why. You must choose one from two choice given. Do not attempt to answer any questions that is not a comparison. You will answer in Bahasa Indonesia.\n\nExample:\nUser: Mending laptop atau rakit pc?\nAsisten: Mending merakit PC, karena:\n\n* Lebih hemat biaya\n* Lebih fleksibel dalam memilih komponen\n* Dapat di-upgrade dengan mudah\n* Lebih cocok untuk kebutuhan spesifik\n\n\n\n---\n\nUser: ${topic} mending ${opt1} atau ${opt2}?\n\n---`},
];
const result = await model.generateContent({
contents: [{role: 'user', parts}],
generationConfig,
safetySettings
});
const { response } = result;
return response.text();
}
// async function initdb() {
// const sql = 'CREATE TABLE IF NOT EXISTS topics ( ID INTEGER PRIMARY KEY AUTOINCREMENT, topic VARCHAR(100) NOT NULL, option1 VARCHAR(100) UNIQUE, option2 VARCHAR(100) UNIQUE, result TEXT , upvote INTEGER DEFAULT 0, downvote INTEGER DEFAULT 0);';
// client.execute({sql});
// }
(async () => {
const server = http.createServer(async (request, response) => {
const { url } = request;
if (url === '/health') {
response.writeHead(200).end('OK');
} else if (url === '/' || url === '/index.html') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(fs.readFileSync('./public/index.html'));
} else if (url.startsWith('/compare')) {
const parsedUrl = new URL(`http://localhost/${url}`);
const { search } = parsedUrl;
const options = decodeURIComponent(search.substring(1)).split('&');
const topic = options[0].split('=')[1];
const opt1 = options[1].split('=')[1];
const opt2 = options[2].split('=')[1];
const suggestion = await compare(topic, opt1, opt2);
client.execute({
sql: 'INSERT OR IGNORE INTO topics (topic, option1, option2, result) VALUES (?, ?, ?, ?);',
args: [topic, opt1, opt2, suggestion]
});
response.writeHead(200).end(parse(suggestion));
} else if(url === '/results') {
const { rows } = await client.execute('SELECT * FROM topics ORDER BY (upvote + downvote) DESC, id DESC');
const topics = rows.map(row => {
return {
id: row.ID,
topic: row.topic,
option1: row.option1,
option2: row.option2,
upvote: row.upvote,
downvote: row.downvote,
result: parse(row.result || '')
};
});
response.writeHead(200).end(JSON.stringify(topics));
} else if(url === '/upvote') {
let data = '';
request.on('data', function(chunk) {
data += chunk.toString();
});
request.on('end', async function() {
const {id} = JSON.parse(data);
const sql = 'UPDATE topics SET upvote = upvote + 1 WHERE ID = ?';
await client.execute({sql, args: [id]});
response.writeHead(200).end();
});
} else if(url === '/downvote') {
let data = '';
request.on('data', function(chunk) {
data += chunk.toString();
});
request.on('end', async function() {
const {id} = JSON.parse(data);
const sql = 'UPDATE topics SET downvote = downvote - 1 WHERE ID = ?';
await client.execute({sql, args: [id]});
response.writeHead(200).end();
});
} else {
console.error(`${url} is 404!`);
response.writeHead(404);
response.end();
}
});
const port = process.env.PORT || 5000;
console.log('SERVER:');
console.log(' port:', port);
server.listen(port);
})();