naman1102 commited on
Commit
6e69a67
·
1 Parent(s): 85d8e61

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +77 -10
tools.py CHANGED
@@ -225,19 +225,51 @@ def wikipedia_search_tool(wiki_query: str) -> str:
225
 
226
  If no valid wiki_query is provided, returns {}.
227
  """
228
- print("reached wikipedia search tool")
229
- query = wiki_query
230
  try:
231
- docs = WikipediaLoader(query=query, load_max_docs=5).load()
 
 
232
  result = ""
233
  counter = 1
234
  for doc in docs:
235
- result += f"\n\nDocument{counter}: {doc.metadata['title']}\n. {doc.page_content}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  counter += 1
 
 
 
 
 
237
  return result
 
238
  except Exception as e:
239
- print(f"Error during Wikipedia search: {e}")
240
- return "Wikipedia search failed, try another tool or query"
 
241
 
242
  @tool
243
  def arxiv_search_tool(arxiv_query: str) -> str:
@@ -246,18 +278,53 @@ def arxiv_search_tool(arxiv_query: str) -> str:
246
  Expects: arxiv_query is a non‐empty string.
247
  Returns: text summary of first matching page or an error message>"
248
  """
249
- print("reached arxiv_search_tool")
250
  try:
251
  docs = ArxivLoader(query=arxiv_query, load_max_docs=5).load()
 
 
252
  result = ""
253
  counter = 1
254
  for doc in docs:
255
- result += f"\n\nDocument{counter}: {doc.metadata['title']}\n. {doc.page_content}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  counter += 1
 
 
 
 
 
257
  return result
 
258
  except Exception as e:
259
- print(f"Error during Arxiv search: {e}")
260
- return "Arxiv search failed, try another tool or query"
 
261
 
262
 
263
  from langchain_openai import ChatOpenAI
 
225
 
226
  If no valid wiki_query is provided, returns {}.
227
  """
228
+ print(f"DEBUG: reached wikipedia_search_tool with query: {wiki_query}")
 
229
  try:
230
+ docs = WikipediaLoader(query=wiki_query, load_max_docs=5).load()
231
+ print(f"DEBUG: WikipediaLoader returned {len(docs)} documents")
232
+
233
  result = ""
234
  counter = 1
235
  for doc in docs:
236
+ print(f"DEBUG: Processing Wikipedia document {counter}")
237
+ print(f"DEBUG: Document metadata: {doc.metadata}")
238
+ print(f"DEBUG: Document content length: {len(doc.page_content)}")
239
+
240
+ # Handle different metadata structures
241
+ title = "Unknown Title"
242
+ if hasattr(doc, 'metadata') and doc.metadata:
243
+ # Try different possible title keys
244
+ if 'title' in doc.metadata:
245
+ title = doc.metadata['title']
246
+ elif 'Title' in doc.metadata:
247
+ title = doc.metadata['Title']
248
+ elif 'source' in doc.metadata:
249
+ title = doc.metadata['source']
250
+ else:
251
+ # Use first available key as title
252
+ if doc.metadata:
253
+ first_key = list(doc.metadata.keys())[0]
254
+ title = f"Wikipedia: {doc.metadata[first_key]}"
255
+
256
+ print(f"DEBUG: Using Wikipedia title: {title}")
257
+
258
+ # Truncate content if too long
259
+ content = doc.page_content[:2000] if len(doc.page_content) > 2000 else doc.page_content
260
+ result += f"\n\nDocument{counter}: {title}\n{content}"
261
  counter += 1
262
+
263
+ if not result.strip():
264
+ return "No Wikipedia results found for the given query"
265
+
266
+ print(f"DEBUG: Final Wikipedia result length: {len(result)}")
267
  return result
268
+
269
  except Exception as e:
270
+ error_msg = f"Error during Wikipedia search: {str(e)}"
271
+ print(f"DEBUG: {error_msg}")
272
+ return error_msg
273
 
274
  @tool
275
  def arxiv_search_tool(arxiv_query: str) -> str:
 
278
  Expects: arxiv_query is a non‐empty string.
279
  Returns: text summary of first matching page or an error message>"
280
  """
281
+ print(f"DEBUG: reached arxiv_search_tool with query: {arxiv_query}")
282
  try:
283
  docs = ArxivLoader(query=arxiv_query, load_max_docs=5).load()
284
+ print(f"DEBUG: ArxivLoader returned {len(docs)} documents")
285
+
286
  result = ""
287
  counter = 1
288
  for doc in docs:
289
+ print(f"DEBUG: Processing document {counter}")
290
+ print(f"DEBUG: Document metadata: {doc.metadata}")
291
+ print(f"DEBUG: Document content length: {len(doc.page_content)}")
292
+
293
+ # Handle different metadata structures
294
+ title = "Unknown Title"
295
+ if hasattr(doc, 'metadata') and doc.metadata:
296
+ # Try different possible title keys
297
+ if 'title' in doc.metadata:
298
+ title = doc.metadata['title']
299
+ elif 'Title' in doc.metadata:
300
+ title = doc.metadata['Title']
301
+ elif 'entry_id' in doc.metadata:
302
+ title = doc.metadata['entry_id']
303
+ elif 'summary' in doc.metadata:
304
+ title = f"ArXiv Paper {counter}"
305
+ else:
306
+ # Use first available key as title
307
+ if doc.metadata:
308
+ first_key = list(doc.metadata.keys())[0]
309
+ title = f"{first_key}: {doc.metadata[first_key]}"
310
+
311
+ print(f"DEBUG: Using title: {title}")
312
+
313
+ # Truncate content if too long
314
+ content = doc.page_content[:2000] if len(doc.page_content) > 2000 else doc.page_content
315
+ result += f"\n\nDocument{counter}: {title}\n{content}"
316
  counter += 1
317
+
318
+ if not result.strip():
319
+ return "No ArXiv results found for the given query"
320
+
321
+ print(f"DEBUG: Final ArXiv result length: {len(result)}")
322
  return result
323
+
324
  except Exception as e:
325
+ error_msg = f"Error during Arxiv search: {str(e)}"
326
+ print(f"DEBUG: {error_msg}")
327
+ return error_msg
328
 
329
 
330
  from langchain_openai import ChatOpenAI