Skip to content

Python scraper based on AI

add SearcInetnetNode

VinciGit00b8a3590235b8
diff --git a/scrapegraphai/nodes/search_internet_node.py b/scrapegraphai/nodes/search_internet_node.py
new file mode 100644
index 00000000..7b7a9b8e
--- /dev/null
+++ b/scrapegraphai/nodes/search_internet_node.py
@@ -0,0 +1,82 @@
+"""
+Module for generating the answer node
+"""
+from typing import List
+from ..utils.research_web import search_word_on_web
+from .base_node import BaseNode
+
+
+class SearchInternetNode(BaseNode):
+ """
+ A node that generates an answer by querying a language model (LLM) based on the user's input
+ and the content extracted from a webpage. It constructs a prompt from the user's input
+ and the scraped content, feeds it to the LLM, and parses the LLM's response to produce
+ an answer.
+
+ Attributes:
+ node_name (str): The unique identifier name for the node.
+ node_type (str): The type of the node, set to "node" indicating a standard operational node.
+ input (str): The user input used to construct the prompt.
+ output (List[str]): The keys in the state dictionary
+ where the generated answer will be stored.
+ model_config (dict): Configuration parameters for the language model client.
+
+ Args:
+ input (str): The user input used to construct the prompt.
+ output (List[str]): The keys in the state dictionary where the
+ generated answer will be stored.
+ model_config (dict): Configuration parameters for the language model client.
+ node_name (str, optional): The unique identifier name for the node.
+ Defaults to "GenerateAnswer".
+
+ Methods:
+ execute(state): Processes the input and document from the state to generate an answer,
+ updating the state with the generated answer under the 'answer' key.
+ """
+
+ def __init__(self, input: str, output: List[str], model_config: dict,
+ node_name: str = "SearchInternet"):
+ """
+ Initializes the SearchInternetNode with input, output, model configuration, and a node name.
+ Args:
+ input (str): The user input used to construct the prompt.
+ output (List[str]): The keys in the state dictionary where the
+ generated answer will be stored.
+ model_config (dict): Configuration parameters for the language model client.
+ node_name (str): The unique identifier name for the node.
+ """
+ super().__init__(node_name, "node", input, output, 1, model_config)
+
+ def execute(self, state):
+ """
+ Generates an answer by constructing a prompt from the user's input and the scraped
+ content, querying the language model, and parsing its response.
+
+ The method updates the state with the generated answer under the 'answer' key.
+
+ Args:
+ state (dict): The current state of the graph, expected to contain 'user_input',
+ and optionally 'parsed_document' or 'relevant_chunks' within 'keys'.
+
+ Returns:
+ dict: The updated state with the 'answer' key containing the generated answer.
+
+ Raises:
+ KeyError: If 'user_input' or 'document' is not found in the state, indicating
+ that the necessary information for generating an answer is missing.
+ """
+
+ print(f"--- Executing {self.node_name} Node ---")
+
+ input_keys = self.get_input_keys(state)
+
+ # Fetching data from the state based on the input keys
+ input_data = [state[key] for key in input_keys]
+
+ user_prompt = input_data[0]
+
+ answer = search_word_on_web(user_prompt)
+
+ # Update the state with the generated answer
+ state.update({self.output[0]: answer})
+ return state
diff --git a/scrapegraphai/utils/research_web.py b/scrapegraphai/utils/research_web.py
index a4bce5f5..dfb7659a 100644
--- a/scrapegraphai/utils/research_web.py
+++ b/scrapegraphai/utils/research_web.py
@@ -5,7 +5,7 @@ import re
from langchain_community.tools import DuckDuckGoSearchResults
-def search_word_on_google(word, max_results=10):
+def search_word_on_web(word, max_results=10):
"""
Function that given a word it finds it on the intenet
Args: