WeByT3 commited on
Commit
0c6cece
·
verified ·
1 Parent(s): 4c0107d

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +25 -6
tools.py CHANGED
@@ -1,6 +1,7 @@
1
  from langchain_core.tools import tool
2
  import pandas as pd
3
  import os
 
4
  import wikipedia
5
 
6
 
@@ -51,20 +52,38 @@ def divide(a: int, b: int) -> int:
51
  return a / b
52
 
53
  @tool
54
- def wikipedia_search_tool(query: str) -> str:
55
  """
56
- Search wikipedia for articles
 
 
57
 
58
  Args:
59
  query: The query to search in wikipedia
 
60
 
61
  Example:
62
  {"message": "How many albums did the Beatles produce between 1965 and 1968?"}
63
- wikipedia_search_tool("The Beatles")
64
  """
65
  try:
66
- page_title = wikipedia.search(query)[0]
67
- summary = wikipedia.summary(page_title, sentences=5)
68
- return f"Title: {page.title}\n\n{page.content}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  except Exception as e:
70
  return f"Error: {str(e)}"
 
1
  from langchain_core.tools import tool
2
  import pandas as pd
3
  import os
4
+ import re
5
  import wikipedia
6
 
7
 
 
52
  return a / b
53
 
54
  @tool
55
+ def wikipedia_search_tool(query: str, year_range: str = None) -> str:
56
  """
57
+ Deep Wikipedia search with optional year filtering.
58
+ - Retrieves full text from multiple related pages.
59
+ - Filters for year mentions if year_range is specified (e.g. '1965-1968').
60
 
61
  Args:
62
  query: The query to search in wikipedia
63
+ year_range: The range of years to search information for. Only use if necessary
64
 
65
  Example:
66
  {"message": "How many albums did the Beatles produce between 1965 and 1968?"}
67
+ wikipedia_search_tool("The Beatles albums", "1965-1968")
68
  """
69
  try:
70
+ results = wikipedia.search(query, results=5)
71
+ if not results:
72
+ return "No relevant pages found."
73
+
74
+ combined = ""
75
+ for title in results:
76
+ try:
77
+ page = wikipedia.page(title)
78
+ content = clean_and_truncate(page.content)
79
+ if year_range:
80
+ y1, y2 = map(int, year_range.split('-'))
81
+ content = extract_year_range(content, y1, y2)
82
+ if content:
83
+ combined += f"\n\n## {title}\n{content}"
84
+ except:
85
+ continue
86
+
87
+ return combined.strip() if combined else "No content matched the year filter."
88
  except Exception as e:
89
  return f"Error: {str(e)}"