Retrieve information from custom queries using Serper.dev.
| 404 | |
| 405 | |
| 406 | class SerperRM(dspy.Retrieve): |
| 407 | """Retrieve information from custom queries using Serper.dev.""" |
| 408 | |
| 409 | def __init__( |
| 410 | self, |
| 411 | serper_search_api_key=None, |
| 412 | k=3, |
| 413 | query_params=None, |
| 414 | ENABLE_EXTRA_SNIPPET_EXTRACTION=False, |
| 415 | min_char_count: int = 150, |
| 416 | snippet_chunk_size: int = 1000, |
| 417 | webpage_helper_max_threads=10, |
| 418 | ): |
| 419 | """Args: |
| 420 | serper_search_api_key str: API key to run serper, can be found by creating an account on https://serper.dev/ |
| 421 | query_params (dict or list of dict): parameters in dictionary or list of dictionaries that has a max size of 100 that will be used to query. |
| 422 | Commonly used fields are as follows (see more information in https://serper.dev/playground): |
| 423 | q str: query that will be used with google search |
| 424 | type str: type that will be used for browsing google. Types are search, images, video, maps, places, etc. |
| 425 | gl str: Country that will be focused on for the search |
| 426 | location str: Country where the search will originate from. All locates can be found here: https://api.serper.dev/locations. |
| 427 | autocorrect bool: Enable autocorrect on the queries while searching, if query is misspelled, will be updated. |
| 428 | results int: Max number of results per page. |
| 429 | page int: Max number of pages per call. |
| 430 | tbs str: date time range, automatically set to any time by default. |
| 431 | qdr:h str: Date time range for the past hour. |
| 432 | qdr:d str: Date time range for the past 24 hours. |
| 433 | qdr:w str: Date time range for past week. |
| 434 | qdr:m str: Date time range for past month. |
| 435 | qdr:y str: Date time range for past year. |
| 436 | """ |
| 437 | super().__init__(k=k) |
| 438 | self.usage = 0 |
| 439 | self.query_params = None |
| 440 | self.ENABLE_EXTRA_SNIPPET_EXTRACTION = ENABLE_EXTRA_SNIPPET_EXTRACTION |
| 441 | self.webpage_helper = WebPageHelper( |
| 442 | min_char_count=min_char_count, |
| 443 | snippet_chunk_size=snippet_chunk_size, |
| 444 | max_thread_num=webpage_helper_max_threads, |
| 445 | ) |
| 446 | |
| 447 | if query_params is None: |
| 448 | self.query_params = {"num": k, "autocorrect": True, "page": 1} |
| 449 | else: |
| 450 | self.query_params = query_params |
| 451 | self.query_params.update({"num": k}) |
| 452 | self.serper_search_api_key = serper_search_api_key |
| 453 | if not self.serper_search_api_key and not os.environ.get("SERPER_API_KEY"): |
| 454 | raise RuntimeError( |
| 455 | "You must supply a serper_search_api_key param or set environment variable SERPER_API_KEY" |
| 456 | ) |
| 457 | |
| 458 | elif self.serper_search_api_key: |
| 459 | self.serper_search_api_key = serper_search_api_key |
| 460 | |
| 461 | else: |
| 462 | self.serper_search_api_key = os.environ["SERPER_API_KEY"] |
| 463 |