Skip to content

Commit 539142f

Browse files
authored
Add way to get serpapi results async (langchain-ai#3604)
Sometimes it's nice to get the raw results from serpapi, and we're missing the async version of this function.
1 parent 443a893 commit 539142f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

langchain/utilities/serpapi.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,23 @@ def validate_environment(cls, values: Dict) -> Dict:
7777
return values
7878

7979
async def arun(self, query: str) -> str:
80-
"""Use aiohttp to run query through SerpAPI and parse result."""
80+
"""Run query through SerpAPI and parse result async."""
81+
return self._process_response(await self.aresults(query))
82+
83+
def run(self, query: str) -> str:
84+
"""Run query through SerpAPI and parse result."""
85+
return self._process_response(self.results(query))
86+
87+
def results(self, query: str) -> dict:
88+
"""Run query through SerpAPI and return the raw result."""
89+
params = self.get_params(query)
90+
with HiddenPrints():
91+
search = self.search_engine(params)
92+
res = search.get_dict()
93+
return res
94+
95+
async def aresults(self, query: str) -> dict:
96+
"""Use aiohttp to run query through SerpAPI and return the results async."""
8197

8298
def construct_url_and_params() -> Tuple[str, Dict[str, str]]:
8399
params = self.get_params(query)
@@ -97,18 +113,6 @@ def construct_url_and_params() -> Tuple[str, Dict[str, str]]:
97113
async with self.aiosession.get(url, params=params) as response:
98114
res = await response.json()
99115

100-
return self._process_response(res)
101-
102-
def run(self, query: str) -> str:
103-
"""Run query through SerpAPI and parse result."""
104-
return self._process_response(self.results(query))
105-
106-
def results(self, query: str) -> dict:
107-
"""Run query through SerpAPI and return the raw result."""
108-
params = self.get_params(query)
109-
with HiddenPrints():
110-
search = self.search_engine(params)
111-
res = search.get_dict()
112116
return res
113117

114118
def get_params(self, query: str) -> Dict[str, str]:

0 commit comments

Comments
 (0)