forked from mishushakov/llm-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mishushakov#32 from mishushakov/codegen
Codegen
- Loading branch information
Showing
8 changed files
with
204 additions
and
38 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
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,41 @@ | ||
import { chromium } from 'playwright' | ||
import { z } from 'zod' | ||
import { anthropic } from '@ai-sdk/anthropic' | ||
import LLMScraper from './../src' | ||
|
||
// Launch a browser instance | ||
const browser = await chromium.launch() | ||
|
||
// Initialize LLM provider | ||
const llm = anthropic('claude-3-5-sonnet-20240620') | ||
|
||
// Create a new LLMScraper | ||
const scraper = new LLMScraper(llm) | ||
|
||
// Open new page | ||
const page = await browser.newPage() | ||
await page.goto('https://www.bbc.com') | ||
|
||
// Define schema to extract contents into | ||
const schema = z.object({ | ||
news: z.array( | ||
z.object({ | ||
title: z.string(), | ||
description: z.string(), | ||
url: z.string(), | ||
}) | ||
), | ||
}) | ||
|
||
// Generate code and run it on the page | ||
const { code } = await scraper.generate(page, schema) | ||
console.log('code', code) | ||
|
||
const result = await page.evaluate(code) | ||
const data = schema.parse(result) | ||
|
||
// Show the parsed result | ||
console.log('result', data) | ||
|
||
await page.close() | ||
await browser.close() |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Oops, something went wrong.