devstein / langchain

⚡ Building applications with LLMs through composability ⚡
MIT License
1 stars 0 forks source link

Issue: Can not configure serpapi base url via env #4

Open devstein opened 1 year ago

devstein commented 1 year ago

Issue you'd like to raise.

Currently, the base URL of Serpapi is been hard coded.

While some search services(e.g. Bing, BING_SEARCH_URL) are configurable.

In some companies, the original can not be allowed to access. We need to use nginx redirect proxy.

So we need to make the base URL configurable via env.

Suggestion:

Make serpapi base url configurable via env

dosubot[bot] commented 1 year ago

File: langchain/utilities/serpapi.py

To make the SerpAPI base URL configurable via environment variables, you can update the aresults method in the SerpAPIWrapper class. Replace the hardcoded URL with a variable that gets the value from the environment variable or uses the default value if the environment variable is not set.

Here's how you can update the file:

  1. Import the os module at the beginning of the file if it's not already imported.

  2. Update the aresults method to get the base URL from the environment variable:

async def aresults(self, query: str) -> dict:
    """Use aiohttp to run query through SerpAPI and return the results async."""

    def construct_url_and_params() -> Tuple[str, Dict[str, str]]:
        params = self.get_params(query)
        params["source"] = "python"
        if self.serpapi_api_key:
            params["serp_api_key"] = self.serpapi_api_key
        params["output"] = "json"
        # Get the base URL from the environment variable or use the default value
        base_url = os.environ.get("SERPAPI_BASE_URL", "https://serpapi.com")
        url = f"{base_url}/search"
        return url, params

    url, params = construct_url_and_params()
    if not self.aiosession:
        async with aiohttp.ClientSession() as session:
            async with session.get(url, params=params) as response:
                res = await response.json()
    else:
        async with self.aiosession.get(url, params=params) as response:
            res = await response.json()

    return res

Now, the SerpAPI base URL can be configured via the SERPAPI_BASE_URL environment variable. If the environment variable is not set, it will use the default value "https://serpapi.com".