From 2625ffabc121dacf3f326dae644ce910cf545d91 Mon Sep 17 00:00:00 2001 From: assafelovic Date: Tue, 19 Dec 2023 09:59:25 +0200 Subject: [PATCH] added fallback to tavily search --- .../retrievers/tavily_search/tavily_search.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/gpt_researcher/retrievers/tavily_search/tavily_search.py b/gpt_researcher/retrievers/tavily_search/tavily_search.py index 5357398fe..af86e3a69 100644 --- a/gpt_researcher/retrievers/tavily_search/tavily_search.py +++ b/gpt_researcher/retrievers/tavily_search/tavily_search.py @@ -3,6 +3,7 @@ # libraries import os from tavily import TavilyClient +from duckduckgo_search import DDGS class TavilySearch(): @@ -39,8 +40,12 @@ def search(self, max_results=7): Returns: """ - # Search the query - results = self.client.search(self.query, search_depth="advanced", max_results=max_results) - # Return the results - search_response = [{"href": obj["url"], "body": obj["content"]} for obj in results.get("results", [])] + try: + # Search the query + results = self.client.search(self.query, search_depth="advanced", max_results=max_results) + # Return the results + search_response = [{"href": obj["url"], "body": obj["content"]} for obj in results.get("results", [])] + except Exception as e: # Fallback in case overload on Tavily Search API + ddg = DDGS() + search_response = ddg.text(self.query, region='wt-wt', max_results=max_results) return search_response