MartinHummel commited on
Commit
53be28d
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py CHANGED
@@ -18,6 +18,48 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def wikipedia_search(query: str, results_limit: int = 5) -> str:
23
+ """
24
+ A tool that searches Wikipedia for a given query and returns a summary of the top result along with other potential matches.
25
+
26
+ Args:
27
+ query: The search query for Wikipedia.
28
+ results_limit: The maximum number of search results to consider (default is 5).
29
+
30
+ Returns:
31
+ A string containing the top result's title and a short summary, as well as other related titles if available.
32
+ """
33
+ try:
34
+ import wikipedia
35
+ except ImportError:
36
+ return "The 'wikipedia' library is not installed. Please install it using 'pip install wikipedia'."
37
+
38
+ try:
39
+ # Perform the search using Wikipedia's API
40
+ search_results = wikipedia.search(query, results=results_limit)
41
+ if not search_results:
42
+ return f"No results found for '{query}'."
43
+
44
+ # Fetch summary for the top result
45
+ top_title = search_results[0]
46
+ try:
47
+ summary = wikipedia.summary(top_title, sentences=2)
48
+ except Exception as e:
49
+ summary = f"Could not retrieve summary for '{top_title}': {e}"
50
+
51
+ # Format additional results if available
52
+ other_results = search_results[1:]
53
+ if other_results:
54
+ others = ", ".join(other_results)
55
+ return f"Top result: {top_title}\nSummary: {summary}\nOther results: {others}"
56
+ else:
57
+ return f"Top result: {top_title}\nSummary: {summary}"
58
+
59
+ except Exception as e:
60
+ return f"An error occurred during the Wikipedia search: {e}"
61
+
62
+
63
  @tool
64
  def get_current_time_in_timezone(timezone: str) -> str:
65
  """A tool that fetches the current local time in a specified timezone.