SerpApi#

class src.openCHA.tasks.serpapi.SerpAPI(*, name: str = 'serpapi', chat_name: str = 'InternetSearchSerp', description: str = 'A low-cost Google Search API.Useful for when you need to answer questions about current events.', dependencies: List[str] = [], inputs: List[str] = ['It should be a search query.'], outputs: List[str] = ["It returns a json object containing key: **url**. For example: {'url': 'http://google.com'}"], datapipe: DataPipe = None, output_type: bool = False, return_direct: bool = False, search_engine: Any = None, params: Dict = {'engine': 'google', 'gl': 'us', 'google_domain': 'google.com', 'hl': 'en'}, serpapi_api_key: str | None = None, aiosession: ClientSession | None = None)[source]#

Description:

This code defines a class named SerpAPI, which is a specific implementation of the abstract BaseTask class. The SerpAPI class represents a task that utilizes the SerpAPI (Google Search API) to perform internet searches and retrieve relevant information.

_execute(inputs: List[Any] = None) str[source]#

Run query through SerpAPI and parse result.

Parameters:

input (str) – The input, which should be a search query.

Returns:

The parsed result from the SerpAPI.

Return type:

str

static _process_response(res: Dict) str[source]#

Process response from SerpAPI.

Parameters:

res (Dict) – The raw response from the SerpAPI.

Returns:

Processed information from the SerpAPI response.

Return type:

str

explain() str[source]#

Provide an explanation of the task.

Returns:

Explanation of the SerpAPI task.

Return type:

str

get_params(query: str) Dict[str, str][source]#

Get parameters for SerpAPI.

Parameters:

query (str) – The search query.

Returns:

The parameters for the SerpAPI.

Return type:

Dict[str, str]

results(query: str) Dict[source]#

Run query through SerpAPI and return the raw result.

Parameters:

query (str) – The search query.

Returns:

The raw result from the SerpAPI.

Return type:

Dict

search_engine: Any#
classmethod validate_environment(values: Dict) Dict[source]#

Validate that api key and python package exists in environment.

Parameters:

values (Dict) – The dictionary of attribute values.

Returns:

The updated dictionary of attribute values.

Return type:

Dict

Raises:

ValueError – If the SerpAPI python package is not installed.