streaming.ts 840 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. title: z.string(),
  17. points: z.number(),
  18. by: z.string(),
  19. commentsURL: z.string(),
  20. })
  21. // Run the scraper in streaming mode
  22. const { stream } = await scraper.stream(page, schema, {
  23. format: 'html',
  24. })
  25. // Stream the result from LLM
  26. for await (const data of stream) {
  27. console.log(data)
  28. }
  29. await page.close()
  30. await browser.close()