Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)}"
|