naman1102 commited on
Commit
eca7386
·
1 Parent(s): 4f114c3

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +10 -4
tools.py CHANGED
@@ -87,17 +87,22 @@ def simple_search(query: str, max_results: int = 5) -> List[str]:
87
  with DDGS() as ddgs:
88
  results = []
89
  for r in ddgs.text(q, max_results=max_results):
90
- # Format each result with title, link and snippet
91
- result = f"{r['title']} {r['link']}\n{r['body']}"
 
 
 
 
 
92
  results.append(result)
93
  return results
94
  except Exception as e:
95
  print(f"Search error: {str(e)}")
96
  return []
97
 
98
- # Retry logic with exponential backoff
99
  max_attempts = 4
100
- base_delay = 10 # seconds
101
 
102
  for attempt in range(max_attempts):
103
  try:
@@ -108,6 +113,7 @@ def simple_search(query: str, max_results: int = 5) -> List[str]:
108
  except Exception as e:
109
  print(f"Attempt {attempt + 1}/{max_attempts} failed: {str(e)}")
110
  if attempt < max_attempts - 1:
 
111
  delay = base_delay * (2 ** attempt) # exponential backoff
112
  print(f"Retrying in {delay}s...")
113
  time.sleep(delay)
 
87
  with DDGS() as ddgs:
88
  results = []
89
  for r in ddgs.text(q, max_results=max_results):
90
+ # Handle missing keys gracefully
91
+ title = r.get('title', 'No title')
92
+ link = r.get('link', r.get('href', 'No link'))
93
+ body = r.get('body', r.get('snippet', 'No description'))
94
+
95
+ # Format result with available information
96
+ result = f"{title} – {link}\n{body}"
97
  results.append(result)
98
  return results
99
  except Exception as e:
100
  print(f"Search error: {str(e)}")
101
  return []
102
 
103
+ # Retry logic with exponential backoff and longer delays
104
  max_attempts = 4
105
+ base_delay = 30 # increased base delay to 30 seconds
106
 
107
  for attempt in range(max_attempts):
108
  try:
 
113
  except Exception as e:
114
  print(f"Attempt {attempt + 1}/{max_attempts} failed: {str(e)}")
115
  if attempt < max_attempts - 1:
116
+ # Longer delays between attempts
117
  delay = base_delay * (2 ** attempt) # exponential backoff
118
  print(f"Retrying in {delay}s...")
119
  time.sleep(delay)