langchain-serpbit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_serpbit/__init__.py +8 -0
- langchain_serpbit/py.typed +0 -0
- langchain_serpbit/search.py +131 -0
- langchain_serpbit-0.1.0.dist-info/METADATA +63 -0
- langchain_serpbit-0.1.0.dist-info/RECORD +8 -0
- langchain_serpbit-0.1.0.dist-info/WHEEL +5 -0
- langchain_serpbit-0.1.0.dist-info/licenses/LICENSE +21 -0
- langchain_serpbit-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Google Search for LangChain through Serpbit: Google's Custom Search tools, on after Custom Search stops searching the
|
|
2
|
+
whole web on 2027-01-01."""
|
|
3
|
+
|
|
4
|
+
from langchain_serpbit.search import SerpbitSearchAPIWrapper, SerpbitSearchResults, SerpbitSearchRun
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
|
|
8
|
+
__all__ = ["SerpbitSearchAPIWrapper", "SerpbitSearchRun", "SerpbitSearchResults", "__version__"]
|
|
File without changes
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""Google Search for LangChain through Serpbit: langchain-google-community's Google Search tools, the same calls and the
|
|
2
|
+
same answers, on Serpbit's Custom Search API in place of Google's, which stops searching the whole web on 2027-01-01."""
|
|
3
|
+
|
|
4
|
+
from typing import Any, Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
7
|
+
from langchain_core.tools import BaseTool
|
|
8
|
+
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
|
|
9
|
+
from pydantic import BaseModel, ConfigDict, Field, SecretStr, model_validator
|
|
10
|
+
from serpbit import Serpbit
|
|
11
|
+
|
|
12
|
+
NO_RESULT = "No good Google Search Result was found"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SerpbitSearchAPIWrapper(BaseModel):
|
|
16
|
+
"""Google Search through Serpbit, in place of langchain-google-community's ``GoogleSearchAPIWrapper``: ``run()``
|
|
17
|
+
and ``results()`` take and give what Google's did. The key is ``SERPBIT_API_KEY``'s by default (keys are made at
|
|
18
|
+
https://app.serpbit.com/keys). There is no search engine id: the results are the whole web's.
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
.. code-block:: python
|
|
22
|
+
|
|
23
|
+
from langchain_serpbit import SerpbitSearchAPIWrapper
|
|
24
|
+
|
|
25
|
+
search = SerpbitSearchAPIWrapper(k=5)
|
|
26
|
+
search.run("espresso grinder")
|
|
27
|
+
search.results("espresso grinder", 3, search_params={"gl": "de", "dateRestrict": "m1"})
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
serpbit_api_key: Optional[SecretStr] = None
|
|
31
|
+
"""Serpbit API key; ``SERPBIT_API_KEY`` by default."""
|
|
32
|
+
|
|
33
|
+
k: int = 10
|
|
34
|
+
"""Number of results ``run()`` reads, 1 to 10."""
|
|
35
|
+
|
|
36
|
+
siterestrict: bool = False
|
|
37
|
+
"""Kept for code written for Google's wrapper. ``True`` is refused: a Custom Search engine's own list of sites does
|
|
38
|
+
not carry over. Ask for one site with ``search_params={"siteSearch": "example.com"}``."""
|
|
39
|
+
|
|
40
|
+
base_url: Optional[str] = None
|
|
41
|
+
"""The API's address; ``SERPBIT_BASE_URL``, else https://api.serpbit.com."""
|
|
42
|
+
|
|
43
|
+
client: Any = None
|
|
44
|
+
"""The Serpbit client, made from the key when not given."""
|
|
45
|
+
|
|
46
|
+
model_config = ConfigDict(extra="forbid")
|
|
47
|
+
|
|
48
|
+
@model_validator(mode="before")
|
|
49
|
+
@classmethod
|
|
50
|
+
def validate_environment(cls, values: Dict) -> Any:
|
|
51
|
+
"""Refuse siterestrict, read the key and make the client."""
|
|
52
|
+
if values.get("siterestrict"):
|
|
53
|
+
raise ValueError(
|
|
54
|
+
"siterestrict searches a Custom Search engine's own sites, which do not carry over to Serpbit: "
|
|
55
|
+
"pass search_params={'siteSearch': 'example.com'} to search one site"
|
|
56
|
+
)
|
|
57
|
+
if values.get("client") is None:
|
|
58
|
+
key = convert_to_secret_str(get_from_dict_or_env(values, "serpbit_api_key", "SERPBIT_API_KEY"))
|
|
59
|
+
values["serpbit_api_key"] = key
|
|
60
|
+
values["client"] = Serpbit(key.get_secret_value(), base_url=values.get("base_url"))
|
|
61
|
+
return values
|
|
62
|
+
|
|
63
|
+
def _custom_search(self, query: str, **params: Any) -> List[dict]:
|
|
64
|
+
return self.client.custom_search(query, **params).get("items", [])
|
|
65
|
+
|
|
66
|
+
def run(self, query: str) -> str:
|
|
67
|
+
"""Run query through Google Search: the results' snippets, joined by spaces."""
|
|
68
|
+
results = self._custom_search(query, num=self.k)
|
|
69
|
+
if not results:
|
|
70
|
+
return NO_RESULT
|
|
71
|
+
return " ".join(r["snippet"] for r in results if "snippet" in r)
|
|
72
|
+
|
|
73
|
+
def results(
|
|
74
|
+
self,
|
|
75
|
+
query: str,
|
|
76
|
+
num_results: int,
|
|
77
|
+
search_params: Optional[Dict[str, str]] = None,
|
|
78
|
+
) -> List[Dict]:
|
|
79
|
+
"""Run query through Google Search and return each result's title, link and snippet.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
query: The query to search for.
|
|
83
|
+
num_results: Number of results to return, 1 to 10.
|
|
84
|
+
search_params: Custom Search's parameters: gl, lr, dateRestrict, siteSearch, start...
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
A dict a result, with title, link and snippet (when the result has one).
|
|
88
|
+
"""
|
|
89
|
+
results = self._custom_search(query, num=num_results, **(search_params or {}))
|
|
90
|
+
if not results:
|
|
91
|
+
return [{"Result": NO_RESULT}]
|
|
92
|
+
out = []
|
|
93
|
+
for r in results:
|
|
94
|
+
row = {"title": r["title"], "link": r["link"]}
|
|
95
|
+
if "snippet" in r:
|
|
96
|
+
row["snippet"] = r["snippet"]
|
|
97
|
+
out.append(row)
|
|
98
|
+
return out
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class SerpbitSearchRun(BaseTool):
|
|
102
|
+
"""Google Search through Serpbit, in place of langchain-google-community's ``GoogleSearchRun``: the same name and
|
|
103
|
+
description, so prompts and traces stay as they are. Returns the results' snippets."""
|
|
104
|
+
|
|
105
|
+
name: str = "google_search"
|
|
106
|
+
description: str = (
|
|
107
|
+
"A wrapper around Google Search. "
|
|
108
|
+
"Useful for when you need to answer questions about current events. "
|
|
109
|
+
"Input should be a search query."
|
|
110
|
+
)
|
|
111
|
+
api_wrapper: SerpbitSearchAPIWrapper = Field(default_factory=SerpbitSearchAPIWrapper)
|
|
112
|
+
|
|
113
|
+
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
|
|
114
|
+
return self.api_wrapper.run(query)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class SerpbitSearchResults(BaseTool):
|
|
118
|
+
"""Google Search through Serpbit, in place of langchain-google-community's ``GoogleSearchResults``: the same name
|
|
119
|
+
and description. Returns the first ``num_results`` results, each a title, link and snippet."""
|
|
120
|
+
|
|
121
|
+
name: str = "google_search_results_json"
|
|
122
|
+
description: str = (
|
|
123
|
+
"A wrapper around Google Search. "
|
|
124
|
+
"Useful for when you need to answer questions about current events. "
|
|
125
|
+
"Input should be a search query. Output is a JSON array of the query results"
|
|
126
|
+
)
|
|
127
|
+
num_results: int = 4
|
|
128
|
+
api_wrapper: SerpbitSearchAPIWrapper = Field(default_factory=SerpbitSearchAPIWrapper)
|
|
129
|
+
|
|
130
|
+
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
|
|
131
|
+
return str(self.api_wrapper.results(query, self.num_results))
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langchain-serpbit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Google Search for LangChain through Serpbit: GoogleSearchAPIWrapper's tools, on after Google's Custom Search API closes.
|
|
5
|
+
Author-email: Serpbit <support@serpbit.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://serpbit.com/custom-search-api-alternative/
|
|
8
|
+
Project-URL: Documentation, https://serpbit.com/docs/#sdks
|
|
9
|
+
Keywords: langchain,google search,custom search api,serp api,web search,ai agents
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: langchain-core<2,>=0.3
|
|
19
|
+
Requires-Dist: serpbit<1,>=0.3.0
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# langchain-serpbit
|
|
23
|
+
|
|
24
|
+
Google Search for LangChain after Google's Custom Search JSON API stops searching the whole web on January 1, 2027.
|
|
25
|
+
These are the Google Search tools of `langchain-google-community`, with the same calls, names and answers, served by
|
|
26
|
+
[Serpbit](https://serpbit.com/custom-search-api-alternative/) from Google's own results pages.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pip install langchain-serpbit
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Moving is two lines: the import and the key. Make a key at https://app.serpbit.com/keys and set `SERPBIT_API_KEY`.
|
|
33
|
+
The search engine id is gone, because the results come from the whole web.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
# Before
|
|
37
|
+
from langchain_google_community import GoogleSearchAPIWrapper, GoogleSearchResults, GoogleSearchRun
|
|
38
|
+
search = GoogleSearchAPIWrapper(k=5) # GOOGLE_API_KEY, GOOGLE_CSE_ID
|
|
39
|
+
|
|
40
|
+
# After
|
|
41
|
+
from langchain_serpbit import SerpbitSearchAPIWrapper, SerpbitSearchResults, SerpbitSearchRun
|
|
42
|
+
search = SerpbitSearchAPIWrapper(k=5) # SERPBIT_API_KEY
|
|
43
|
+
|
|
44
|
+
search.run("espresso grinder") # the results' snippets
|
|
45
|
+
search.results("espresso grinder", 3, search_params={"gl": "de", "dateRestrict": "m1"})
|
|
46
|
+
|
|
47
|
+
tools = [SerpbitSearchRun(api_wrapper=search), SerpbitSearchResults(api_wrapper=search, num_results=4)]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The tools keep Google's names, `google_search` and `google_search_results_json`, and Google's descriptions, so
|
|
51
|
+
prompts, traces and evaluations stay as they are. `search_params` are Custom Search's own: `gl`, `hl`, `lr`, `safe`,
|
|
52
|
+
`dateRestrict`, `siteSearch`, `fileType`, `start`, `exactTerms` and the rest
|
|
53
|
+
([the full list](https://serpbit.com/docs/#cse)).
|
|
54
|
+
|
|
55
|
+
What differs from Google's:
|
|
56
|
+
- `siterestrict=True` is refused, because a Custom Search engine's own list of sites does not carry over. To search one
|
|
57
|
+
site, pass `search_params={"siteSearch": "example.com"}`.
|
|
58
|
+
- `results()` has no `image` or `thumbnail`, because Serpbit's answer has no `pagemap`.
|
|
59
|
+
- A failed call raises the [`serpbit`](https://pypi.org/project/serpbit/) client's errors, such as
|
|
60
|
+
`serpbit.InsufficientCreditsError` when the credits run out.
|
|
61
|
+
- A search takes about 2 seconds, against under 1 on Google's API.
|
|
62
|
+
|
|
63
|
+
Each search costs 1 credit. The free plan gives 1,000 credits a month, with no card.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
langchain_serpbit/__init__.py,sha256=ln_c3MwyaJOSEvM-pjKJB2dwXs5ckfWiKJRNjI30CnY,372
|
|
2
|
+
langchain_serpbit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
langchain_serpbit/search.py,sha256=ZcawDHBTt0ZL2QrwPFwkNb7rnQD2TvrPlUG2X2C4ruw,5715
|
|
4
|
+
langchain_serpbit-0.1.0.dist-info/licenses/LICENSE,sha256=8_l_zG7x9IoJSOUDO9EWsYd5mUbic3B8FrL3-hsLQPE,1064
|
|
5
|
+
langchain_serpbit-0.1.0.dist-info/METADATA,sha256=yal6PGqDLbj4Vjg6TW-bGnuNb4D2a31Lww_vrK5wk_E,3035
|
|
6
|
+
langchain_serpbit-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
langchain_serpbit-0.1.0.dist-info/top_level.txt,sha256=x9CNxdToyXZDKRZOB1f2-7Flh09zi-D1qwMCsr1AB_I,18
|
|
8
|
+
langchain_serpbit-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Serpbit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
langchain_serpbit
|