-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (52 loc) · 1.9 KB
/
Copy pathserver.js
File metadata and controls
64 lines (52 loc) · 1.9 KB
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
var express = require('express');
var app = express();
var request = require('request');
var { OpenAIApi, Configuration } = require('openai');
let config = new Configuration({
apiKey: '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',
});
let openai = new OpenAIApi(config);
app.get('/', function(req,res){
res.sendFile(__dirname + '/index.html')
})
var client_id = 'w9j2vlusmqW5BjFCF2GJ';
var client_secret = '_E09sQYgbC';
app.get('/translate', function (req, res) {
var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
var query = req.query.q;
var options = {
url: api_url,
form: {'source':'ko', 'target':'en', 'text':query},
headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
};
request.post(options, function (error, response, body) {
var eng = JSON.parse(body).message?.result.translatedText;
openai.createCompletion({
model: "text-davinci-002",
prompt: eng,
temperature: 0.7,
max_tokens: 128,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
}).then((result) => {
console.log('ai 응답', result.data.choices[0].text);
var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
var query = result.data.choices[0].text;
var options = {
url: api_url,
form: {'source':'en', 'target':'ko', 'text':query},
headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
};
request.post(options, function (error, response, body) {
console.log('papago : ', body);
res.status(200).json(body);
});
}).catch((error)=>{
console.log('openai error', error)
})
});
});
app.listen(3000, function () {
console.log('http://localhost:3000/ app listening on port 3000!');
});