|
| 1 | +""" |
| 2 | +GraphIterator Module |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import List, Optional |
| 6 | +import copy |
| 7 | +from tqdm import tqdm |
| 8 | +from .base_node import BaseNode |
| 9 | + |
| 10 | + |
| 11 | +class GraphIteratorNode(BaseNode): |
| 12 | + """ |
| 13 | + A node responsible for parsing HTML content from a document. |
| 14 | + The parsed content is split into chunks for further processing. |
| 15 | +
|
| 16 | + This node enhances the scraping workflow by allowing for targeted extraction of |
| 17 | + content, thereby optimizing the processing of large HTML documents. |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + verbose (bool): A flag indicating whether to show print statements during execution. |
| 21 | +
|
| 22 | + Args: |
| 23 | + input (str): Boolean expression defining the input keys needed from the state. |
| 24 | + output (List[str]): List of output keys to be updated in the state. |
| 25 | + node_config (dict): Additional configuration for the node. |
| 26 | + node_name (str): The unique identifier name for the node, defaulting to "Parse". |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, input: str, output: List[str], node_config: Optional[dict]=None, node_name: str = "GraphIterator"): |
| 30 | + super().__init__(node_name, "node", input, output, 2, node_config) |
| 31 | + |
| 32 | + self.verbose = False if node_config is None else node_config.get("verbose", False) |
| 33 | + |
| 34 | + def execute(self, state: dict) -> dict: |
| 35 | + """ |
| 36 | + Executes the node's logic to parse the HTML document content and split it into chunks. |
| 37 | +
|
| 38 | + Args: |
| 39 | + state (dict): The current state of the graph. The input keys will be used to fetch the |
| 40 | + correct data from the state. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + dict: The updated state with the output key containing the parsed content chunks. |
| 44 | +
|
| 45 | + Raises: |
| 46 | + KeyError: If the input keys are not found in the state, indicating that the |
| 47 | + necessary information for parsing the content is missing. |
| 48 | + """ |
| 49 | + |
| 50 | + if self.verbose: |
| 51 | + print(f"--- Executing {self.node_name} Node ---") |
| 52 | + |
| 53 | + # Interpret input keys based on the provided input expression |
| 54 | + input_keys = self.get_input_keys(state) |
| 55 | + |
| 56 | + # Fetching data from the state based on the input keys |
| 57 | + input_data = [state[key] for key in input_keys] |
| 58 | + |
| 59 | + user_prompt = input_data[0] |
| 60 | + urls = input_data[1] |
| 61 | + |
| 62 | + graph_instance = self.node_config.get("graph_instance", None) |
| 63 | + if graph_instance is None: |
| 64 | + raise ValueError("Graph instance is required for graph iteration.") |
| 65 | + |
| 66 | + # set the prompt and source for each url |
| 67 | + graph_instance.prompt = user_prompt |
| 68 | + graphs_instances = [] |
| 69 | + for url in urls: |
| 70 | + # make a copy of the graph instance |
| 71 | + copy_graph_instance = copy.copy(graph_instance) |
| 72 | + copy_graph_instance.source = url |
| 73 | + graphs_instances.append(copy_graph_instance) |
| 74 | + |
| 75 | + # run the graph for each url and use tqdm for progress bar |
| 76 | + graphs_answers = [] |
| 77 | + for graph in tqdm(graphs_instances, desc="Processing Graph Instances", disable=not self.verbose): |
| 78 | + result = graph.run() |
| 79 | + graphs_answers.append(result) |
| 80 | + |
| 81 | + state.update({self.output[0]: graphs_answers}) |
| 82 | + |
| 83 | + return state |
0 commit comments