-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
52 lines (44 loc) · 1.76 KB
/
main.ts
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
import * as dotenv from "dotenv";
dotenv.config();
import { Configuration, OpenAIApi } from "openai";
import trainingTweets from "./training-tweets.json" assert { type: "json" };
import fs from "fs";
const TRAINING_TWEET_COUNT = 30;
const GENERATED_TWEET_COUNT = 70;
const randomTweets = trainingTweets
.sort(() => Math.random() - 0.5)
.slice(0, TRAINING_TWEET_COUNT);
console.log("Training tweets:", randomTweets);
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
console.log("Creating completion...");
const completion = await openai.createChatCompletion({
model: "gpt-4",
messages: [
{
role: "system",
content: `
You are 'GptHotzo', a german comendian publishing tweets under the handle @gpthotzo. Your tweets replicate those of your famous counterpart elhotzo.
User will give you a list of example tweets you created separated by '---'.
You will have to create ${GENERATED_TWEET_COUNT} funny german tweets that are similar to the training tweets, staying in the 280 character tweet limit.
Do not include any tweets that are in the example tweets! End each tweet with "#elhotzo"
Return the tweets as a minified JSON array, do not add any other text.
`,
},
{
role: "user",
content: randomTweets.join("\n---\n"),
},
],
});
console.log("Completion created");
console.log(completion.data.choices[0].message);
const tweets = JSON.parse(completion.data.choices[0].message!.content);
console.log("Tweets:", tweets);
const currentTweets = JSON.parse(
fs.readFileSync("tweets.json", "utf8")
) as string[];
const newTweets = [...currentTweets, ...tweets];
fs.writeFileSync("tweets.json", JSON.stringify(newTweets, null, 2));