Wajahat698 commited on
Commit
a71907c
·
verified ·
1 Parent(s): c253389

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -1197,23 +1197,56 @@ def cache_response(query, response, ttl=3600):
1197
 
1198
 
1199
  tavily_tool = TavilySearchResults(
1200
- max_results=12,
1201
  search_depth="advanced",
1202
- topic="news",
1203
  days=1,
1204
  include_answer=True,
1205
  include_raw_content=True,
1206
- # include_domains=[...],
1207
  exclude_domains=['example.com'],
1208
- # name="...", # overwrite default tool name
1209
- # description="...", # overwrite default tool description
1210
- # args_schema=..., # overwrite default args_schema: BaseModel
1211
  )
1212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1213
  #Compile all tool functions into a list
1214
  tools = [
1215
  knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
1216
- tavily_tool,
1217
  #google_search_tool, # Tool for performing a Google search and retrieving search result snippets
1218
  ]
1219
 
 
1197
 
1198
 
1199
  tavily_tool = TavilySearchResults(
1200
+ max_results=13,
1201
  search_depth="advanced",
 
1202
  days=1,
1203
  include_answer=True,
1204
  include_raw_content=True,
 
1205
  exclude_domains=['example.com'],
 
 
 
1206
  )
1207
 
1208
+ def validate_tavily_results(tavily_tool):
1209
+ """
1210
+ Fetch and validate results from TavilySearchResults.
1211
+ Ensures that results do not lead to 'Page Not Found' pages.
1212
+ """
1213
+ results = tavily_tool.search()
1214
+ valid_results = []
1215
+
1216
+ for result in results:
1217
+ url = result.get('url')
1218
+ if not url:
1219
+ continue
1220
+
1221
+ try:
1222
+ # Fetch page content
1223
+ response = requests.get(url, timeout=10)
1224
+ if response.status_code != 200:
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
1243
+
1244
+
1245
+
1246
  #Compile all tool functions into a list
1247
  tools = [
1248
  knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
1249
+ {"tool": tavily_tool, "validate": validate_tavily_results(tavily_tool)},
1250
  #google_search_tool, # Tool for performing a Google search and retrieving search result snippets
1251
  ]
1252