Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
added openai wrapper and examples
PeriniM3ed5fd5e489a
diff --git a/.gitignore b/.gitignoreindex 45159e0c..c8faf323 100644--- a/.gitignore+++ b/.gitignore@@ -21,3 +21,6 @@ docs/source/_static/.envvenv/.vscode/++# exclude pdfdiff --git a/examples/custom_graph_example.py b/examples/custom_graph_example.pyindex 91d865ba..e65e3eeb 100644--- a/examples/custom_graph_example.py+++ b/examples/custom_graph_example.py@@ -4,25 +4,21 @@ Example of custom graph using existing nodesimport osfrom dotenv import load_dotenv--from langchain_openai import ChatOpenAI+from scrapegraphai.models import OpenAIfrom scrapegraphai.graphs import BaseGraphfrom scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode-# load the environment variablesload_dotenv()-openai_key = os.getenv("OPENAI_APIKEY")-if not openai_key:- print("Error: OpenAI API key not found in environment variables.")# Define the configuration for the language model+openai_key = os.getenv("OPENAI_APIKEY")llm_config = {"api_key": openai_key,"model_name": "gpt-3.5-turbo","temperature": 0,"streaming": True}-model = ChatOpenAI(**llm_config)+model = OpenAI(llm_config)# define the nodes for the graphfetch_html_node = FetchHTMLNode("fetch_html")diff --git a/examples/graph_builder_example.py b/examples/graph_builder_example.pyindex f006404e..dbf45b62 100644--- a/examples/graph_builder_example.py+++ b/examples/graph_builder_example.py@@ -3,11 +3,9 @@ from dotenv import load_dotenvfrom scrapegraphai.builders import GraphBuilderload_dotenv()-openai_key = os.getenv("OPENAI_APIKEY")-if not openai_key:- print("Error: OpenAI API key not found in environment variables.")# Define the configuration for the language model+openai_key = os.getenv("OPENAI_APIKEY")llm_config = {"api_key": openai_key,"model_name": "gpt-3.5-turbo",@@ -16,15 +14,12 @@ llm_config = {}# Example usage of GraphBuilder-user_prompt = "Give me all the titles of the page"+user_prompt = "Extract the news and generate a text summary with a voiceover."graph_builder = GraphBuilder(user_prompt, llm_config)graph_json = graph_builder.build_graph()# Convert the resulting JSON to Graphviz formatgraphviz_graph = graph_builder.convert_json_to_graphviz(graph_json)-# save on current directory-curr_dir = os.path.dirname(os.path.realpath(__file__))-file_path = os.path.join(curr_dir, 'custom_graph')--graphviz_graph.render(file_path, view=True)+# Save the graph to a file and open it in the default viewer+graphviz_graph.render('ScrapeGraphAI_generated_graph', view=True)diff --git a/examples/smart_scraper_example.py b/examples/smart_scraper_example.pyindex 312b79b3..3e4601d2 100644--- a/examples/smart_scraper_example.py+++ b/examples/smart_scraper_example.py@@ -1,15 +1,15 @@"""Basic example of scraping pipeline using SmartScraper"""+import osfrom dotenv import load_dotenvfrom scrapegraphai.graphs import SmartScraperload_dotenv()+# Define the configuration for the language modelopenai_key = os.getenv("OPENAI_APIKEY")-if not openai_key:- print("Error: OpenAI API key not found in environment variables.")llm_config = {"api_key": openai_key,"model_name": "gpt-3.5-turbo",diff --git a/scrapegraphai/builders/graph_builder.py b/scrapegraphai/builders/graph_builder.pyindex 087a7f26..0954bbfb 100644--- a/scrapegraphai/builders/graph_builder.py+++ b/scrapegraphai/builders/graph_builder.py@@ -1,7 +1,7 @@from langchain_core.prompts import ChatPromptTemplatefrom langchain.chains import create_extraction_chain-from langchain_openai import ChatOpenAI-from scrapegraphai.utils import nodes_metadata, graph_schema+from ..models import OpenAI+from ..utils import nodes_metadata, graph_schemaclass GraphBuilder:"""@@ -59,8 +59,8 @@ class GraphBuilder:# Ensure the api_key is set, raise an error if it's notif "api_key" not in llm_params:raise ValueError("LLM configuration must include an 'api_key'.")- # Create the ChatOpenAI instance with the provided and default parameters- return ChatOpenAI(**llm_params)+ # Create the OpenAI instance with the provided and default parameters+ return OpenAI(llm_params)def _generate_nodes_description(self):"""@@ -114,7 +114,7 @@ class GraphBuilder:graphviz.Digraph: A Graphviz object representing the graph configuration."""import graphviz-+graph = graphviz.Digraph(comment='ScrapeGraphAI Generated Graph', format=format,node_attr={'color': 'lightblue2', 'style': 'filled'})diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.pyindex caaf9f05..bae35616 100644--- a/scrapegraphai/graphs/smart_scraper_graph.py+++ b/scrapegraphai/graphs/smart_scraper_graph.py@@ -1,4 +1,4 @@-from langchain_openai import ChatOpenAI+from ..models import OpenAIfrom .base_graph import BaseGraphfrom ..nodes import (FetchHTMLNode,@@ -64,7 +64,7 @@ class SmartScraper:if "api_key" not in llm_params:raise ValueError("LLM configuration must include an 'api_key'.")# Create the ChatOpenAI instance with the provided and default parameters- return ChatOpenAI(**llm_params)+ return OpenAI(llm_params)def _create_graph(self):"""diff --git a/scrapegraphai/models/__init__.py b/scrapegraphai/models/__init__.pyindex 0639abe6..b9aa68be 100644--- a/scrapegraphai/models/__init__.py+++ b/scrapegraphai/models/__init__.py@@ -1,3 +1,5 @@"""__init__.py file for models folder"""++from .openai import OpenAI\ No newline at end of filediff --git a/scrapegraphai/models/openai.py b/scrapegraphai/models/openai.pynew file mode 100644index 00000000..887cc728--- /dev/null+++ b/scrapegraphai/models/openai.py@@ -0,0 +1,13 @@+from langchain_openai import ChatOpenAI++class OpenAI(ChatOpenAI):+ def __init__(self, llm_config: dict):+ """+ A wrapper for the ChatOpenAI class that provides default configuration+ and could be extended with additional methods if needed.++ Args:+ llm_config (dict): Configuration parameters for the language model.+ """+ # Initialize the superclass (ChatOpenAI) with provided config parameters+ super().__init__(**llm_config)diff --git a/scrapegraphai/nodes/image_to_text_node.py b/scrapegraphai/nodes/image_to_text_node.pyindex 32ccb120..d75c40fc 100644--- a/scrapegraphai/nodes/image_to_text_node.py+++ b/scrapegraphai/nodes/image_to_text_node.py@@ -63,8 +63,7 @@ class ImageToTextNode(BaseNode):url (str): url of the image where to:return: The updated state after executing this node."""- # Da fixare-+if not self.llm.model_name == "gpt-4-vision-preview":raise ValueError("Model is not gpt-4-vision-preview")