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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -52
app.py CHANGED
@@ -22,77 +22,44 @@ def my_custom_tool(arg1: str, arg2: int) -> str: # Return type specification is
22
 
23
  @tool
24
  def tavily_search(query: str) -> str:
25
- """An enhanced search tool that performs web search using Tavily API with better error handling and formatting.
26
  Args:
27
  query: The search query to look up
28
  Returns:
29
- str: A well-formatted summary of search results with key highlights
30
  """
31
  try:
32
  client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
33
- search_result = client.search(
34
- query=query,
35
- search_depth="advanced",
36
- include_domains=["*.gov", "*.edu", "*.org"], # More reliable sources
37
- exclude_domains=["pinterest.com", "reddit.com"] # Exclude social media
38
- )
39
 
40
- summary = f"🔍 Search Results for: {query}\n\n"
41
- summary += "Key Findings:\n"
42
-
43
- for idx, result in enumerate(search_result['results'][:5], 1):
44
- summary += f"\n{idx}. {result['title']}\n"
45
- summary += f" Source: {result['domain']}\n"
46
- if 'published_date' in result:
47
- summary += f" Date: {result['published_date']}\n"
48
- summary += f" Summary: {result['description'][:250]}...\n"
49
 
50
  return summary
51
  except Exception as e:
52
- return f"Search Error: {str(e)}. Please try rephrasing your query or try again later."
53
 
54
 
55
  @tool
56
- def get_current_time_in_timezone(location: str) -> str:
57
- """An improved timezone tool with better location handling and formatted output.
58
  Args:
59
- location: City name or timezone identifier (e.g., 'Tokyo' or 'Asia/Tokyo')
60
  Returns:
61
- str: Formatted time information with additional context
62
  """
63
  try:
64
- # Common city to timezone mappings
65
- city_to_timezone = {
66
- 'tokyo': 'Asia/Tokyo',
67
- 'new york': 'America/New_York',
68
- 'london': 'Europe/London',
69
- 'paris': 'Europe/Paris',
70
- 'istanbul': 'Europe/Istanbul'
71
- }
72
-
73
- # Input validation and normalization
74
- clean_location = location.lower().strip()
75
- if clean_location in city_to_timezone:
76
- timezone = city_to_timezone[clean_location]
77
- else:
78
- timezone = location
79
-
80
  tz = pytz.timezone(timezone)
81
- current_time = datetime.datetime.now(tz)
82
-
83
- # Format the output
84
- formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
85
- day_name = current_time.strftime("%A")
86
-
87
- return f"""
88
- 📍 Location: {location.title()}
89
- 🕒 Current Time: {formatted_time}
90
- 📅 Day: {day_name}
91
- """
92
- except pytz.exceptions.UnknownTimeZoneError:
93
- return f"Error: '{location}' is not a recognized timezone or city. Please try a major city name or standard timezone format."
94
  except Exception as e:
95
- return f"Time lookup error: {str(e)}"
 
96
 
97
  # Initialize tools
98
  final_answer = FinalAnswerTool()
 
22
 
23
  @tool
24
  def tavily_search(query: str) -> str:
25
+ """A tool that performs web search using Tavily API and formats the results.
26
  Args:
27
  query: The search query to look up
28
  Returns:
29
+ str: A formatted summary of search results
30
  """
31
  try:
32
  client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
33
+ search_result = client.search(query=query, search_depth="advanced")
 
 
 
 
 
34
 
35
+ # Format the results in a user-friendly way
36
+ summary = f"Here are the search results for: {query}\n\n"
37
+ for result in search_result['results'][:5]: # Top 5 results
38
+ summary += f"• {result['title']}\n"
39
+ summary += f" Summary: {result['description'][:200]}...\n\n"
 
 
 
 
40
 
41
  return summary
42
  except Exception as e:
43
+ return f"Error performing search: {str(e)}"
44
 
45
 
46
  @tool
47
+ def get_current_time_in_timezone(timezone: str) -> str:
48
+ """A tool that fetches and formats the current local time in a specified timezone.
49
  Args:
50
+ timezone: A string representing a valid timezone (e.g., 'America/New_York', 'Asia/Tokyo')
51
  Returns:
52
+ str: A formatted string containing the timezone and current time
53
  """
54
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  tz = pytz.timezone(timezone)
56
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
57
+ # Return a more descriptive string with cleaned timezone name
58
+ city = timezone.split('/')[-1].replace('_', ' ')
59
+ return f"The current time in {city} is: {local_time}"
 
 
 
 
 
 
 
 
 
60
  except Exception as e:
61
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
62
+
63
 
64
  # Initialize tools
65
  final_answer = FinalAnswerTool()