-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAIAPI.js
181 lines (169 loc) · 6.13 KB
/
OpenAIAPI.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import OpenAI from "openai";
class OpenAIAPI {
//todo: need a better solution than just adding arguments to pass in more functions
constructor(wordList,OpenAI_utilities) {
this.client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Your OpenAI API Key
});
this.wordList = wordList;
this.OpenAI_utilities = OpenAI_utilities;
}
getRandomWord() {
const randomIndex = Math.floor(Math.random() * this.wordList.length);
return this.wordList[randomIndex];
}
async generateWordAndPictureUntilSuccess(wordParam = null,score,language){
console.log("starting generation");
console.log({score});
let success = false;
let alreadyShownToday = false;
let generateAttempts = 0;
let word;
let picture;
while(!success){
try{
//if can't generate word or get random from language, just select a word from the list
if(alreadyShownToday && generateAttempts > 3){
word = this.getRandomWord();
}
else{
//start by searching word_image for an existing word in this language
word = await this.OpenAI_utilities.getRandomWordByLanguage(language);
//if there are no words in this language, we need to generate one
if(!word){
word = await this.generateWord(wordParam,score,language);
}
console.log({word});
alreadyShownToday = await this.OpenAI_utilities.wordGeneratedToday(word);
console.log({alreadyShownToday});
if (alreadyShownToday){
generateAttempts++;
throw "already shown today";
}
if (word.length > process.env.WORD_LENGTH_MAX){
throw "word too long";
}
}
picture = await this.generatePicture(word,language);
success = true;
}
catch(error){
console.error("Error generating word and picture:",error);
console.log("word+picture generation failed, trying again...");
}
}
const result = {
word:word,
picture:picture
}
return result;
}
//give user a chance at getting an easy game, lower chance as score increases
chance(num) {
//give user a minimum number of games that are automatically easy
if (num < process.env.EASY_GAMES_COUNT){
return true;
}
num = parseInt(num);
// Generate a random integer between 0 and num (inclusive)
const result = Math.floor(Math.random() * (num + 1));
console.log("chance:",{num},"result:",{result})
// Check if the result is equal to num and return the boolean value
return result >= (num / 2);
}
async generateWord(topicParam,score = 0,language) {
try {
console.log("generating word...");
let prompt;
if (topicParam){
prompt = process.env.GENERATE_WORD_PROMPT_TOPIC.replace('topic',topicParam).replace('language',language);
}
else{
if(this.chance(score)){
prompt = process.env.GENERATE_WORD_PROMPT_CONCRETE.replace('language',language);
}
else{
prompt = process.env.GENERATE_WORD_PROMPT_ABSTRACT.replace('language',language);
}
}
console.log({prompt});
const response = await this.client.chat.completions.create({
model: "gpt-3.5-turbo", // Or another suitable model
messages: [
{role:"user",content:prompt}
],
max_tokens: 10,
});
console.log({response});
let word = response.choices[0].message.content.toLowerCase().trim();
// Additional logic to ensure the word meets your criteria
console.log("generateWord:",{word});
return word;
} catch (error) {
console.error('Error generating word:', error);
throw error;
}
}
async generatePicture(word,language) {
try {
const existingPicture = await this.OpenAI_utilities.findExistingPicture(word);
console.log({existingPicture});
if (existingPicture){
console.log("found existing picture:",existingPicture);
try{
//generate another picture so we add more variety to this word_image
//but do it async so user doesn't have to wait for extra image to be generated
let picture = this.generateAndStorePicture(word,language);
console.log("Successfully generated extra picture");
}
catch(err){
console.error('Error generating extra picture:',err)
}
return existingPicture;
}
let picture = await this.generateAndStorePicture(word,language);
return picture;
} catch (error) {
console.error('Error generating picture:', error);
throw error;
}
}
async generateAndStorePicture(word,language){
const response = await this.client.images.generate({
model: "dall-e-3", // Replace with the appropriate model
prompt:`Generate a picture of ${word}, in a random historical art style. Do not include the word '${word}' in the picture. Do not include any text in the picture.`,
n: 1, // Number of images to generate
size: "1024x1024" // Image size
});
let picture = response.data[0].url; // URL of the generated image
console.log("storing image...");
this.OpenAI_utilities.storeImage(word,picture,language);
return picture;
}
async generateCompliment(word,language) {
try {
console.log("generating compliment...");
let prompt = process.env.GENERATE_COMPLIMENT.replaceAll("'word'",word).replace('language',language);
if(language !== "English"){
prompt += " Do not include an English translation.";
}
console.log({prompt});
const response = await this.client.chat.completions.create({
model: "gpt-3.5-turbo", // Or another suitable model
messages: [
{role:"user",content:prompt}
],
max_tokens: 10,
});
console.log({response});
let compliment = response.choices[0].message.content;
// Additional logic to ensure the word meets your criteria
console.log("generateCompliment:",{compliment});
return compliment;
} catch (error) {
console.error('Error generating compliment:', error);
throw error;
}
}
}
export default OpenAIAPI;