Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Support backoff and retry for DuckDuckGoSearchRM #163

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
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
Loading