Skip to content

Python scraper based on AI

add examples for schema

Marco Vinciguerra4f53b09bf12e
diff --git a/examples/anthropic/search_graph_schema_haiku.py b/examples/anthropic/search_graph_schema_haiku.py
new file mode 100644
index 00000000..649f8497
--- /dev/null
+++ b/examples/anthropic/search_graph_schema_haiku.py
@@ -0,0 +1,58 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+load_dotenv()
+
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+graph_config = {
+ "llm": {
+ "api_key": os.getenv("ANTHROPIC_API_KEY"),
+ "model": "claude-3-haiku-20240307",
+ "max_tokens": 4000},
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/anthropic/smart_scraper_schema_haiku.py b/examples/anthropic/smart_scraper_schema_haiku.py
index 587eb8c2..83cedd2a 100644
--- a/examples/anthropic/smart_scraper_schema_haiku.py
+++ b/examples/anthropic/smart_scraper_schema_haiku.py
@@ -3,6 +3,8 @@ Basic example of scraping pipeline using SmartScraper using Azure OpenAI Key
"""
import os
+from typing import List
+from pydantic import BaseModel, Field
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
@@ -17,22 +19,12 @@ load_dotenv()
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Create the SmartScraperGraph instance and run it
@@ -48,7 +40,7 @@ graph_config = {
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description",
# also accepts a string with the already downloaded HTML code
- schema=schema,
+ schema=Projects,
source="https://perinim.github.io/projects/",
config=graph_config
)
diff --git a/examples/azure/search_graph_schema_azure.py b/examples/azure/search_graph_schema_azure.py
new file mode 100644
index 00000000..f435b547
--- /dev/null
+++ b/examples/azure/search_graph_schema_azure.py
@@ -0,0 +1,74 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+load_dotenv()
+
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+from langchain_openai import AzureChatOpenAI
+from langchain_openai import AzureOpenAIEmbeddings
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+
+llm_model_instance = AzureChatOpenAI(
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+ azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
+)
+
+embedder_model_instance = AzureOpenAIEmbeddings(
+ azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+)
+
+# ************************************************
+# Create the SmartScraperGraph instance and run it
+# ************************************************
+
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/azure/smart_scraper_schema_azure.py b/examples/azure/smart_scraper_schema_azure.py
index 1df69610..34fbe3d3 100644
--- a/examples/azure/smart_scraper_schema_azure.py
+++ b/examples/azure/smart_scraper_schema_azure.py
@@ -3,6 +3,8 @@ Basic example of scraping pipeline using SmartScraper with schema
"""
import os, json
+from typing import List
+from pydantic import BaseModel, Field
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
from langchain_openai import AzureOpenAIEmbeddings
@@ -14,22 +16,12 @@ load_dotenv()
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Initialize the model instances
@@ -60,7 +52,7 @@ graph_config = {
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description",
source="https://perinim.github.io/projects/",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/bedrock/search_graph_schema_bedrock.py b/examples/bedrock/search_graph_schema_bedrock.py
new file mode 100644
index 00000000..90539155
--- /dev/null
+++ b/examples/bedrock/search_graph_schema_bedrock.py
@@ -0,0 +1,58 @@
+"""
+Example of Search Graph
+"""
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "client": "client_name",
+ "model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
+ "temperature": 0.0
+ },
+ "embeddings": {
+ "model": "bedrock/cohere.embed-multilingual-v3"
+ }
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/bedrock/smart_scraper_schema_bedrock.py b/examples/bedrock/smart_scraper_schema_bedrock.py
index d830a373..6213ea1f 100644
--- a/examples/bedrock/smart_scraper_schema_bedrock.py
+++ b/examples/bedrock/smart_scraper_schema_bedrock.py
@@ -1,33 +1,21 @@
"""
Basic example of scraping pipeline using SmartScraper
"""
-
-import os
-from dotenv import load_dotenv
+from typing import List
+from pydantic import BaseModel, Field
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
-load_dotenv()
# ************************************************
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Define the configuration for the graph
@@ -52,7 +40,7 @@ smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description",
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/deepseek/search_graph_schema_deepseek.py b/examples/deepseek/search_graph_schema_deepseek.py
new file mode 100644
index 00000000..8debee2f
--- /dev/null
+++ b/examples/deepseek/search_graph_schema_deepseek.py
@@ -0,0 +1,68 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+load_dotenv()
+
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+deepseek_key = os.getenv("DEEPSEEK_APIKEY")
+
+graph_config = {
+ "llm": {
+ "model": "deepseek-chat",
+ "openai_api_key": deepseek_key,
+ "openai_api_base": 'https://api.deepseek.com/v1',
+ },
+ "embeddings": {
+ "model": "ollama/nomic-embed-text",
+ "temperature": 0,
+ # "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "verbose": True,
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/deepseek/smart_scraper_schema_deepseek.py b/examples/deepseek/smart_scraper_schema_deepseek.py
index 8d0cf376..a16ae575 100644
--- a/examples/deepseek/smart_scraper_schema_deepseek.py
+++ b/examples/deepseek/smart_scraper_schema_deepseek.py
@@ -3,6 +3,8 @@ Basic example of scraping pipeline using SmartScraper
"""
import os
+from typing import List
+from pydantic import BaseModel, Field
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
@@ -13,22 +15,12 @@ load_dotenv()
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Define the configuration for the graph
@@ -58,7 +50,7 @@ smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description.",
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/gemini/search_graph_schema_gemini.py b/examples/gemini/search_graph_schema_gemini.py
new file mode 100644
index 00000000..5c8429dd
--- /dev/null
+++ b/examples/gemini/search_graph_schema_gemini.py
@@ -0,0 +1,61 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+load_dotenv()
+
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+gemini_key = os.getenv("GOOGLE_APIKEY")
+
+graph_config = {
+ "llm": {
+ "api_key": gemini_key,
+ "model": "gemini-pro",
+ },
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/gemini/smart_scraper_schema_gemini.py b/examples/gemini/smart_scraper_schema_gemini.py
index 157d9542..3f9326ff 100644
--- a/examples/gemini/smart_scraper_schema_gemini.py
+++ b/examples/gemini/smart_scraper_schema_gemini.py
@@ -3,6 +3,8 @@ Basic example of scraping pipeline using SmartScraper with schema
"""
import os
+from typing import List
+from pydantic import BaseModel, Field
from dotenv import load_dotenv
from scrapegraphai.utils import prettify_exec_info
from scrapegraphai.graphs import SmartScraperGraph
@@ -11,22 +13,12 @@ load_dotenv()
# ************************************************
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Define the configuration for the graph
@@ -49,7 +41,7 @@ smart_scraper_graph = SmartScraperGraph(
prompt="List me all the news with their description.",
# also accepts a string with the already downloaded HTML code
source="https://www.wired.com",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/groq/search_graph_schema_groq.py b/examples/groq/search_graph_schema_groq.py
new file mode 100644
index 00000000..41f03dc4
--- /dev/null
+++ b/examples/groq/search_graph_schema_groq.py
@@ -0,0 +1,69 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+load_dotenv()
+
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+groq_key = os.getenv("GROQ_APIKEY")
+
+graph_config = {
+ "llm": {
+ "model": "groq/gemma-7b-it",
+ "api_key": groq_key,
+ "temperature": 0
+ },
+ "embeddings": {
+ "model": "ollama/nomic-embed-text",
+ "temperature": 0,
+ # "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "headless": False
+}
+
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/groq/smart_scraper_schema_groq.py b/examples/groq/smart_scraper_schema_groq.py
index 2b80c658..e0c51c98 100644
--- a/examples/groq/smart_scraper_schema_groq.py
+++ b/examples/groq/smart_scraper_schema_groq.py
@@ -3,6 +3,8 @@ Basic example of scraping pipeline using SmartScraper with schema
"""
import os, json
+from typing import List
+from pydantic import BaseModel, Field
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
@@ -13,22 +15,12 @@ load_dotenv()
# Define the output schema for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Define the configuration for the graph
@@ -58,7 +50,7 @@ smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description.",
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/local_models/search_graph_schema_ollama.py b/examples/local_models/search_graph_schema_ollama.py
new file mode 100644
index 00000000..ae7c0632
--- /dev/null
+++ b/examples/local_models/search_graph_schema_ollama.py
@@ -0,0 +1,63 @@
+"""
+Example of Search Graph
+"""
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "model": "ollama/mistral",
+ "temperature": 0,
+ "format": "json", # Ollama needs the format to be specified explicitly
+ # "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "embeddings": {
+ "model": "ollama/nomic-embed-text",
+ "temperature": 0,
+ # "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "verbose": True,
+ "headless": False
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/local_models/smart_scraper_schema_ollama.py b/examples/local_models/smart_scraper_schema_ollama.py
index e26c7c45..5c7aa03f 100644
--- a/examples/local_models/smart_scraper_schema_ollama.py
+++ b/examples/local_models/smart_scraper_schema_ollama.py
@@ -2,28 +2,20 @@
Basic example of scraping pipeline using SmartScraper with schema
"""
import json
+from typing import List
+from pydantic import BaseModel, Field
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
# ************************************************
# Define the configuration for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
graph_config = {
"llm": {
@@ -48,7 +40,7 @@ graph_config = {
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description",
source="https://perinim.github.io/projects/",
- schema=schema,
+ schema=Projects,
config=graph_config
)
diff --git a/examples/oneapi/search_graph_oneapi.py b/examples/oneapi/search_graph_oneapi.py
index 4190a0ff..6756f33b 100644
--- a/examples/oneapi/search_graph_oneapi.py
+++ b/examples/oneapi/search_graph_oneapi.py
@@ -2,11 +2,8 @@
Example of Search Graph
"""
-import os
-from dotenv import load_dotenv
from scrapegraphai.graphs import SearchGraph
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
-load_dotenv()
# ************************************************
# Define the configuration for the graph
diff --git a/examples/oneapi/search_graph_schema_oneapi.py b/examples/oneapi/search_graph_schema_oneapi.py
new file mode 100644
index 00000000..7fc44539
--- /dev/null
+++ b/examples/oneapi/search_graph_schema_oneapi.py
@@ -0,0 +1,55 @@
+"""
+Example of Search Graph
+"""
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+from pydantic import BaseModel, Field
+from typing import List
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Dish(BaseModel):
+ name: str = Field(description="The name of the dish")
+ description: str = Field(description="The description of the dish")
+
+class Dishes(BaseModel):
+ dishes: List[Dish]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "api_key": "***************************",
+ "model": "oneapi/qwen-turbo",
+ "base_url": "http://127.0.0.1:3000/v1", # 设置 OneAPI URL
+ }
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me Chioggia's famous dishes",
+ config=graph_config,
+ schema=Dishes
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/oneapi/smart_scraper_schema_oneapi.py b/examples/oneapi/smart_scraper_schema_oneapi.py
index bb7c729d..0c011bb6 100644
--- a/examples/oneapi/smart_scraper_schema_oneapi.py
+++ b/examples/oneapi/smart_scraper_schema_oneapi.py
@@ -1,29 +1,20 @@
"""
Basic example of scraping pipeline using SmartScraper and OneAPI
"""
-
+from typing import List
+from pydantic import BaseModel, Field
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
# ************************************************
# Define the configuration for the graph
# ************************************************
-schema= """
- {
- "Projects": [
- "Project #":
- {
- "title": "...",
- "description": "...",
- },
- "Project #":
- {
- "title": "...",
- "description": "...",
- }
- ]
- }
-"""
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
# ************************************************
# Define the configuration for the graph
@@ -46,6 +37,7 @@ smart_scraper_graph = SmartScraperGraph(
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
config=graph_config,
+ schema=Projects
)
# ************************************************
diff --git a/examples/openai/smart_scraper_schema_openai.py b/examples/openai/smart_scraper_schema_openai.py
index 85c6b2dc..076f1327 100644
--- a/examples/openai/smart_scraper_schema_openai.py
+++ b/examples/openai/smart_scraper_schema_openai.py
@@ -3,10 +3,9 @@ Basic example of scraping pipeline using SmartScraper with schema
"""
import os, json
+from typing import List
from dotenv import load_dotenv
from pydantic import BaseModel, Field
-from typing import List
-
from scrapegraphai.graphs import SmartScraperGraph
load_dotenv()