Cheangys commited on
Commit
e859fe1
·
verified ·
1 Parent(s): 61614af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -33,14 +33,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
36
  @tool
37
  def get_stock_price(stock: str) -> str:
38
  """
39
  Fetches the current stock price using DuckDuckGo search.
40
-
41
  Args:
42
  stock (str): A stock ticker symbol or company name (e.g., 'AAPL', 'GOOGL').
43
-
44
  Returns:
45
  str: A message with the stock price or an error message.
46
  """
@@ -54,16 +54,35 @@ def get_stock_price(stock: str) -> str:
54
  # Debugging: Print results to see what is returned
55
  print(f"DEBUG: search results -> {search_results}")
56
 
57
- # Check if results exist and return the first result
58
  if isinstance(search_results, list) and len(search_results) > 0:
59
- return f"The current price for {stock} is: {search_results[0]}"
60
-
61
- return f"Could not fetch stock price for {stock}. Please try again."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  except Exception as e:
64
  return f"Error fetching price for stock '{stock}': {str(e)}"
65
 
66
-
67
 
68
  final_answer = FinalAnswerTool()
69
 
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ import re # Import regular expression library
37
+
38
  @tool
39
  def get_stock_price(stock: str) -> str:
40
  """
41
  Fetches the current stock price using DuckDuckGo search.
 
42
  Args:
43
  stock (str): A stock ticker symbol or company name (e.g., 'AAPL', 'GOOGL').
 
44
  Returns:
45
  str: A message with the stock price or an error message.
46
  """
 
54
  # Debugging: Print results to see what is returned
55
  print(f"DEBUG: search results -> {search_results}")
56
 
57
+ # Handle different result types
58
  if isinstance(search_results, list) and len(search_results) > 0:
59
+ # Try to extract the stock price from the first result
60
+ first_result = search_results[0]
61
+
62
+ # Attempt to find a pattern like "123.45 USD" in the text
63
+ match = re.search(r"(\d+\.\d+)\s*(USD|EUR|CAD|GBP)", first_result, re.IGNORECASE)
64
+
65
+ if match:
66
+ price = match.group(1)
67
+ currency = match.group(2)
68
+ return f"The current price for {stock} is: {price} {currency}"
69
+ else:
70
+ return f"Could not extract stock price from search results for {stock}."
71
+ elif isinstance(search_results, str):
72
+ # Try to directly find a price in the string
73
+ match = re.search(r"(\d+\.\d+)\s*(USD|EUR|CAD|GBP)", search_results, re.IGNORECASE)
74
+ if match:
75
+ price = match.group(1)
76
+ currency = match.group(2)
77
+ return f"The current price for {stock} is: {price} {currency}"
78
+ else:
79
+ return f"Could not extract stock price from search results for {stock}."
80
+ else:
81
+ return f"No search results found for {stock}."
82
 
83
  except Exception as e:
84
  return f"Error fetching price for stock '{stock}': {str(e)}"
85
 
 
86
 
87
  final_answer = FinalAnswerTool()
88