This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (53 loc) · 1.59 KB
/
index.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
import chalk from "chalk";
import OpenAI from "openai";
import ora from "ora";
import prompts from "prompts";
import "dotenv/config";
if (!process.env.OPENAI_ORG) throw new Error("OPENAI_ORG is not defined");
if (!process.env.OPENAI_KEY) throw new Error("OPENAI_KEY is not defined");
const openai = new OpenAI({
organization: process.env.OPENAI_ORG,
apiKey: process.env.OPENAI_KEY,
});
const generate = async () => {
try {
const answer = await prompts({
type: "text",
name: "tweet",
message: "What do you want to tweet?",
validate: (value) => {
if (value.length < 5) {
return "The tweet must be at least 5 characters long";
} else if (value.length > 100) {
return "The tweet must be less than 100 characters long";
} else {
return true;
}
},
});
const spinner = ora("Generating tweet...").start();
const prompt = `Tweet ${answer.tweet}`;
const response = await openai.completions.create({
model: "text-davinci-003",
prompt: prompt.toString(),
temperature: 0,
max_tokens: 60,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
spinner.stop();
console.log(chalk.green.bold("🐦") + chalk.bold(" Generated tweet: ") + response.choices[0].text.trim() + "\n");
const rerun = await prompts({
type: "confirm",
name: "value",
message: "Do you want to generate another tweet?",
initial: true,
});
rerun.value ? generate() : console.log(chalk.green.bold("👋") + chalk.bold(" Bye!"));
} catch (error) {
console.log(chalk.red.bold("\n✖") + chalk.bold(" Error: ") + error);
process.exit(1);
}
};
generate();