|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using Code Generator with schema |
| 3 | +""" |
| 4 | + |
| 5 | +import os, json |
| 6 | +from typing import List |
| 7 | +from dotenv import load_dotenv |
| 8 | +from pydantic import BaseModel, Field |
| 9 | +from scrapegraphai.graphs import CodeGeneratorGraph |
| 10 | + |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +# ************************************************ |
| 14 | +# Define the output schema for the graph |
| 15 | +# ************************************************ |
| 16 | + |
| 17 | +class Project(BaseModel): |
| 18 | + title: str = Field(description="The title of the project") |
| 19 | + description: str = Field(description="The description of the project") |
| 20 | + |
| 21 | +class Projects(BaseModel): |
| 22 | + projects: List[Project] |
| 23 | + |
| 24 | +# ************************************************ |
| 25 | +# Define the configuration for the graph |
| 26 | +# ************************************************ |
| 27 | + |
| 28 | +together_key = os.getenv("TOGETHER_KEY") |
| 29 | + |
| 30 | +graph_config = { |
| 31 | + "llm": { |
| 32 | + "model": "togetherai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", |
| 33 | + "api_key": together_key, |
| 34 | + }, |
| 35 | + "verbose": True, |
| 36 | + "headless": False, |
| 37 | + "reduction": 2, |
| 38 | + "max_iterations": { |
| 39 | + "overall": 10, |
| 40 | + "syntax": 3, |
| 41 | + "execution": 3, |
| 42 | + "validation": 3, |
| 43 | + "semantic": 3 |
| 44 | + }, |
| 45 | + "output_file_name": "extracted_data.py" |
| 46 | +} |
| 47 | + |
| 48 | +# ************************************************ |
| 49 | +# Create the SmartScraperGraph instance and run it |
| 50 | +# ************************************************ |
| 51 | + |
| 52 | +code_generator_graph = CodeGeneratorGraph( |
| 53 | + prompt="List me all the projects with their description", |
| 54 | + source="https://perinim.github.io/projects/", |
| 55 | + schema=Projects, |
| 56 | + config=graph_config |
| 57 | +) |
| 58 | + |
| 59 | +result = code_generator_graph.run() |
| 60 | +print(result) |
0 commit comments