-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
400 lines (353 loc) · 11.9 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Load necessary packages
const dotenv = require("dotenv");
const puppeteer = require("puppeteer");
const { encode } = require("@nem035/gpt-3-encoder");
const chance = require("chance").Chance();
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const { WEBPAGE_UNDERSTANDER_PROMPT } = require("./prompts");
// Configure the OpenAI API
dotenv.config();
const OpenAI = require("openai");
const openai = new OpenAI();
// Set constants
const ALLOWED_TEXT_ELEMENTS =
"p, h1, h2, h3, h4, h5, h6, a, td, th, tr, pre, code, blockquote";
const DEFAULT_CHUNK_AMOUNT = 12952;
const DEFAULT_SUMMARY_PROMPT =
"Please sort these facts from in order of importance, with the most important fact first";
const DEFAULT_SUMMARY_MAX_TOKENS = 4096;
/**
* Main function to fetch and summarize a webpage from a URL
* @param {string} url - The URL of the webpage to summarize
* @param {Object} options - Additional options for summarization
* @param {string} options.model - The OpenAI model to use for summarization (default: "gpt-4-turbo-preview")
* @param {number} options.chunkAmount - The desired chunk amount (default: DEFAULT_CHUNK_AMOUNT)
* @param {string} options.summaryPrompt - The prompt for generating the summary (default: DEFAULT_SUMMARY_PROMPT)
* @param {number} options.summaryMaxTokens - The maximum number of tokens for the summary (default: DEFAULT_SUMMARY_MAX_TOKENS)
* @param {string} options.chunkPrompt - The prompt for processing chunks (default: WEBPAGE_UNDERSTANDER_PROMPT)
* @returns {Promise<string>} - The generated summary
*/
async function fetchAndSummarizeUrl(url, options = {}) {
const cleanedUrl = cleanUrlForPuppeteer(url);
const {
model = "gpt-3.5-turbo",
chunkAmount = DEFAULT_CHUNK_AMOUNT,
summaryPrompt = DEFAULT_SUMMARY_PROMPT,
summaryMaxTokens = DEFAULT_SUMMARY_MAX_TOKENS,
chunkPrompt = WEBPAGE_UNDERSTANDER_PROMPT,
} = options;
try {
logMessage(`📝 Fetching URL: ${cleanedUrl}`);
const data = await fetchAndParseURL(cleanedUrl);
logMessage(`📝 Fetched URL: ${cleanedUrl}`);
const summary = await generateSummary(cleanedUrl, data, {
model,
chunkAmount,
summaryPrompt,
summaryMaxTokens,
chunkPrompt,
});
logMessage(`📝 Generated summary for URL: ${cleanedUrl}`);
console.log(summary);
return summary;
} catch (error) {
logMessage(error);
return error;
}
}
/**
* Generate a summary from the fetched webpage data
* @param {string} url - The URL of the webpage
* @param {Object} data - The fetched webpage data
* @param {Object} options - Additional options for summarization
* @param {string} options.model - The OpenAI model to use for summarization
* @param {number} options.chunkAmount - The desired chunk amount
* @param {string} options.summaryPrompt - The prompt for generating the summary
* @param {number} options.summaryMaxTokens - The maximum number of tokens for the summary
* @param {string} options.chunkPrompt - The prompt for processing chunks
* @returns {Promise<string>} - The generated summary
*/
async function generateSummary(url, data, options) {
const { model, chunkAmount, summaryPrompt, summaryMaxTokens, chunkPrompt } =
options;
logMessage("📝 Generating summary...");
let text = cleanText(data.text);
const tokenCount = countMessageTokens(text);
logMessage(`📝 Token count: ${tokenCount}`);
const chunks = splitTextIntoChunks(text, chunkAmount, tokenCount);
logMessage(`📝 Splitting text into ${chunks.length} chunks...`);
logMessage(`📝 Chunk length: ${chunkAmount} tokens`);
let factList = "";
try {
const chunkResponses = await processChunks(
chunks,
data,
model,
chunkPrompt
);
factList = chunkResponses.join("\n");
} catch (error) {
logMessage(error);
return error;
}
logMessage(`📝 Generated ${factList.split("\n").length} fact summary.`);
logMessage(`📝 Generating summary of: ${factList}`);
// pause for a bit before generating the summary
await sleep(1000);
const summaryCompletion = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
max_tokens: summaryMaxTokens,
temperature: 1.3,
top_p: 0.88,
frequency_penalty: 0.1,
messages: [
{
role: "user",
content: `${summaryPrompt}:\n\nURL: <${url}>\n\n${factList}`,
},
],
});
const summary = summaryCompletion.choices[0].message.content;
return summary;
}
/**
* Fetch and parse the URL using puppeteer
* @param {string} url - The URL to fetch and parse
* @returns {Promise<Object>} - The fetched webpage data
*/
async function fetchAndParseURL(url) {
try {
const browser = await puppeteer.launch({ headless: "new" });
const page = await browser.newPage();
await page.setUserAgent(randomUserAgent());
await page.goto(url);
logMessage(`🕸️ Navigating to ${url}`);
await page.waitForSelector("body");
const title = await page.title();
const text = await page.$$eval(ALLOWED_TEXT_ELEMENTS, (elements) =>
elements
.map((element) => element?.textContent.replace(/<[^>]*>?/gm, "") + " ")
.join(" ")
.replace(/\s+/g, " ")
.trim()
);
const links = await page.$$eval("a", (elements) =>
elements.map((element) => ({
text: element?.textContent.replace(/<[^>]*>?/gm, "").trim(),
href: element.href,
}))
);
logMessage(`📝 Page raw text: ${text}`);
await browser.close();
return { title, text, links };
} catch (error) {
logMessage(`❌ Error fetching and parsing URL: ${error}`);
throw error;
}
}
/**
* Process chunks of text and send them to OpenAI API for processing
* @param {string[]} chunks - The chunks of text to process
* @param {Object} data - The fetched webpage data
* @param {string} model - The OpenAI model to use for processing
* @param {string} chunkPrompt - The prompt for processing chunks
* @param {number} limit - The maximum number of chunks to process concurrently
* @param {number} sleepDuration - The duration to sleep between API requests in milliseconds
* @returns {Promise<string[]>} - The processed chunk responses
*/
async function processChunks(
chunks,
data,
model,
chunkPrompt,
limit = 2,
sleepDuration = 2000
) {
const results = [];
chunks = chunks.filter((chunk) => chunk.length > 0);
for (let i = 0; i < chunks.length; i += limit) {
const chunkPromises = chunks
.slice(i, i + limit)
.map(async (chunk, index) => {
await sleep(sleepDuration);
logMessage(`📝 Sending chunk ${i + index + 1} of ${chunks.length}...`);
logMessage(`📝 Chunk text: ${chunk}`);
const completion = await openai.chat.completions.create({
model,
max_tokens: 2048,
temperature: 0.5,
presence_penalty: -0.1,
messages: [
{
role: "user",
content: `${chunkPrompt} ${chunk} Remember to ignore any navigation links or other text that isn't relevant to the main content of the page. Include relevant URLs in your summaries wherever possible.`,
},
],
});
return completion.choices[0].message.content;
});
results.push(...(await Promise.all(chunkPromises)));
}
return results;
}
/**
* Generate a random user agent
* @returns {string} - The randomly selected user agent
*/
function randomUserAgent() {
const potentialUserAgents = [
`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36`,
`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15`,
`Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36`,
`Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0`,
];
const pickedUserAgent = chance.pickone(potentialUserAgents);
logMessage(`📝 Picked User Agent: ${pickedUserAgent}`);
return pickedUserAgent;
}
/**
* Count the number of tokens in a message array
* @param {string[]} messageArray - The array of messages
* @returns {number} - The total number of tokens
*/
function countMessageTokens(messageArray = []) {
let totalTokens = 0;
if (!messageArray || messageArray.length === 0) {
return totalTokens;
}
for (let i = 0; i < messageArray.length; i++) {
const message = messageArray[i];
const encodedMessage = encode(JSON.stringify(message));
totalTokens += encodedMessage.length;
}
return totalTokens;
}
/**
* Clean the URL for use with puppeteer
* @param {string} dirtyUrl - The URL to clean
* @returns {string} - The cleaned URL
*/
function cleanUrlForPuppeteer(dirtyUrl) {
if (!dirtyUrl) return "";
dirtyUrl = dirtyUrl.toString();
return dirtyUrl.replace(/^'+|'+$/g, "");
}
/**
* Clean the text by removing newlines, tabs, and multiple spaces
* @param {string} text - The text to clean
* @returns {string} - The cleaned text
*/
function cleanText(text) {
return text
.replace(/\n/g, " ")
.replace(/\t/g, " ")
.replace(/ +(?= )/g, "");
}
/**
* Split the text into chunks based on the specified chunk amount and token count
* @param {string} text - The text to split into chunks
* @param {number} chunkAmount - The desired chunk amount
* @param {number} tokenCount - The total token count of the text
* @returns {string[]} - The array of text chunks
*/
function splitTextIntoChunks(text, chunkAmount, tokenCount) {
const chunks = [];
const lines = text.split("\n");
let currentChunk = "";
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineTokens = countMessageTokens(line);
if (currentChunk.length + lineTokens <= chunkAmount) {
currentChunk += line + "\n";
} else {
chunks.push(currentChunk.trim());
currentChunk = line + "\n";
}
}
if (currentChunk.length > 0) {
chunks.push(currentChunk.trim());
}
return chunks;
}
/**
* Sleep for a specified number of milliseconds
* @param {number} ms - The number of milliseconds to sleep
* @returns {Promise} - A promise that resolves after the specified time
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Log a message to the console
* @param {string} message - The message to log
*/
function logMessage(message) {
console.log(message);
}
/**
* Main function to handle command-line arguments and initiate the summarization process
*/
async function main() {
const argv = yargs(hideBin(process.argv))
.option("url", {
alias: "u",
type: "string",
description: "URL to summarize",
})
.option("model", {
alias: "m",
type: "string",
description: "OpenAI model to use for summarization",
default: "gpt-4-turbo-preview",
})
.option("chunkAmount", {
alias: "c",
type: "number",
description: "Desired chunk size for text splitting",
default: DEFAULT_CHUNK_AMOUNT,
})
.option("summaryPrompt", {
alias: "sp",
type: "string",
description: "Prompt for generating the summary",
default: DEFAULT_SUMMARY_PROMPT,
})
.option("summaryMaxTokens", {
alias: "smt",
type: "number",
description: "Maximum number of tokens for the summary",
default: DEFAULT_SUMMARY_MAX_TOKENS,
})
.option("chunkPrompt", {
alias: "cp",
type: "string",
description: "Prompt for processing text chunks",
default: WEBPAGE_UNDERSTANDER_PROMPT,
}).argv;
const {
url,
model,
chunkAmount,
summaryPrompt,
summaryMaxTokens,
chunkPrompt,
} = argv;
if (url) {
const options = {
model,
chunkAmount,
summaryPrompt,
summaryMaxTokens,
chunkPrompt,
};
await fetchAndSummarizeUrl(url, options);
} else {
console.error("No URL provided");
}
}
if (require.main === module) {
main();
} else {
module.exports = { fetchAndSummarizeUrl };
}