Duygu Jones commited on
Commit
e54ddb0
·
verified ·
1 Parent(s): ba7e21b

Update app.py

Browse files

wiki_tool added

Files changed (1) hide show
  1. app.py +49 -1
app.py CHANGED
@@ -8,6 +8,9 @@ import os
8
  from tavily import TavilyClient
9
  from Gradio_UI import GradioUI
10
 
 
 
 
11
  # Example tool template - can be modified for your needs
12
 
13
  @tool
@@ -20,6 +23,51 @@ def my_custom_tool(arg1: str, arg2: int) -> str: # Return type specification is
20
  return "What magic will you build?"
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @tool
24
  def tavily_search(query: str) -> str:
25
  """A tool that performs web search using Tavily API and formats the results.
@@ -84,7 +132,7 @@ with open("prompts.yaml", 'r') as stream:
84
  # Initialize agent with tools
85
  agent = CodeAgent(
86
  model=model,
87
- tools=[final_answer, tavily_search, get_current_time_in_timezone, image_generation_tool], # Added tavily_search
88
  max_steps=6,
89
  verbosity_level=1,
90
  grammar=None,
 
8
  from tavily import TavilyClient
9
  from Gradio_UI import GradioUI
10
 
11
+ import wikipedia
12
+ import textwrap
13
+
14
  # Example tool template - can be modified for your needs
15
 
16
  @tool
 
23
  return "What magic will you build?"
24
 
25
 
26
+ @tool
27
+ def wiki_search(query: str, sentences: int = 3) -> str:
28
+ """Search Wikipedia and return a summary of the topic.
29
+ Args:
30
+ query: The topic to search for on Wikipedia
31
+ sentences: Number of sentences to return in the summary (default: 3)
32
+ Returns:
33
+ str: A summary of the Wikipedia article
34
+ """
35
+ try:
36
+ # Set language to English for consistency
37
+ wikipedia.set_lang("en")
38
+
39
+ # Search for the page
40
+ search_results = wikipedia.search(query)
41
+ if not search_results:
42
+ return f"No Wikipedia results found for: {query}"
43
+
44
+ try:
45
+ # Get the page
46
+ page = wikipedia.page(search_results[0], auto_suggest=False)
47
+
48
+ # Get summary and full URL
49
+ summary = wikipedia.summary(search_results[0], sentences=sentences, auto_suggest=False)
50
+ url = page.url
51
+
52
+ # Format the response
53
+ response = f"Wikipedia article: {page.title}\n"
54
+ response += f"URL: {url}\n\n"
55
+ response += f"Summary:\n{textwrap.fill(summary, width=80)}\n"
56
+
57
+ return response
58
+
59
+ except wikipedia.DisambiguationError as e:
60
+ # Handle disambiguation pages
61
+ options = e.options[:5] # Show first 5 options
62
+ response = f"'{query}' may refer to multiple topics. Here are some options:\n\n"
63
+ for i, option in enumerate(options, 1):
64
+ response += f"{i}. {option}\n"
65
+ return response
66
+
67
+ except Exception as e:
68
+ return f"Error searching Wikipedia: {str(e)}"
69
+
70
+
71
  @tool
72
  def tavily_search(query: str) -> str:
73
  """A tool that performs web search using Tavily API and formats the results.
 
132
  # Initialize agent with tools
133
  agent = CodeAgent(
134
  model=model,
135
+ tools=[final_answer, tavily_search,wiki_search, get_current_time_in_timezone, image_generation_tool],
136
  max_steps=6,
137
  verbosity_level=1,
138
  grammar=None,