wt002 commited on
Commit
0ccd2e5
·
verified ·
1 Parent(s): 2846e6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -24
app.py CHANGED
@@ -21,31 +21,17 @@ load_dotenv()
21
 
22
 
23
 
24
- @tool
25
- def search_wikipedia(query: str) -> str:
26
- """
27
- Fetches a summary of a Wikipedia page for a given query.
28
- Args:
29
- query: The search term to look up on Wikipedia.
30
- Returns:
31
- str: A summary of the Wikipedia page if successful, or an error message if the request fails.
32
- Raises:
33
- requests.exceptions.RequestException: If there is an issue with the HTTP request.
34
- """
35
- url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
36
 
37
- try:
38
- response = requests.get(url)
39
- response.raise_for_status()
40
-
41
- data = response.json()
42
- title = data["title"]
43
- extract = data["extract"]
44
-
45
- return f"Summary for {title}: {extract}"
46
-
47
- except requests.exceptions.RequestException as e:
48
- return f"Error fetching Wikipedia data: {str(e)}"
49
 
50
 
51
 
 
21
 
22
 
23
 
24
+ import wikipedia
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ @tool
27
+ class WikipediaSearchTool(Tool):
28
+ def use(self, query: str) -> str:
29
+ try:
30
+ return wikipedia.summary(query, sentences=2)
31
+ except wikipedia.exceptions.DisambiguationError as e:
32
+ return f"Disambiguation Error: {e.options}"
33
+ except wikipedia.exceptions.PageError:
34
+ return "No page found."
 
 
 
35
 
36
 
37