-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0ab395
commit ed45843
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Example will grab todays HN page and works a lot like examples/hn.ts but | ||
// does the parsing from a local HTML file instead of using chromium/playwright | ||
import { z } from 'zod' | ||
import OpenAI from 'openai' | ||
import LLMScraper from '../src' | ||
import path from 'path' | ||
import { writeFileSync } from 'fs' | ||
|
||
// Initialize LLM provider | ||
const llm = new OpenAI() | ||
|
||
// Create a new LLMScraper | ||
const scraper = new LLMScraper(null, llm) | ||
|
||
// Define schema to extract contents into | ||
const schema = z.object({ | ||
top: z | ||
.array( | ||
z.object({ | ||
title: z.string(), | ||
points: z.number(), | ||
by: z.string(), | ||
commentsURL: z.string(), | ||
}) | ||
) | ||
.length(5) // How many results to parse for this specific instance. | ||
.describe('Top 5 stories on Hacker News'), | ||
}) | ||
|
||
// Grab today's HN front page to run the example | ||
const htmlString = await fetch('https://news.ycombinator.com/') | ||
.then((res) => res.text()) | ||
.catch((e) => { | ||
console.error("Failed to fetch content from Hackernews", e) | ||
return null; | ||
}) | ||
|
||
// Run the scraper | ||
const pages = await scraper.rawHTML([htmlString], { | ||
model: 'gpt-4-turbo', | ||
schema, | ||
mode: 'html', | ||
closeOnFinish: true, | ||
}) | ||
|
||
// Stream the result from LLM | ||
for await (const page of pages) { | ||
console.log(page.data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters