forked from Archives/langchain
searx: implement async and extra tool providing json results
- implemented `arun` and `aresults`. Reuses aiosession if available. - helper tools `SearxSearchRun` and `SearxSearchResults` - update docsearx
parent
e2c26909f2
commit
5f698928b2
@ -0,0 +1,49 @@
|
|||||||
|
from langchain.tools.base import BaseTool
|
||||||
|
from langchain.utilities.searx_search import SearxSearchWrapper
|
||||||
|
from typing import Optional, List
|
||||||
|
from pydantic import Extra
|
||||||
|
|
||||||
|
|
||||||
|
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__()
|
Loading…
Reference in New Issue