|
| 1 | +""" |
| 2 | +PGL Gateway search utilities |
| 3 | +""" |
| 4 | + |
| 5 | + |
| 6 | +# Standard library |
| 7 | +from typing import Any, Dict |
| 8 | + |
| 9 | +# Third-party |
| 10 | +import requests |
| 11 | + |
| 12 | + |
| 13 | +_DEFAULT_BASE_URL = "https://pgl-gateway.vercel.app" |
| 14 | +_DEFAULT_TIMEOUT = 30 |
| 15 | + |
| 16 | + |
| 17 | +class PGLSearchGateway: |
| 18 | + """ |
| 19 | + Thin client for the PGL Gateway search API. |
| 20 | +
|
| 21 | + Example: |
| 22 | + from pgl_utils.genai.search_gateway import PGLSearchGateway |
| 23 | +
|
| 24 | + gateway = PGLSearchGateway() |
| 25 | + results = gateway.search("inteligência artificial generativa") |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, base_url: str = _DEFAULT_BASE_URL, timeout: int = _DEFAULT_TIMEOUT): |
| 29 | + self.base_url = base_url.rstrip("/") |
| 30 | + self.timeout = timeout |
| 31 | + |
| 32 | + def search( |
| 33 | + self, |
| 34 | + q: str, |
| 35 | + search_depth: str = "advanced", |
| 36 | + include_raw_content: bool = True, |
| 37 | + **params: Any, |
| 38 | + ) -> Dict[str, Any]: |
| 39 | + """ |
| 40 | + Calls the GET /search endpoint of the PGL Gateway. |
| 41 | +
|
| 42 | + Args: |
| 43 | + q: Search query text. |
| 44 | + search_depth: "basic" or "advanced". |
| 45 | + include_raw_content: Whether to include each result's raw page content. |
| 46 | + **params: Extra query parameters forwarded to the gateway as-is. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + The parsed JSON response from the gateway. |
| 50 | + """ |
| 51 | + query = { |
| 52 | + "q": q, |
| 53 | + "search_depth": search_depth, |
| 54 | + "include_raw_content": include_raw_content, |
| 55 | + **params, |
| 56 | + } |
| 57 | + response = requests.get( |
| 58 | + f"{self.base_url}/search", |
| 59 | + params=query, |
| 60 | + headers={"accept": "application/json"}, |
| 61 | + timeout=self.timeout, |
| 62 | + ) |
| 63 | + response.raise_for_status() |
| 64 | + return response.json() |
0 commit comments