streaming.ts 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 in streaming mode
  29. const { stream } = await scraper.stream(page, schema, {
  30. format: 'html',
  31. })
  32. // Stream the result from LLM
  33. for await (const data of stream) {
  34. console.log(data.top)
  35. }
  36. await page.close()
  37. await browser.close()