|
| 1 | +""" |
| 2 | +OmniSearchGraph Module |
| 3 | +""" |
| 4 | + |
| 5 | +from copy import deepcopy |
| 6 | + |
| 7 | +from .base_graph import BaseGraph |
| 8 | +from ..nodes import ( |
| 9 | + SearchInternetNode, |
| 10 | + GraphIteratorNode, |
| 11 | + MergeAnswersNode |
| 12 | +) |
| 13 | +from .abstract_graph import AbstractGraph |
| 14 | +from .omni_scraper_graph import OmniScraperGraph |
| 15 | + |
| 16 | + |
| 17 | +class OmniSearchGraph(AbstractGraph): |
| 18 | + """ |
| 19 | + OmniSearchGraph is a scraping pipeline that searches the internet for answers to a given prompt. |
| 20 | + It only requires a user prompt to search the internet and generate an answer. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + prompt (str): The user prompt to search the internet. |
| 24 | + llm_model (dict): The configuration for the language model. |
| 25 | + embedder_model (dict): The configuration for the embedder model. |
| 26 | + headless (bool): A flag to run the browser in headless mode. |
| 27 | + verbose (bool): A flag to display the execution information. |
| 28 | + model_token (int): The token limit for the language model. |
| 29 | + max_results (int): The maximum number of results to return. |
| 30 | +
|
| 31 | + Args: |
| 32 | + prompt (str): The user prompt to search the internet. |
| 33 | + config (dict): Configuration parameters for the graph. |
| 34 | +
|
| 35 | + Example: |
| 36 | + >>> omni_search_graph = OmniSearchGraph( |
| 37 | + ... "What is Chioggia famous for?", |
| 38 | + ... {"llm": {"model": "gpt-4o"}} |
| 39 | + ... ) |
| 40 | + >>> result = search_graph.run() |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__(self, prompt: str, config: dict): |
| 44 | + |
| 45 | + self.max_results = config.get("max_results", 3) |
| 46 | + self.copy_config = deepcopy(config) |
| 47 | + |
| 48 | + super().__init__(prompt, config) |
| 49 | + |
| 50 | + def _create_graph(self) -> BaseGraph: |
| 51 | + """ |
| 52 | + Creates the graph of nodes representing the workflow for web scraping and searching. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + BaseGraph: A graph instance representing the web scraping and searching workflow. |
| 56 | + """ |
| 57 | + |
| 58 | + # ************************************************ |
| 59 | + # Create a OmniScraperGraph instance |
| 60 | + # ************************************************ |
| 61 | + |
| 62 | + omni_scraper_instance = OmniScraperGraph( |
| 63 | + prompt="", |
| 64 | + source="", |
| 65 | + config=self.copy_config |
| 66 | + ) |
| 67 | + |
| 68 | + # ************************************************ |
| 69 | + # Define the graph nodes |
| 70 | + # ************************************************ |
| 71 | + |
| 72 | + search_internet_node = SearchInternetNode( |
| 73 | + input="user_prompt", |
| 74 | + output=["urls"], |
| 75 | + node_config={ |
| 76 | + "llm_model": self.llm_model, |
| 77 | + "max_results": self.max_results |
| 78 | + } |
| 79 | + ) |
| 80 | + graph_iterator_node = GraphIteratorNode( |
| 81 | + input="user_prompt & urls", |
| 82 | + output=["results"], |
| 83 | + node_config={ |
| 84 | + "graph_instance": omni_scraper_instance, |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + merge_answers_node = MergeAnswersNode( |
| 89 | + input="user_prompt & results", |
| 90 | + output=["answer"], |
| 91 | + node_config={ |
| 92 | + "llm_model": self.llm_model, |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + return BaseGraph( |
| 97 | + nodes=[ |
| 98 | + search_internet_node, |
| 99 | + graph_iterator_node, |
| 100 | + merge_answers_node |
| 101 | + ], |
| 102 | + edges=[ |
| 103 | + (search_internet_node, graph_iterator_node), |
| 104 | + (graph_iterator_node, merge_answers_node) |
| 105 | + ], |
| 106 | + entry_point=search_internet_node |
| 107 | + ) |
| 108 | + |
| 109 | + def run(self) -> str: |
| 110 | + """ |
| 111 | + Executes the web scraping and searching process. |
| 112 | +
|
| 113 | + Returns: |
| 114 | + str: The answer to the prompt. |
| 115 | + """ |
| 116 | + inputs = {"user_prompt": self.prompt} |
| 117 | + self.final_state, self.execution_info = self.graph.execute(inputs) |
| 118 | + |
| 119 | + return self.final_state.get("answer", "No answer found.") |
0 commit comments