A node that generates a search query based on the user's input and searches the internet for relevant information. The node constructs a prompt for the language model, submits it, and processes the output to generate a search query. It then uses the search query to find relevant inf
| 14 | |
| 15 | |
| 16 | class SearchInternetNode(BaseNode): |
| 17 | """ |
| 18 | A node that generates a search query based on the user's input and searches the internet |
| 19 | for relevant information. The node constructs a prompt for the language model, submits it, |
| 20 | and processes the output to generate a search query. It then uses the search query to find |
| 21 | relevant information on the internet and updates the state with the generated answer. |
| 22 | |
| 23 | Attributes: |
| 24 | llm_model: An instance of the language model client used for generating search queries. |
| 25 | verbose (bool): A flag indicating whether to show print statements during execution. |
| 26 | |
| 27 | Args: |
| 28 | input (str): Boolean expression defining the input keys needed from the state. |
| 29 | output (List[str]): List of output keys to be updated in the state. |
| 30 | node_config (dict): Additional configuration for the node. |
| 31 | node_name (str): The unique identifier name for the node, defaulting to "SearchInternet". |
| 32 | """ |
| 33 | |
| 34 | def __init__( |
| 35 | self, |
| 36 | input: str, |
| 37 | output: List[str], |
| 38 | node_config: Optional[dict] = None, |
| 39 | node_name: str = "SearchInternet", |
| 40 | ): |
| 41 | super().__init__(node_name, "node", input, output, 1, node_config) |
| 42 | |
| 43 | self.llm_model = node_config["llm_model"] |
| 44 | self.verbose = ( |
| 45 | False if node_config is None else node_config.get("verbose", False) |
| 46 | ) |
| 47 | self.proxy = node_config.get("loader_kwargs", {}).get("proxy", None) |
| 48 | self.search_engine = ( |
| 49 | node_config["search_engine"] |
| 50 | if node_config.get("search_engine") |
| 51 | else "duckduckgo" |
| 52 | ) |
| 53 | |
| 54 | self.serper_api_key = ( |
| 55 | node_config["serper_api_key"] if node_config.get("serper_api_key") else None |
| 56 | ) |
| 57 | |
| 58 | self.max_results = node_config.get("max_results", 3) |
| 59 | |
| 60 | def execute(self, state: dict) -> dict: |
| 61 | """ |
| 62 | Generates an answer by constructing a prompt from the user's input and the scraped |
| 63 | content, querying the language model, and parsing its response. |
| 64 | |
| 65 | The method updates the state with the generated answer. |
| 66 | |
| 67 | Args: |
| 68 | state (dict): The current state of the graph. The input keys will be used to fetch the |
| 69 | correct data types from the state. |
| 70 | |
| 71 | Returns: |
| 72 | dict: The updated state with the output key containing the generated answer. |
| 73 |
no outgoing calls