-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathopenai-summary.ts
More file actions
81 lines (68 loc) · 2.1 KB
/
Copy pathopenai-summary.ts
File metadata and controls
81 lines (68 loc) · 2.1 KB
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
/**
* OpenAI Summarization Example
*
* Scrapes a webpage and uses OpenAI to summarize the content.
*
* Usage:
* npx tsx ai-tools/openai-summary.ts https://example.com
*
* Requirements:
* - Set OPENAI_API_KEY environment variable
*/
import { ReaderClient } from "@vakra-dev/reader";
import OpenAI from "openai";
async function main() {
const url = process.argv[2] || "https://example.com";
console.log(`Scraping ${url}...\n`);
// Check for API key
if (!process.env.OPENAI_API_KEY) {
console.error("Error: OPENAI_API_KEY environment variable is required");
process.exit(1);
}
const reader = new ReaderClient();
try {
// Step 1: Scrape the webpage
const result = await reader.scrape({
urls: [url],
formats: ["markdown"], // Markdown is best for LLM consumption
});
const content = result.data[0]?.markdown;
if (!content) {
console.error("No content scraped");
process.exit(1);
}
console.log(`Scraped ${content.length} characters`);
console.log("Sending to OpenAI for summarization...\n");
// Step 2: Summarize with OpenAI
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content:
"You are a helpful assistant that summarizes web content. Provide a concise summary in 2-3 paragraphs.",
},
{
role: "user",
content: `Please summarize the following webpage content:\n\n${content.slice(0, 10000)}`,
},
],
max_tokens: 500,
});
const summary = completion.choices[0]?.message?.content;
console.log("=== SUMMARY ===\n");
console.log(summary);
console.log("\n=== METADATA ===");
console.log(`Source: ${url}`);
console.log(`Content length: ${content.length} chars`);
console.log(`Model: ${completion.model}`);
console.log(`Tokens used: ${completion.usage?.total_tokens}`);
} catch (error: any) {
console.error("Error:", error.message);
process.exit(1);
} finally {
await reader.close();
}
}
main();