DuckDuckGoSearch News Tool (#8292)

Description: 
I wanted to use the DuckDuckGoSearch tool in an agent to let him get the
latest news for a topic. DuckDuckGoSearch has already an implemented
function for retrieving news articles. But there wasn't a tool to use
it. I simply adapted the SearchResult class with an extra argument
"backend". You can set it to "news" to only get news articles.

Furthermore, I added an example to the DuckDuckGo Notebook on how to
further customize the results by using the DuckDuckGoSearchAPIWrapper.

Dependencies: no new dependencies
---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Timon Palm
2023-07-26 20:30:01 +02:00
committed by GitHub
parent 8ce661d5a1
commit 70604e590f
3 changed files with 162 additions and 9 deletions

View File

@@ -56,6 +56,7 @@ class DuckDuckGoSearchResults(BaseTool):
api_wrapper: DuckDuckGoSearchAPIWrapper = Field(
default_factory=DuckDuckGoSearchAPIWrapper
)
backend: str = "api"
def _run(
self,
@@ -63,7 +64,9 @@ class DuckDuckGoSearchResults(BaseTool):
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
"""Use the tool."""
return str(self.api_wrapper.results(query, self.num_results))
res = self.api_wrapper.results(query, self.num_results, backend=self.backend)
res_strs = [", ".join([f"{k}: {v}" for k, v in d.items()]) for d in res]
return ", ".join([f"[{rs}]" for rs in res_strs])
async def _arun(
self,

View File

@@ -62,7 +62,9 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
snippets = self.get_snippets(query)
return " ".join(snippets)
def results(self, query: str, num_results: int) -> List[Dict[str, str]]:
def results(
self, query: str, num_results: int, backend: str = "api"
) -> List[Dict[str, str]]:
"""Run query through DuckDuckGo and return metadata.
Args:
@@ -83,11 +85,20 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
region=self.region,
safesearch=self.safesearch,
timelimit=self.time,
backend=backend,
)
if results is None:
return [{"Result": "No good DuckDuckGo Search Result was found"}]
def to_metadata(result: Dict) -> Dict[str, str]:
if backend == "news":
return {
"date": result["date"],
"title": result["title"],
"snippet": result["body"],
"source": result["source"],
"link": result["url"],
}
return {
"snippet": result["body"],
"title": result["title"],