The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Harrison/serper api bug (#4902)
Harrison Chase9e2227ba11d3
diff --git a/langchain/utilities/google_serper.py b/langchain/utilities/google_serper.pyindex 0830a631d8..9db376c479 100644--- a/langchain/utilities/google_serper.py+++ b/langchain/utilities/google_serper.py@@ -5,6 +5,7 @@ import aiohttpimport requestsfrom pydantic.class_validators import root_validatorfrom pydantic.main import BaseModel+from typing_extensions import Literalfrom langchain.utils import get_from_dict_or_env@@ -28,7 +29,16 @@ class GoogleSerperAPIWrapper(BaseModel):k: int = 10gl: str = "us"hl: str = "en"- type: str = "search" # search, images, places, news+ # "places" and "images" is available from Serper but not implemented in the+ # parser of run(). They can be used in results()+ type: Literal["news", "search", "places", "images"] = "search"+ result_key_for_type = {+ "news": "news",+ "places": "places",+ "images": "images",+ "search": "organic",+ }+tbs: Optional[str] = Noneserper_api_key: Optional[str] = Noneaiosession: Optional[aiohttp.ClientSession] = None@@ -50,7 +60,7 @@ class GoogleSerperAPIWrapper(BaseModel):def results(self, query: str, **kwargs: Any) -> Dict:"""Run query through GoogleSearch."""- return self._google_serper_search_results(+ return self._google_serper_api_results(query,gl=self.gl,hl=self.hl,@@ -62,7 +72,7 @@ class GoogleSerperAPIWrapper(BaseModel):def run(self, query: str, **kwargs: Any) -> str:"""Run query through GoogleSearch and parse result."""- results = self._google_serper_search_results(+ results = self._google_serper_api_results(query,gl=self.gl,hl=self.hl,@@ -125,7 +135,7 @@ class GoogleSerperAPIWrapper(BaseModel):for attribute, value in kg.get("attributes", {}).items():snippets.append(f"{title} {attribute}: {value}.")- for result in results["organic"][: self.k]:+ for result in results[self.result_key_for_type[self.type]][: self.k]:if "snippet" in result:snippets.append(result["snippet"])for attribute, value in result.get("attributes", {}).items():@@ -138,7 +148,7 @@ class GoogleSerperAPIWrapper(BaseModel):def _parse_results(self, results: dict) -> str:return " ".join(self._parse_snippets(results))- def _google_serper_search_results(+ def _google_serper_api_results(self, search_term: str, search_type: str = "search", **kwargs: Any) -> dict:headers = {diff --git a/tests/integration_tests/utilities/test_googleserper_api.py b/tests/integration_tests/utilities/test_googleserper_api.pyindex 78f3275bfb..2fe4a5b7a7 100644--- a/tests/integration_tests/utilities/test_googleserper_api.py+++ b/tests/integration_tests/utilities/test_googleserper_api.py@@ -4,13 +4,20 @@ import pytestfrom langchain.utilities.google_serper import GoogleSerperAPIWrapper-def test_call() -> None:- """Test that call gives the correct answer."""+def test_search_call() -> None:+ """Test that call gives the correct answer from search."""search = GoogleSerperAPIWrapper()output = search.run("What was Obama's first name?")assert "Barack Hussein Obama II" in output+def test_news_call() -> None:+ """Test that call gives the correct answer from news search."""+ search = GoogleSerperAPIWrapper(type="news")+ output = search.run("What's new with stock market?").lower()+ assert "stock" in output or "market" in output++async def test_results() -> None:"""Test that call gives the correct answer."""search = GoogleSerperAPIWrapper()