-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (34 loc) · 1.02 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
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.post("/translate", async (req, res) => {
const { words } = req.body;
try {
const response = await axios.post(
"https://api.openai.com/v4/completions",
{
model: "text-davinci-003", // Update with the latest model if necessary
prompt: words.map((word) => `Translate "${word}" to Dutch.`).join("\n"),
temperature: 0.3,
max_tokens: 60,
},
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
const translations = response.data.choices[0].text.trim().split("\n");
res.json({ translations });
} catch (error) {
console.error("OpenAI API error:", error);
res.status(500).send("Failed to translate words");
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});