Wajahat698 commited on
Commit
368f938
·
verified ·
1 Parent(s): bfb723b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -1191,11 +1191,6 @@ def cache_response(query, response, ttl=3600):
1191
 
1192
 
1193
 
1194
-
1195
-
1196
-
1197
-
1198
-
1199
  tavily_tool = TavilySearchResults(
1200
  max_results=13,
1201
  search_depth="advanced",
@@ -1210,11 +1205,24 @@ def validate_tavily_results(query):
1210
  Fetch and validate results from TavilySearchResults.
1211
  Ensures that results do not lead to 'Page Not Found' pages.
1212
  """
1213
- results = tavily_tool.run(query=query)
 
 
 
 
 
 
 
 
 
 
 
 
 
1214
  valid_results = []
1215
 
1216
  for result in results:
1217
- url = result.get('url')
1218
  if not url:
1219
  continue
1220
 
@@ -1225,18 +1233,17 @@ def validate_tavily_results(query):
1225
  continue
1226
 
1227
  # Parse page content to check for "Page Not Found"
1228
- soup = BeautifulSoup(response.content, 'html.parser')
1229
- page_text = soup.get_text()
1230
 
1231
  # Common markers of "Page Not Found"
1232
- if any(marker in page_text for marker in ["Page Not Found", "404", "Error", "Not Available"]):
1233
  continue
1234
 
1235
  # Add valid result
1236
  valid_results.append(result)
1237
 
1238
  except requests.RequestException:
1239
- # Skip URLs causing errors
1240
  continue
1241
 
1242
  return valid_results
@@ -1247,7 +1254,6 @@ def tavily_tool_with_validation(query):
1247
  return "\n".join([f"{res.get('title')}: {res.get('url')}" for res in valid_results])
1248
 
1249
 
1250
-
1251
  #Compile all tool functions into a list
1252
  tools = [
1253
  knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
 
1191
 
1192
 
1193
 
 
 
 
 
 
1194
  tavily_tool = TavilySearchResults(
1195
  max_results=13,
1196
  search_depth="advanced",
 
1205
  Fetch and validate results from TavilySearchResults.
1206
  Ensures that results do not lead to 'Page Not Found' pages.
1207
  """
1208
+ # Use the Tavily tool to fetch results
1209
+ results = tavily_tool.run(query) # Pass the query as tool_input to the Tavily tool
1210
+
1211
+ # Check if results are in expected format (list of dicts). Adjust if necessary.
1212
+ if isinstance(results, str):
1213
+ # If results are returned as a string, parse them into a structured format
1214
+ # For example, if results are JSON-formatted strings
1215
+ import json
1216
+ try:
1217
+ results = json.loads(results)
1218
+ except json.JSONDecodeError:
1219
+ # If parsing fails, split by newlines assuming URLs are separated by lines
1220
+ results = [{"title": f"Result {i+1}", "url": url.strip()} for i, url in enumerate(results.splitlines())]
1221
+
1222
  valid_results = []
1223
 
1224
  for result in results:
1225
+ url = result.get("url")
1226
  if not url:
1227
  continue
1228
 
 
1233
  continue
1234
 
1235
  # Parse page content to check for "Page Not Found"
1236
+ soup = BeautifulSoup(response.content, "html.parser")
1237
+ page_text = soup.get_text(separator=' ', strip=True)
1238
 
1239
  # Common markers of "Page Not Found"
1240
+ if any(marker.lower() in page_text.lower() for marker in ["Page Not Found", "404", "Error", "Not Available"]):
1241
  continue
1242
 
1243
  # Add valid result
1244
  valid_results.append(result)
1245
 
1246
  except requests.RequestException:
 
1247
  continue
1248
 
1249
  return valid_results
 
1254
  return "\n".join([f"{res.get('title')}: {res.get('url')}" for res in valid_results])
1255
 
1256
 
 
1257
  #Compile all tool functions into a list
1258
  tools = [
1259
  knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses