Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
refctoring of RAG node
VinciGit00f4d5941831b1
diff --git a/scrapegraphai/nodes/fetch_html_node.py b/scrapegraphai/nodes/fetch_html_node.pyindex f99bc049..71d1aa06 100644--- a/scrapegraphai/nodes/fetch_html_node.py+++ b/scrapegraphai/nodes/fetch_html_node.py@@ -33,14 +33,13 @@ class FetchHTMLNode(BaseNode):to succeed."""- def __init__(self, node_name: str, node_type: str = "fetch_node"):+ def __init__(self, node_name: str):"""Initializes the FetchHTMLNode with a node name and node type.Arguments:node_name (str): name of the node- node_type (str, optional): type of the node"""- super().__init__(node_name, node_type)+ super().__init__(node_name, "node")def execute(self, state: dict) -> dict:"""diff --git a/scrapegraphai/nodes/generate_answer_node.py b/scrapegraphai/nodes/generate_answer_node.pyindex 134e7fed..b07cb2c7 100644--- a/scrapegraphai/nodes/generate_answer_node.py+++ b/scrapegraphai/nodes/generate_answer_node.py@@ -32,15 +32,14 @@ class GenerateAnswerNode(BaseNode):updating the state with the generated answer under the 'answer' key."""- def __init__(self, llm, node_name: str, node_type: str = "GenerateAnswerNode"):+ def __init__(self, llm, node_name: str):"""Initializes the GenerateAnswerNode with a language model client and a node name.Args:llm (OpenAIImageToText): An instance of the OpenAIImageToText class.node_name (str): name of the node- node_type (str, optional): type of the node"""- super().__init__(node_name, node_type)+ super().__init__(node_name, "node")self.llm = llmdef execute(self, state: dict) -> dict:diff --git a/scrapegraphai/nodes/get_probable_tags_node.py b/scrapegraphai/nodes/get_probable_tags_node.pyindex 56f67402..fd5eff67 100644--- a/scrapegraphai/nodes/get_probable_tags_node.py+++ b/scrapegraphai/nodes/get_probable_tags_node.py@@ -29,15 +29,14 @@ class GetProbableTagsNode(BaseNode):probable HTML tags, updating the state with these tags under the 'tags' key."""- def __init__(self, llm, node_name: str, node_type: str = "GetPropbableTagsNode"):+ def __init__(self, llm, node_name: str):"""Initializes the GetProbableTagsNode with a language model client and a node name.Args:llm (OpenAIImageToText): An instance of the OpenAIImageToText class.node_name (str): name of the node- node_type (str, optional): type of the node"""- super().__init__(node_name, node_type)+ super().__init__(node_name, "node")self.llm = llmdef execute(self, state: dict):diff --git a/scrapegraphai/nodes/image_to_text_node.py b/scrapegraphai/nodes/image_to_text_node.pyindex 8c7b8ec0..703355d5 100644--- a/scrapegraphai/nodes/image_to_text_node.py+++ b/scrapegraphai/nodes/image_to_text_node.py@@ -16,14 +16,13 @@ class ImageToTextNode(BaseNode):execute(state, url): Execute the node's logic and return the updated state."""- def __init__(self, llm, node_name: str, node_type: str = "ImageToTextNode"):+ def __init__(self, llm, node_name: str):"""Initializes an instance of the ImageToTextNode class.Args:llm (OpenAIImageToText): An instance of the OpenAIImageToText class.node_name (str): name of the node- node_type (str, optional): type of the node"""super().__init__(node_name, "node")self.llm = llmdiff --git a/scrapegraphai/nodes/parse_html_node.py b/scrapegraphai/nodes/parse_html_node.pyindex f145e62e..8ec07244 100644--- a/scrapegraphai/nodes/parse_html_node.py+++ b/scrapegraphai/nodes/parse_html_node.py@@ -28,14 +28,14 @@ class ParseHTMLNode(BaseNode):the specified tags, if provided, and updates the state with the parsed content."""- def __init__(self, node_name: str, node_type: str = "ParseHTMLNode"):+ def __init__(self, node_name: str):"""Initializes the ParseHTMLNode with a node name.Args:node_name (str): name of the nodenode_type (str, optional): type of the node"""- super().__init__(node_name, node_type)+ super().__init__(node_name, "node")def execute(self, state):"""diff --git a/scrapegraphai/nodes/rag_node.py b/scrapegraphai/nodes/rag_node.pyindex 603bbc9f..a9c9eec9 100644--- a/scrapegraphai/nodes/rag_node.py+++ b/scrapegraphai/nodes/rag_node.py@@ -110,5 +110,5 @@ class RAGNode(BaseNode):compressed_docs = compression_retriever.get_relevant_documents(user_input)print("Documents compressed and stored in a vector database.")- state.update({"relevant_chunks": compressed_docs})+ state.update({"document_chunks": compressed_docs})return statediff --git a/tests/nodes/.env.example b/tests/nodes/.env.examplenew file mode 100644index 00000000..4c916f3e--- /dev/null+++ b/tests/nodes/.env.example@@ -0,0 +1 @@+PENAI_APIKEY="your openai.com api key"\ No newline at end of filediff --git a/tests/nodes/fetch_html_node_test.py b/tests/nodes/fetch_html_node_test.pynew file mode 100644index 00000000..5b7f21a5--- /dev/null+++ b/tests/nodes/fetch_html_node_test.py@@ -0,0 +1,48 @@+"""+Tests for the class FetchHTMLNode+"""+import os+import unittest+from dotenv import load_dotenv+from scrapegraphai.nodes import FetchHTMLNode++load_dotenv()+openai_key = os.getenv("OPENAI_APIKEY")+++class TestFetchHTMLNode(unittest.TestCase):+ """+ Class for testing the class FetchHTMLNode+ """++ def test_initialization(self):+ """+ Setup of the tests+ """++ node = FetchHTMLNode("test_node")+ self.assertEqual(node.node_name, "test_node")+ self.assertEqual(node.node_type, "node")++ def test_execute(self):+ """+ Execution of the test+ """+ node = FetchHTMLNode("test_node")+ state = {"url": "http://example.com"}+ updated_state = node.execute(state)+ self.assertIn("document", updated_state)+ self.assertTrue(updated_state["document"])++ def test_execute_missing_url(self):+ """+ Test for when there are missing url+ """+ node = FetchHTMLNode("test_node")+ state = {}+ with self.assertRaises(KeyError):+ node.execute(state)+++if __name__ == '__main__':+ unittest.main()