Cheangys commited on
Commit
737c90a
·
verified ·
1 Parent(s): d3b6a26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -35,16 +35,30 @@ def get_current_time_in_timezone(timezone: str) -> str:
35
 
36
  @tool
37
  def get_stock_price(stock: str) -> str:
38
- """A tool that fetches the current stock price from web search.
 
39
  Args:
40
- stock: A string representing a valid stock name or stock code (e.g., 'DIALOG' or '7277').
 
 
 
41
  """
42
  try:
43
- # Create stock object
44
- stk = DuckDuckGoSearchTool(queries=f'What is the current price of {stock}?')
45
- return f"The current price for {stock} is {stk}"
 
 
 
 
 
 
 
 
 
46
  except Exception as e:
47
  return f"Error fetching price for stock '{stock}': {str(e)}"
 
48
 
49
 
50
  final_answer = FinalAnswerTool()
 
35
 
36
  @tool
37
  def get_stock_price(stock: str) -> str:
38
+ """Fetches the current stock price using DuckDuckGo search.
39
+
40
  Args:
41
+ stock: A string representing a stock ticker or company name (e.g., 'AAPL', 'GOOGL').
42
+
43
+ Returns:
44
+ A string with the stock price or an error message.
45
  """
46
  try:
47
+ # Initialize the search tool
48
+ search_tool = DuckDuckGoSearchTool()
49
+
50
+ # Perform the search query
51
+ search_results = search_tool.run(f"Current stock price of {stock}")
52
+
53
+ # If search_results is a list, extract the first relevant result
54
+ if isinstance(search_results, list) and len(search_results) > 0:
55
+ return f"The current price for {stock} is: {search_results[0]}"
56
+
57
+ return f"Could not fetch stock price for {stock}. Please try again."
58
+
59
  except Exception as e:
60
  return f"Error fetching price for stock '{stock}': {str(e)}"
61
+
62
 
63
 
64
  final_answer = FinalAnswerTool()