-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
searx: implement async + helper tool providing json results (#2129)
- implemented `arun` and `aresults`. Reuses aiosession if available. - helper tools `SearxSearchRun` and `SearxSearchResults` - update doc Co-authored-by: blob42 <spike@w530>
- Loading branch information
Showing
5 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Tool for the SearxNG search API.""" | ||
from pydantic import Extra | ||
|
||
from langchain.tools.base import BaseTool | ||
from langchain.utilities.searx_search import SearxSearchWrapper | ||
|
||
|
||
class SearxSearchRun(BaseTool): | ||
"""Tool that adds the capability to query a Searx instance.""" | ||
|
||
name = "Searx Search" | ||
description = ( | ||
"A meta search engine." | ||
"Useful for when you need to answer questions about current events." | ||
"Input should be a search query." | ||
) | ||
wrapper: SearxSearchWrapper | ||
|
||
def _run(self, query: str) -> str: | ||
"""Use the tool.""" | ||
return self.wrapper.run(query) | ||
|
||
async def _arun(self, query: str) -> str: | ||
"""Use the tool asynchronously.""" | ||
return await self.wrapper.arun(query) | ||
|
||
|
||
class SearxSearchResults(BaseTool): | ||
"""Tool that has capability to query a Searx instance and get back json.""" | ||
|
||
name = "Searx Search" | ||
description = ( | ||
"A meta search engine." | ||
"Useful for when you need to answer questions about current events." | ||
"Input should be a search query. Output is a JSON array of the query results" | ||
) | ||
wrapper: SearxSearchWrapper | ||
num_results: int = 4 | ||
|
||
class Config: | ||
"""Pydantic config.""" | ||
|
||
extra = Extra.allow | ||
|
||
def _run(self, query: str) -> str: | ||
"""Use the tool.""" | ||
return str(self.wrapper.results(query, self.num_results)) | ||
|
||
async def _arun(self, query: str) -> str: | ||
"""Use the tool asynchronously.""" | ||
return (await self.wrapper.aresults(query, self.num_results)).__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters