Scott Cogan commited on
Commit
3f8c358
·
1 Parent(s): 5125078
Files changed (1) hide show
  1. app.py +54 -5
app.py CHANGED
@@ -103,30 +103,79 @@ def open_youtube_video(url: str, query: str) -> str:
103
  def google_search(query: str) -> str:
104
  '''Performs a web search for the given query using DuckDuckGo.'''
105
  try:
 
 
 
106
  response = requests.get(
107
  "https://api.duckduckgo.com/",
108
  params={
109
  "q": query,
110
  "format": "json",
111
  "no_html": 1,
112
- "no_redirect": 1
113
- }
 
 
114
  )
115
  response.raise_for_status()
116
  data = response.json()
117
 
118
- # Combine Abstract and Related Topics for a comprehensive response
119
  result = []
 
 
120
  if data.get("Abstract"):
121
  result.append(data["Abstract"])
 
 
 
 
 
 
122
  if data.get("RelatedTopics"):
123
- for topic in data["RelatedTopics"][:3]: # Limit to first 3 related topics
124
  if "Text" in topic:
125
  result.append(topic["Text"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  return "\n".join(result) if result else "No results found."
128
- except Exception as e:
 
 
 
 
 
129
  return f"Error performing search: {str(e)}"
 
 
 
130
 
131
  def log_message(message: BaseMessage, prefix: str = ""):
132
  """Helper function to log message details."""
 
103
  def google_search(query: str) -> str:
104
  '''Performs a web search for the given query using DuckDuckGo.'''
105
  try:
106
+ # Add delay between requests to avoid rate limiting
107
+ time.sleep(1)
108
+
109
  response = requests.get(
110
  "https://api.duckduckgo.com/",
111
  params={
112
  "q": query,
113
  "format": "json",
114
  "no_html": 1,
115
+ "no_redirect": 1,
116
+ "skip_disambig": 1 # Skip disambiguation pages
117
+ },
118
+ timeout=10 # Add timeout
119
  )
120
  response.raise_for_status()
121
  data = response.json()
122
 
123
+ # Enhanced result processing
124
  result = []
125
+
126
+ # Add Abstract if available
127
  if data.get("Abstract"):
128
  result.append(data["Abstract"])
129
+
130
+ # Add Definition if available
131
+ if data.get("Definition"):
132
+ result.append(f"Definition: {data['Definition']}")
133
+
134
+ # Process Related Topics more thoroughly
135
  if data.get("RelatedTopics"):
136
+ for topic in data["RelatedTopics"][:5]: # Increased to 5 related topics
137
  if "Text" in topic:
138
  result.append(topic["Text"])
139
+ elif "Topics" in topic:
140
+ for subtopic in topic["Topics"][:2]:
141
+ if "Text" in subtopic:
142
+ result.append(subtopic["Text"])
143
+
144
+ # Add Answer if available
145
+ if data.get("Answer"):
146
+ result.append(f"Answer: {data['Answer']}")
147
+
148
+ # If no results found, try alternative search approach
149
+ if not result:
150
+ # Try searching with quotes for exact match
151
+ quoted_response = requests.get(
152
+ "https://api.duckduckgo.com/",
153
+ params={
154
+ "q": f'"{query}"',
155
+ "format": "json",
156
+ "no_html": 1,
157
+ "no_redirect": 1
158
+ },
159
+ timeout=10
160
+ )
161
+ quoted_data = quoted_response.json()
162
+
163
+ if quoted_data.get("Abstract"):
164
+ result.append(quoted_data["Abstract"])
165
+ if quoted_data.get("Answer"):
166
+ result.append(f"Answer: {quoted_data['Answer']}")
167
 
168
  return "\n".join(result) if result else "No results found."
169
+
170
+ except requests.exceptions.Timeout:
171
+ logger.warning(f"Search timeout for query: {query}")
172
+ return "Search timed out. Please try again."
173
+ except requests.exceptions.RequestException as e:
174
+ logger.error(f"Search error for query {query}: {str(e)}")
175
  return f"Error performing search: {str(e)}"
176
+ except Exception as e:
177
+ logger.error(f"Unexpected error during search for query {query}: {str(e)}")
178
+ return "An unexpected error occurred during the search."
179
 
180
  def log_message(message: BaseMessage, prefix: str = ""):
181
  """Helper function to log message details."""