Source code for langchain.utilities.searchapi
from typing import Any, Dict, Optional
import aiohttp
import requests
from langchain.pydantic_v1 import BaseModel, root_validator
from langchain.utils import get_from_dict_or_env
[docs]class SearchApiAPIWrapper(BaseModel):
    """
    Wrapper around SearchApi API.
    To use, you should have the environment variable ``SEARCHAPI_API_KEY``
    set with your API key, or pass `searchapi_api_key`
    as a named parameter to the constructor.
    Example:
        .. code-block:: python
            from langchain.utilities import SearchApiAPIWrapper
            searchapi = SearchApiAPIWrapper()
    """
    # Use "google" engine by default.
    # Full list of supported ones can be found in https://www.searchapi.io docs
    engine: str = "google"
    searchapi_api_key: Optional[str] = None
    aiosession: Optional[aiohttp.ClientSession] = None
    class Config:
        """Configuration for this pydantic object."""
        arbitrary_types_allowed = True
    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that API key exists in environment."""
        searchapi_api_key = get_from_dict_or_env(
            values, "searchapi_api_key", "SEARCHAPI_API_KEY"
        )
        values["searchapi_api_key"] = searchapi_api_key
        return values
[docs]    def run(self, query: str, **kwargs: Any) -> str:
        results = self.results(query, **kwargs)
        return self._result_as_string(results)
[docs]    async def arun(self, query: str, **kwargs: Any) -> str:
        results = await self.aresults(query, **kwargs)