-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion-handler.js
246 lines (204 loc) · 6.94 KB
/
question-handler.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { createClient } from "@supabase/supabase-js";
import { Configuration, OpenAIApi } from "openai";
import dotenv from "dotenv";
import fs from "fs";
import { fileURLToPath } from "url";
import { dirname } from "path";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
dotenv.config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
organization: process.env.OPENAI_ORG,
});
console.log("org: " + process.env.OPENAI_ORG);
const openai = new OpenAIApi(configuration);
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: {
persistSession: false,
},
});
// get the first row of the highlights table and use the api to generate a question and add it to the row as the question column
const generateQuestion = async (row) => {
try {
const highlight = row.text;
const prompt = `Generate a single question from this quote. The end user cannot see the quote so DO NOT use any abstract concepts like "the speaker" or "the writer" in your question. BE EXPLICIT. DO NOT ASSUME the reader has read the quote. DO NOT use passive voice and do not use passive pronouns like he/she/they/him/her etc. You can use any of who/what/where/when/why. Say nothing else.\n\nQuote:\n\n${highlight}\n\nQ:`;
console.log("prompt: " + highlight);
const completion = await openai.createChatCompletion({
messages: [
{
role: "system",
content: prompt,
},
],
model: "gpt-3.5-turbo",
});
const content = completion.data.choices[0].message.content;
return content;
} catch (err) {
console.log("START ERROR")
console.error(err);
console.error(err.response);
console.error(err.response.data);
console.error(err.response.data.error);
console.error(err.response.data.error.message);
console.error(err.response.data.error.code);
console.error(err.response.data.error.status);
console.error(err.response.data.error.request);
console.log("END ERROR")
throw err;
}
};
const addQuestionToHighlight = async (row, question) => {
try {
const res = await supabase
.from("highlights")
.update({ question })
.match({ id: row.id });
} catch (err) {
console.log("Error adding question to highlight");
console.error(err);
throw err;
}
};
const filter_start_at_page = (records, start_at_id) => {
if (!start_at_id) {
return records;
}
// get anything in array after the start_at_id it should be in one of the pages [][]
let found = false;
let pageNumber = 0;
for (const page of records) {
for (const row of page) {
if (row.id === start_at_id) {
found = true;
console.log("found start at id: " + start_at_id + " at page: " + pageNumber);
}
if (found) {
break;
}
}
pageNumber++;
}
return pageNumber;
};
const fetchAllRecords = async (table_name, page_size = 1000) => {
const all_records = [];
let current_page = 0;
while (true) {
const response = await supabase
.from(table_name)
.select("*")
// where question column is null
.is("question", null)
.range(current_page * page_size, (current_page + 1) * page_size - 1);
const records = response.data;
if (!records || records.length === 0) {
break;
}
// remove the embedding column
for (const record of records) {
delete record.embedding;
}
if (records && records.length > 0) {
all_records.push(records);
current_page += 1;
console.log("current page: " + current_page);
} else {
break;
}
}
await saveRecordsToFile(all_records);
return all_records;
};
(async () => {
try {
const justSave = false;
// check for records.json file
let res = await fetchAllRecords("highlights");
// const filePath = path.join(__dirname, "records.json");
// if (fs.existsSync(filePath)) {
// const file = fs.readFileSync(filePath);
// res = JSON.parse(file);
// } else {
// res = await fetchAllRecords("highlights");
// }
console.log("collected len: " + res.length * res[0].length);
if(!justSave) {
let count = 0;
// filter res to only include pages after pageNum
// res = res.slice(pageNum - 1);
// // iterate over all highlights and generate a question for each one
for (const page of res) {
console.log("page: " + count++);
for (const row of page) {
if (row.question && row.question.length > 0) {
console.log(
"SKIPPING row with question: " +
row.question +
" || " +
row.text +
" || " +
row.id
);
continue;
} else {
const question = await generateQuestion(row);
await addQuestionToHighlight(row, question);
console.log(question + " || " + row.text + " || " + row.id);
// wait 1 second between each request
// await new Promise((resolve) => setTimeout(resolve, 500));
}
}
}
// ...
// for (let pageIndex = res.length - 1; pageIndex >= 0; pageIndex--) {
// console.log("page: " + pageIndex);
// const page = res[pageIndex];
// for (let rowIndex = page.length - 1; rowIndex >= 0; rowIndex--) {
// const row = page[rowIndex];
// if (row.question && row.question.length > 0) {
// console.log(
// "SKIPPING row with question: " +
// row.question +
// " || " +
// row.text +
// " || " +
// row.id
// );
// continue;
// } else {
// const question = await generateQuestion(row);
// await addQuestionToHighlight(row, question);
// console.log(question + " || " + row.text + " || " + row.id);
// // wait 1 second between each request
// }
// }
// }
}
// ...
// for (const row of data) {
// if (row.question) {
// console.log("skipping row with question: " + row.question + " || " + row.text + " || " + row.id);
// continue;
// }
// const question = await generateQuestion(row);
// await addQuestionToHighlight(row, question);
// console.log(question + " || " + row.text + " || " + row.id);
// }
// const row = data[0];
// const question = await generateQuestion(row);
// await addQuestionToHighlight(row, question);
// console.log(question + " || " + row.text + " || " + row.id);
} catch (err) {
console.log("Error generating question");
console.error(err);
}
})();
const saveRecordsToFile = async (records) => {
const filePath = path.join(__dirname, "records.json");
fs.writeFileSync(filePath, JSON.stringify(records));
};