Skip to content

Commit

Permalink
feat: use latest duckduckgo_search API to call (langchain-ai#6409)
Browse files Browse the repository at this point in the history
# Provider the latest duckduckgo_search API

The Git commit contents involve two files related to some DuckDuckGo
query operations, and an upgrade of the DuckDuckGo module to version
3.8.3. A suitable commit message could be "Upgrade DuckDuckGo module to
version 3.8.3, including query operations". Specifically, in the
duckduckgo_search.py file, a DDGS() class instance is newly added to
replace the previous ddg() function, and the time parameter name in the
get_snippets() and results() methods is changed from "time" to
"timelimit" to accommodate recent changes. In the pyproject.toml file,
the duckduckgo-search module is upgraded to version 3.8.3.

[duckduckgo_search readme
attention](https://github.com/deedy5/duckduckgo_search): Versions before
v2.9.4 no longer work as of May 12, 2023

## Who can review?

@vowelparrot

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
  • Loading branch information
Undertone0809 and hwchase17 committed Jun 20, 2023
1 parent 9eec7c3 commit 8a604b9
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 40 deletions.
76 changes: 42 additions & 34 deletions langchain/utilities/duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ def validate_environment(cls, values: Dict) -> Dict:

def get_snippets(self, query: str) -> List[str]:
"""Run query through DuckDuckGo and return concatenated results."""
from duckduckgo_search import ddg

results = ddg(
query,
region=self.region,
safesearch=self.safesearch,
time=self.time,
max_results=self.max_results,
)
if results is None or len(results) == 0:
return ["No good DuckDuckGo Search Result was found"]
snippets = [result["body"] for result in results]
return snippets
from duckduckgo_search import DDGS

with DDGS() as ddgs:
results = ddgs.text(
query,
region=self.region,
safesearch=self.safesearch,
timelimit=self.time,
)
if results is None or next(results, None) is None:
return ["No good DuckDuckGo Search Result was found"]
snippets = []
for i, res in enumerate(results, 1):
snippets.append(res["body"])
if i == self.max_results:
break
return snippets

def run(self, query: str) -> str:
snippets = self.get_snippets(query)
Expand All @@ -71,24 +75,28 @@ def results(self, query: str, num_results: int) -> List[Dict[str, str]]:
title - The title of the result.
link - The link to the result.
"""
from duckduckgo_search import ddg

results = ddg(
query,
region=self.region,
safesearch=self.safesearch,
time=self.time,
max_results=num_results,
)

if results is None or len(results) == 0:
return [{"Result": "No good DuckDuckGo Search Result was found"}]

def to_metadata(result: Dict) -> Dict[str, str]:
return {
"snippet": result["body"],
"title": result["title"],
"link": result["href"],
}

return [to_metadata(result) for result in results]
from duckduckgo_search import DDGS

with DDGS() as ddgs:
results = ddgs.text(
query,
region=self.region,
safesearch=self.safesearch,
timelimit=self.time,
)
if results is None or next(results, None) is None:
return [{"Result": "No good DuckDuckGo Search Result was found"}]

def to_metadata(result: Dict) -> Dict[str, str]:
return {
"snippet": result["body"],
"title": result["title"],
"link": result["href"],
}

formatted_results = []
for i, res in enumerate(results, 1):
formatted_results.append(to_metadata(res))
if i == num_results:
break
return formatted_results
Loading

0 comments on commit 8a604b9

Please sign in to comment.