local.ts 814 B

12345678910111213141516171819202122232425262728293031323334
  1. import { chromium } from 'playwright'
  2. import { LlamaModel } from 'node-llama-cpp'
  3. import { z } from 'zod'
  4. import LLMScraper from './../src'
  5. // Launch a browser instance
  6. const browser = await chromium.launch()
  7. const modelPath =
  8. '/Users/mish/jan/models/tinyllama-1.1b/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf'
  9. const llm = new LlamaModel({ modelPath })
  10. // Initialize a new LLMScraper with local model
  11. const scraper = new LLMScraper(llm)
  12. // Open the page
  13. const page = await browser.newPage()
  14. await page.goto('https://example.com')
  15. // Define schema to extract contents into
  16. const schema = z.object({
  17. h1: z.string().describe('The main heading of the page'),
  18. })
  19. // Run the scraper
  20. const { data } = await scraper.run(page, schema, {
  21. format: 'text',
  22. })
  23. console.log(data)
  24. await page.close()
  25. await browser.close()