zhijingeu commited on
Commit
5a45f55
·
verified ·
1 Parent(s): ddb2b25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -2,6 +2,8 @@ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
 
 
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
@@ -18,6 +20,37 @@ 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.
@@ -55,7 +88,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[get_current_time_in_timezone,final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
2
  import datetime
3
  import requests
4
  import pytz
5
+ import yfinance as yf
6
+ import pandas as pd
7
  import yaml
8
  from tools.final_answer import FinalAnswerTool
9
 
 
20
  """
21
  return "What magic will you build ?"
22
 
23
+ @tool
24
+ def get_sp500_price(date_str)-> str:
25
+ """
26
+ Fetches the S&P 500 price for a given date.
27
+
28
+ Parameters:
29
+ date_str (str): The date in 'YYYY-MM-DD' format.
30
+
31
+ Returns:
32
+ float: The closing price of the S&P 500 on the given date.
33
+ """
34
+ try:
35
+ # Convert the input string to a datetime object
36
+ date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
37
+
38
+ # Define the ticker symbol for S&P 500
39
+ sp500 = '^GSPC'
40
+
41
+ # Fetch the data for the given date
42
+ data = yf.download(sp500, start=date_str, end=(date + datetime.timedelta(days=1)).strftime('%Y-%m-%d'))
43
+
44
+ # Check if data is available
45
+ if not data.empty:
46
+ # Return the closing price
47
+ return data['Close'].iloc[0]
48
+ else:
49
+ return None
50
+ except Exception as e:
51
+ print(f"An error occurred: {e}")
52
+ return None
53
+
54
  @tool
55
  def get_current_time_in_timezone(timezone: str) -> str:
56
  """A tool that fetches the current local time in a specified timezone.
 
88
 
89
  agent = CodeAgent(
90
  model=model,
91
+ tools=[get_sp500_price,get_current_time_in_timezone,final_answer], ## add your tools here (don't remove final answer)
92
  max_steps=6,
93
  verbosity_level=1,
94
  grammar=None,