|
| 1 | +""" |
| 2 | +SmartScraperMultiCondGraph Module with ConditionalNode |
| 3 | +""" |
| 4 | +from copy import deepcopy |
| 5 | +from typing import List, Optional |
| 6 | +from pydantic import BaseModel |
| 7 | +from .base_graph import BaseGraph |
| 8 | +from .abstract_graph import AbstractGraph |
| 9 | +from .smart_scraper_graph import SmartScraperGraph |
| 10 | +from ..nodes import ( |
| 11 | + GraphIteratorNode, |
| 12 | + MergeAnswersNode, |
| 13 | + ConcatAnswersNode, |
| 14 | + ConditionalNode |
| 15 | +) |
| 16 | +from ..utils.copy import safe_deepcopy |
| 17 | + |
| 18 | +class SmartScraperMultiCondGraph(AbstractGraph): |
| 19 | + """ |
| 20 | + SmartScraperMultiConditionalGraph is a scraping pipeline that scrapes a |
| 21 | + list of URLs and generates answers to a given prompt. |
| 22 | +
|
| 23 | + Attributes: |
| 24 | + prompt (str): The user prompt to search the internet. |
| 25 | + llm_model (dict): The configuration for the language model. |
| 26 | + embedder_model (dict): The configuration for the embedder model. |
| 27 | + headless (bool): A flag to run the browser in headless mode. |
| 28 | + verbose (bool): A flag to display the execution information. |
| 29 | + model_token (int): The token limit for the language model. |
| 30 | +
|
| 31 | + Args: |
| 32 | + prompt (str): The user prompt to search the internet. |
| 33 | + source (List[str]): The source of the graph. |
| 34 | + config (dict): Configuration parameters for the graph. |
| 35 | + schema (Optional[BaseModel]): The schema for the graph output. |
| 36 | +
|
| 37 | + Example: |
| 38 | + >>> search_graph = MultipleSearchGraph( |
| 39 | + ... "What is Chioggia famous for?", |
| 40 | + ... {"llm": {"model": "openai/gpt-3.5-turbo"}} |
| 41 | + ... ) |
| 42 | + >>> result = search_graph.run() |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, prompt: str, source: List[str], |
| 46 | + config: dict, schema: Optional[BaseModel] = None): |
| 47 | + |
| 48 | + self.max_results = config.get("max_results", 3) |
| 49 | + self.copy_config = safe_deepcopy(config) |
| 50 | + self.copy_schema = deepcopy(schema) |
| 51 | + |
| 52 | + super().__init__(prompt, config, source, schema) |
| 53 | + |
| 54 | + def _create_graph(self) -> BaseGraph: |
| 55 | + """ |
| 56 | + Creates the graph of nodes representing the workflow for web scraping and searching, |
| 57 | + including a ConditionalNode to decide between merging or concatenating the results. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + BaseGraph: A graph instance representing the web scraping and searching workflow. |
| 61 | + """ |
| 62 | + |
| 63 | + # Node that iterates over the URLs and collects results |
| 64 | + graph_iterator_node = GraphIteratorNode( |
| 65 | + input="user_prompt & urls", |
| 66 | + output=["results"], |
| 67 | + node_config={ |
| 68 | + "graph_instance": SmartScraperGraph, |
| 69 | + "scraper_config": self.copy_config, |
| 70 | + }, |
| 71 | + schema=self.copy_schema, |
| 72 | + node_name="GraphIteratorNode" |
| 73 | + ) |
| 74 | + |
| 75 | + # ConditionalNode to check if len(results) > 2 |
| 76 | + conditional_node = ConditionalNode( |
| 77 | + input="results", |
| 78 | + output=["results"], |
| 79 | + node_name="ConditionalNode", |
| 80 | + node_config={ |
| 81 | + 'key_name': 'results', |
| 82 | + 'condition': 'len(results) > 2' |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + merge_answers_node = MergeAnswersNode( |
| 87 | + input="user_prompt & results", |
| 88 | + output=["answer"], |
| 89 | + node_config={ |
| 90 | + "llm_model": self.llm_model, |
| 91 | + "schema": self.copy_schema |
| 92 | + }, |
| 93 | + node_name="MergeAnswersNode" |
| 94 | + ) |
| 95 | + |
| 96 | + concat_node = ConcatAnswersNode( |
| 97 | + input="results", |
| 98 | + output=["answer"], |
| 99 | + node_config={}, |
| 100 | + node_name="ConcatNode" |
| 101 | + ) |
| 102 | + |
| 103 | + # Build the graph |
| 104 | + return BaseGraph( |
| 105 | + nodes=[ |
| 106 | + graph_iterator_node, |
| 107 | + conditional_node, |
| 108 | + merge_answers_node, |
| 109 | + concat_node, |
| 110 | + ], |
| 111 | + edges=[ |
| 112 | + (graph_iterator_node, conditional_node), |
| 113 | + (conditional_node, merge_answers_node), # True node (len(results) > 2) |
| 114 | + (conditional_node, concat_node), # False node (len(results) <= 2) |
| 115 | + ], |
| 116 | + entry_point=graph_iterator_node, |
| 117 | + graph_name=self.__class__.__name__ |
| 118 | + ) |
| 119 | + |
| 120 | + def run(self) -> str: |
| 121 | + """ |
| 122 | + Executes the web scraping and searching process. |
| 123 | +
|
| 124 | + Returns: |
| 125 | + str: The answer to the prompt. |
| 126 | + """ |
| 127 | + inputs = {"user_prompt": self.prompt, "urls": self.source} |
| 128 | + self.final_state, self.execution_info = self.graph.execute(inputs) |
| 129 | + |
| 130 | + return self.final_state.get("answer", "No answer found.") |
0 commit comments