Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions promptulate/tools/duckduckgo/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
safe_search: str = "moderate"
time: Optional[str] = "y"
max_num_of_results: int = 5

proxies: Optional[str] = None
class Config:
"""Configuration for this pydantic object."""

Expand All @@ -37,7 +37,7 @@ def query(
if not num_results:
num_results = self.max_num_of_results

with DDGS() as ddgs:
with DDGS(self.proxies) as ddgs:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duckduckgo_search==3.9.11 defines DDGS(headers=None, proxies=None, timeout=10), so DDGS(self.proxies) binds the proxy string to headers, not proxies. This means the proxy is never applied. Please pass it explicitly as DDGS(proxies=self.proxies) here and in the other call site.

results = ddgs.text(
keyword,
region=self.region,
Expand Down Expand Up @@ -74,7 +74,7 @@ def query_by_formatted_results(
if not num_results:
num_results = self.max_num_of_results

with DDGS() as ddgs:
with DDGS(self.proxies) as ddgs:
results = ddgs.text(
query,
region=self.region,
Expand Down
7 changes: 6 additions & 1 deletion promptulate/tools/duckduckgo/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def _run(self, keyword: str, **kwargs) -> Union[str, List[str]]:
result_type="original"
"""
from duckduckgo_search.exceptions import RateLimitException

if "proxies" in kwargs:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mutates self.api_wrapper.proxies only when kwargs["proxies"] is truthy, so once one call sets a proxy, later calls on the same tool instance will keep using that stale proxy unless the process is restarted. It also makes proxies=None unable to clear the setting. Please thread proxies through per call, or at least assign self.api_wrapper.proxies = kwargs.get("proxies") before the query.

if kwargs["proxies"]:
self.api_wrapper.proxies = kwargs["proxies"]
attempt = 0
while attempt < self.max_retry:
try:
Expand Down Expand Up @@ -80,6 +82,9 @@ def _run(self, keyword: str, **kwargs) -> Union[str, List[Dict[str, str]]]:
default is string. Return List[Dict[str, str]] type data if you pass
result_type="original"
"""
if "proxies" in kwargs:
if kwargs["proxies"]:
self.api_wrapper.proxies = kwargs["proxies"]
result = self.api_wrapper.query_by_formatted_results(keyword, **kwargs)
if "return_type" in kwargs and kwargs["return_type"] == "original":
return result
Expand Down