Skip to content

Python scraper based on AI

rollback

Marco Vinciguerra6d33a8a25ef9
diff --git a/examples/example.py b/examples/example.py
deleted file mode 100644
index 322b6a81..00000000
--- a/examples/example.py
+++ /dev/null
@@ -1,64 +0,0 @@
-from scrapegraphai.graphs import PDFScraperGraph
-
-graph_config = {
- "llm": {
- "model": "ollama/llama3",
- "temperature": 0,
- "format": "json", # Ollama needs the format to be specified explicitly
- "model_tokens": 4000,
- },
- "embeddings": {
- "model": "ollama/nomic-embed-text",
- "temperature": 0,
- },
- "verbose": True,
- "headless": False,
-}
-
-# Covert to list
-sources = [
- "This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather—the interaction between call center architecture and outdoor weather conditions—in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity – largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.",
- "The diffusion of social media coincided with a worsening of mental health conditions among adolescents and young adults in the United States, giving rise to speculation that social media might be detrimental to mental health. In this paper, we provide quasi-experimental estimates of the impact of social media on mental health by leveraging a unique natural experiment: the staggered introduction of Facebook across U.S. colleges. Our analysis couples data on student mental health around the years of Facebook's expansion with a generalized difference-in-differences empirical strategy. We find that the roll-out of Facebook at a college increased symptoms of poor mental health, especially depression. We also find that, among students predicted to be most susceptible to mental illness, the introduction of Facebook led to increased utilization of mental healthcare services. Lastly, we find that, after the introduction of Facebook, students were more likely to report experiencing impairments to academic performance resulting from poor mental health. Additional evidence on mechanisms suggests that the results are due to Facebook fostering unfavorable social comparisons.",
- "Hollywood films are generally released first in the United States and then later abroad, with some variation in lags across films and countries. With the growth in movie piracy since the appearance of BitTorrent in 2003, films have become available through illegal piracy immediately after release in the US, while they are not available for legal viewing abroad until their foreign premieres in each country. We make use of this variation in international release lags to ask whether longer lags – which facilitate more local pre-release piracy – depress theatrical box office receipts, particularly after the widespread adoption of BitTorrent. We find that longer release windows are associated with decreased box office returns, even after controlling for film and country fixed effects. This relationship is much stronger in contexts where piracy is more prevalent: after BitTorrent’s adoption and in heavily-pirated genres. Our findings indicate that, as a lower bound, international box office returns in our sample were at least 7% lower than they would have been in the absence of pre-release piracy. By contrast, we do not see evidence of elevated sales displacement in US box office revenue following the adoption of BitTorrent, and we suggest that delayed legal availability of the content abroad may drive the losses to piracy."
- # Add more sources here
-]
-
-prompt = """
-You are an expert in reviewing academic manuscripts. Please analyze the abstracts provided from an academic journal article to extract and clearly identify the following elements:
-
-Independent Variable (IV): The variable that is manipulated or considered as the primary cause affecting other variables.
-Dependent Variable (DV): The variable that is measured or observed, which is expected to change as a result of variations in the Independent Variable.
-Exogenous Shock: Identify any external or unexpected events used in the study that serve as a natural experiment or provide a unique setting for observing the effects on the IV and DV.
-Response Format: For each abstract, present your response in the following structured format:
-
-Independent Variable (IV):
-Dependent Variable (DV):
-Exogenous Shock:
-
-Example Queries and Responses:
-
-Query: This paper provides evidence from a natural experiment on the relationship between positive affect and productivity. We link highly detailed administrative data on the behaviors and performance of all telesales workers at a large telecommunications company with survey reports of employee happiness that we collected on a weekly basis. We use variation in worker mood arising from visual exposure to weather the interaction between call center architecture and outdoor weather conditions in order to provide a quasi-experimental test of the effect of happiness on productivity. We find evidence of a positive impact on sales performance, which is driven by changes in labor productivity largely through workers converting more calls into sales, and to a lesser extent by making more calls per hour and adhering more closely to their schedule. We find no evidence in our setting of effects on measures of high-frequency labor supply such as attendance and break-taking.
-
-Response:
-
-Independent Variable (IV): Employee happiness.
-Dependent Variable (DV): Overall firm productivity.
-Exogenous Shock: Sudden company-wide increase in bonus payments.
-
-Query: The diffusion of social media coincided with a worsening of mental health conditions among adolescents and young adults in the United States, giving rise to speculation that social media might be detrimental to mental health. In this paper, we provide quasi-experimental estimates of the impact of social media on mental health by leveraging a unique natural experiment: the staggered introduction of Facebook across U.S. colleges. Our analysis couples data on student mental health around the years of Facebook's expansion with a generalized difference-in-differences empirical strategy. We find that the roll-out of Facebook at a college increased symptoms of poor mental health, especially depression. We also find that, among students predicted to be most susceptible to mental illness, the introduction of Facebook led to increased utilization of mental healthcare services. Lastly, we find that, after the introduction of Facebook, students were more likely to report experiencing impairments to academic performance resulting from poor mental health. Additional evidence on mechanisms suggests that the results are due to Facebook fostering unfavorable social comparisons.
-
-Response:
-
-Independent Variable (IV): Exposure to social media.
-Dependent Variable (DV): Mental health outcomes.
-Exogenous Shock: staggered introduction of Facebook across U.S. colleges.
-"""
-results = []
-for source in sources:
- pdf_scraper_graph = PDFScraperGraph(
- prompt=prompt,
- source=source,
- config=graph_config
- )
- result = pdf_scraper_graph.run()
- results.append(result)
\ No newline at end of file
diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py
index 31945ec2..6a0c7a4c 100644
--- a/scrapegraphai/graphs/abstract_graph.py
+++ b/scrapegraphai/graphs/abstract_graph.py
@@ -8,9 +8,13 @@ from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings
from langchain_community.embeddings import HuggingFaceHubEmbeddings, OllamaEmbeddings
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from ..helpers import models_tokens
-from ..models import AzureOpenAI, Bedrock, Gemini, Groq, HuggingFace, Ollama, OpenAI, Anthropic
+from ..models import AzureOpenAI, Bedrock, Gemini, Groq, HuggingFace, Ollama, OpenAI, Anthropic, DeepSeek
from langchain_google_genai.embeddings import GoogleGenerativeAIEmbeddings
+from ..helpers import models_tokens
+from ..models import AzureOpenAI, Bedrock, Gemini, Groq, HuggingFace, Ollama, OpenAI, Anthropic, DeepSeek
+
+
class AbstractGraph(ABC):
"""
Scaffolding class for creating a graph representation and executing it.
@@ -18,6 +22,7 @@ class AbstractGraph(ABC):
prompt (str): The prompt for the graph.
source (str): The source of the graph.
config (dict): Configuration parameters for the graph.
+ schema (str): The schema for the graph output.
llm_model: An instance of a language model client, configured for generating answers.
embedder_model: An instance of an embedding model client,
configured for generating embeddings.
@@ -28,6 +33,7 @@ class AbstractGraph(ABC):
prompt (str): The prompt for the graph.
config (dict): Configuration parameters for the graph.
source (str, optional): The source of the graph.
+ schema (str, optional): The schema for the graph output.
Example:
>>> class MyGraph(AbstractGraph):
@@ -39,15 +45,21 @@ class AbstractGraph(ABC):
>>> result = my_graph.run()
"""
- def __init__(self, prompt: str, config: dict, source: Optional[str] = None):
+ def __init__(self, prompt: str, config: dict, source: Optional[str] = None, schema: Optional[str] = None):
self.prompt = prompt
self.source = source
self.config = config
+ self.schema = schema
self.llm_model = self._create_llm(config["llm"], chat=True)
self.embedder_model = self._create_default_embedder(llm_config=config["llm"]
) if "embeddings" not in config else self._create_embedder(
config["embeddings"])
+ self.verbose = False if config is None else config.get(
+ "verbose", False)
+ self.headless = True if config is None else config.get(
+ "headless", True)
+ self.loader_kwargs = config.get("loader_kwargs", {})
# Create the graph
self.graph = self._create_graph()
@@ -55,18 +67,20 @@ class AbstractGraph(ABC):
self.execution_info = None
# Set common configuration parameters
-
self.verbose = False if config is None else config.get(
"verbose", False)
self.headless = True if config is None else config.get(
"headless", True)
self.loader_kwargs = config.get("loader_kwargs", {})
- common_params = {"headless": self.headless,
-
- "loader_kwargs": self.loader_kwargs,
- "llm_model": self.llm_model,
- "embedder_model": self.embedder_model}
+ common_params = {
+ "headless": self.headless,
+ "verbose": self.verbose,
+ "loader_kwargs": self.loader_kwargs,
+ "llm_model": self.llm_model,
+ "embedder_model": self.embedder_model
+ }
+
self.set_common_params(common_params, overwrite=False)
def set_common_params(self, params: dict, overwrite=False):
@@ -79,7 +93,7 @@ class AbstractGraph(ABC):
for node in self.graph.nodes:
node.update_config(params, overwrite)
-
+
def _set_model_token(self, llm):
if 'Azure' in str(type(llm)):
@@ -157,7 +171,7 @@ class AbstractGraph(ABC):
raise KeyError("Model not supported") from exc
return Anthropic(llm_params)
elif "ollama" in llm_params["model"]:
- llm_params["model"] = llm_params["model"].split("/")[-1]
+ llm_params["model"] = llm_params["model"].split("ollama/")[-1]
# allow user to set model_tokens in config
try:
@@ -231,6 +245,8 @@ class AbstractGraph(ABC):
model="models/embedding-001")
if isinstance(self.llm_model, OpenAI):
return OpenAIEmbeddings(api_key=self.llm_model.openai_api_key)
+ elif isinstance(self.llm_model, DeepSeek):
+ return OpenAIEmbeddings(api_key=self.llm_model.openai_api_key)
elif isinstance(self.llm_model, AzureOpenAIEmbeddings):
return self.llm_model
elif isinstance(self.llm_model, AzureOpenAI):
@@ -271,7 +287,7 @@ class AbstractGraph(ABC):
elif "azure" in embedder_config["model"]:
return AzureOpenAIEmbeddings()
elif "ollama" in embedder_config["model"]:
- embedder_config["model"] = embedder_config["model"].split("/")[-1]
+ embedder_config["model"] = embedder_config["model"].split("ollama/")[-1]
try:
models_tokens["ollama"][embedder_config["model"]]
except KeyError as exc:
@@ -297,6 +313,10 @@ class AbstractGraph(ABC):
except KeyError as exc:
raise KeyError("Model not supported") from exc
return BedrockEmbeddings(client=client, model_id=embedder_config["model"])
+ else:
+ raise ValueError(
+ "Model provided by the configuration not supported")
+
def get_state(self, key=None) -> dict:
"""""
Get the final state of the graph.
@@ -334,4 +354,4 @@ class AbstractGraph(ABC):
"""
Abstract method to execute the graph and return the result.
"""
- pass
+ pass
\ No newline at end of file
diff --git a/scrapegraphai/graphs/pdf_scraper_graph.py b/scrapegraphai/graphs/pdf_scraper_graph.py
index 39278ab7..86ab2a49 100644
--- a/scrapegraphai/graphs/pdf_scraper_graph.py
+++ b/scrapegraphai/graphs/pdf_scraper_graph.py
@@ -11,7 +11,7 @@ from ..nodes import (
FetchNode,
ParseNode,
RAGNode,
- GenerateAnswerPDFNode
+ GenerateAnswerNode
)
@@ -48,7 +48,7 @@ class PDFScraperGraph(AbstractGraph):
"""
def __init__(self, prompt: str, source: str, config: dict, schema: Optional[str] = None):
- super().__init__(prompt, config, source)
+ super().__init__(prompt, config, source, schema)
self.input_key = "pdf" if source.endswith("pdf") else "pdf_dir"
@@ -64,21 +64,41 @@ class PDFScraperGraph(AbstractGraph):
input='pdf | pdf_dir',
output=["doc", "link_urls", "img_urls"],
)
- generate_answer_node_pdf = GenerateAnswerPDFNode(
+ parse_node = ParseNode(
+ input="doc",
+ output=["parsed_doc"],
+ node_config={
+ "chunk_size": self.model_token,
+ }
+ )
+ rag_node = RAGNode(
+ input="user_prompt & (parsed_doc | doc)",
+ output=["relevant_chunks"],
+ node_config={
+ "llm_model": self.llm_model,
+ "embedder_model": self.embedder_model,
+ }
+ )
+ generate_answer_node = GenerateAnswerNode(
input="user_prompt & (relevant_chunks | parsed_doc | doc)",
output=["answer"],
node_config={
"llm_model": self.llm_model,
+ "schema": self.schema,
}
)
return BaseGraph(
nodes=[
fetch_node,
- generate_answer_node_pdf,
+ parse_node,
+ rag_node,
+ generate_answer_node,
],
edges=[
- (fetch_node, generate_answer_node_pdf)
+ (fetch_node, parse_node),
+ (parse_node, rag_node),
+ (rag_node, generate_answer_node)
],
entry_point=fetch_node
)
@@ -94,4 +114,4 @@ class PDFScraperGraph(AbstractGraph):
inputs = {"user_prompt": self.prompt, self.input_key: self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
- return self.final_state.get("answer", "No answer found.")
+ return self.final_state.get("answer", "No answer found.")
\ No newline at end of file
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index ee230695..4093e49f 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -2,17 +2,14 @@
SmartScraperGraph Module
"""
-from typing import Optional
-
from .base_graph import BaseGraph
-from .abstract_graph import AbstractGraph
-
from ..nodes import (
FetchNode,
ParseNode,
RAGNode,
GenerateAnswerNode
)
+from .abstract_graph import AbstractGraph
class SmartScraperGraph(AbstractGraph):
@@ -25,7 +22,6 @@ class SmartScraperGraph(AbstractGraph):
prompt (str): The prompt for the graph.
source (str): The source of the graph.
config (dict): Configuration parameters for the graph.
- schema (str): The schema for the graph output.
llm_model: An instance of a language model client, configured for generating answers.
embedder_model: An instance of an embedding model client,
configured for generating embeddings.
@@ -36,7 +32,6 @@ class SmartScraperGraph(AbstractGraph):
prompt (str): The prompt for the graph.
source (str): The source of the graph.
config (dict): Configuration parameters for the graph.
- schema (str): The schema for the graph output.
Example:
>>> smart_scraper = SmartScraperGraph(
@@ -48,8 +43,8 @@ class SmartScraperGraph(AbstractGraph):
)
"""
- def __init__(self, prompt: str, source: str, config: dict, schema: Optional[str] = None):
- super().__init__(prompt, config, source, schema)
+ def __init__(self, prompt: str, source: str, config: dict):
+ super().__init__(prompt, config, source)
self.input_key = "url" if source.startswith("http") else "local_dir"
@@ -86,8 +81,7 @@ class SmartScraperGraph(AbstractGraph):
input="user_prompt & (relevant_chunks | parsed_doc | doc)",
output=["answer"],
node_config={
- "llm_model": self.llm_model,
- "schema": self.schema,
+ "llm_model": self.llm_model
}
)