Skip to content

Commit

Permalink
Support backoff and retry when encountering `duckduckgo_search.except…
Browse files Browse the repository at this point in the history
…ions.RatelimitException`.
  • Loading branch information
shaoyijia committed Sep 9, 2024
1 parent f78a073 commit d50511f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions knowledge_storm/rm.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import os
from typing import Callable, Union, List

import backoff
import dspy
import requests
from dsp import backoff_hdlr, giveup_hdlr

from langchain_huggingface import HuggingFaceEmbeddings
from langchain_qdrant import Qdrant
Expand Down Expand Up @@ -684,6 +687,20 @@ def get_usage_and_reset(self):
self.usage = 0
return {"DuckDuckGoRM": usage}

@backoff.on_exception(
backoff.expo,
(Exception,),
max_time=1000,
max_tries=8,
on_backoff=backoff_hdlr,
giveup=giveup_hdlr,
)
def request(self, query: str):
results = self.ddgs.text(
query, max_results=self.k, backend=self.duck_duck_go_backend
)
return results

def forward(
self, query_or_queries: Union[str, List[str]], exclude_urls: List[str] = []
):
Expand All @@ -705,9 +722,7 @@ def forward(

for query in queries:
# list of dicts that will be parsed to return
results = self.ddgs.text(
query, max_results=self.k, backend=self.duck_duck_go_backend
)
results = self.request(query)

for d in results:
# assert d is dict
Expand Down

0 comments on commit d50511f

Please sign in to comment.