-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (69 loc) · 2.18 KB
/
app.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
require("dotenv").config();
const path = require("path");
const express = require("express");
const OpenAI = require("openai");
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json())
app.use(express.static(path.join(__dirname, "public")));
app.set("views", "./public");
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.post("/generate-content", async (req, res) => {
const { prompt, snippet } = req.body
console.log(prompt, snippet)
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
});
// hit api;
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": `
Buatkan sebuah blog tentang ${prompt}
Terdiri dari pembuka, isi konten, dan penutup
Isinya harus SEO-friendly agar mendapatkan featured snippet ${snippet}.
Buatkan dalam format HTML dan gunakan class tailwind langsung, setiap point penting ditebalkan.
` }
],
temperature: 0,
stream: true,
});
try {
for await (const chunk of completion) {
const result = chunk.choices[0].delta.content;
if (result) {
res.write(result)
}
}
} catch (error) {
console.log(error)
res.write("errorr generate prompt")
}
});
// const generateBlogPost = async (prompt, snippetType) => {
// try {
// const response = await openai.completions.create({
// model: "gpt-3.5-turbo-instruct",
// prompt: `
// Buatkan sebuah blog tentang ${prompt}
// Terdiri dari pembuka, isi konten, dan penutup
// Isinya harus SEO-friendly agar mendapatkan featured snippet ${snippetType}.
// Buatkan dalam format HTML dan gunakan class tailwind langsung, setiap point penting ditebalkan.
// `,
// max_tokens: 4000,
// });
// console.log(response.choices);
// return response.choices[0].text.trim();
// } catch (error) {
// throw error;
// }
// };
module.exports = app;