hn.ts 919 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { chromium } from 'playwright'
  2. import { z } from 'zod'
  3. import { openai } from '@ai-sdk/openai'
  4. import LLMScraper from './../src'
  5. // Launch a browser instance
  6. const browser = await chromium.launch()
  7. // Initialize LLM provider
  8. const llm = openai.chat('gpt-4o')
  9. // Create a new LLMScraper
  10. const scraper = new LLMScraper(llm)
  11. // Open new page
  12. const page = await browser.newPage()
  13. await page.goto('https://news.ycombinator.com')
  14. // Define schema to extract contents into
  15. const schema = z.object({
  16. top: z
  17. .array(
  18. z.object({
  19. title: z.string(),
  20. points: z.number(),
  21. by: z.string(),
  22. commentsURL: z.string(),
  23. })
  24. )
  25. .length(5)
  26. .describe('Top 5 stories on Hacker News'),
  27. })
  28. // Run the scraper
  29. const { data } = await scraper.run(page, schema, {
  30. format: 'html',
  31. })
  32. // Show the result from LLM
  33. console.log(data.top)
  34. await page.close()
  35. await browser.close()