WeByT3 commited on
Commit
7b53193
·
verified ·
1 Parent(s): c3d7a61

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +11 -29
tools.py CHANGED
@@ -2,7 +2,7 @@ from langchain_core.tools import tool
2
  import pandas as pd
3
  import os
4
  import re
5
- import wikipedia
6
 
7
 
8
  @tool
@@ -52,38 +52,20 @@ def divide(a: int, b: int) -> int:
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)}"
 
2
  import pandas as pd
3
  import os
4
  import re
5
+ import requests
6
 
7
 
8
  @tool
 
52
  return a / b
53
 
54
  @tool
55
+ def wikidata_search_tool(sparql_query: str) -> str:
56
  """
57
+ A tool that allows to search in WikiData for specific information
58
+ and returns the information as a json in string format
 
59
 
60
  Args:
61
+ sparql_query: The query to search in wikidata. This must be a SPARQL query.
 
62
 
63
  Example:
64
  {"message": "How many albums did the Beatles produce between 1965 and 1968?"}
65
+ wikidata_search_tool("The Beatles albums")
66
  """
67
+ endpoint_url = "https://query.wikidata.org/sparql"
68
+ headers = {"Accept": "application/sparql-results+json"}
69
+ response = requests.get(endpoint_url, headers=headers, params={"query": sparql_query})
70
+ response.raise_for_status()
71
+ return response.json()