skurtis commited on
Commit
aa9d2eb
·
verified ·
1 Parent(s): e394e84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -47
app.py CHANGED
@@ -6,61 +6,60 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # thoughts - add another tool to convert a users english input "10 year treasury yield": DGS10 to the appropriate ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  @tool
11
- def pull_rates(fred_id: str)-> str: #it's import to specify the return type
12
- #Keep this format for the description / args / args description but feel free to modify the tool
13
- """A tool that fetches the latest interest or treasury rate from FRED.
14
  Args:
15
- fred_id: a string representing a valid insterst rate code to pull from the FRED website
 
 
16
  """
17
-
18
- # Dictionary mapping user-friendly tenors to their FRED IDs.
19
- yield_curve_ids = {'SOFR': 'SOFR',
20
- 'DGS1MO': '1 Month',
21
- 'DGS3MO': '3 Month',
22
- 'DGS6MO': '6 Month',
23
- 'DGS1': '1 Year',
24
- 'DGS2': '2 Year',
25
- 'DGS3': '3 Year',
26
- 'DGS5': '5 Year',
27
- 'DGS7': '7 Year',
28
- 'DGS10': '10 Year',
29
- 'DGS20': '20 Year',
30
- 'DGS30': '30 Year'}
31
-
32
- english_fred_id = yield_curve_ids[fred_id]
33
- # Build the URL for the CSV on the FRED website
34
- fred_api_url = f"https://fred.stlouisfed.org/graph/fredgraph.csv?id={fred_id}"
35
-
36
- try:
37
- import pandas as pd
38
- # Create timezone object
39
- # Read the CSV data directly into a pandas DataFrame
40
- df = pd.read_csv(fred_api_url)
41
- # get the latest rate
42
- latest_rate = df.loc[df['observation_date']==df['observation_date'].max(),fred_id].values[0]
43
-
44
- # return f"The {english_fred_id} yield closed at {latest_rate} on {df['observation_date'].max()}%"
45
- return f"The current {fred_id} is {latest_rate}%"
46
-
47
- except Exception as e:
48
- return "Error fetching rates from FRED."
49
 
50
  @tool
51
- def get_current_time_in_timezone(timezone: str) -> str:
52
- """A tool that fetches the current local time in a specified timezone.
53
  Args:
54
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
55
  """
 
56
  try:
57
- # Create timezone object
58
- tz = pytz.timezone(timezone)
59
- # Get current time in that timezone
60
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
61
- return f"The current local time in {timezone} is: {local_time}"
 
 
 
62
  except Exception as e:
63
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
64
 
65
 
66
  final_answer = FinalAnswerTool()
@@ -86,7 +85,7 @@ with open("prompts.yaml", 'r') as stream:
86
 
87
  agent = CodeAgent(
88
  model=model,
89
- tools=[final_answer, get_current_time_in_timezone, pull_rates], ## add your tools here (don't remove final answer)
90
  max_steps=10,
91
  verbosity_level=1,
92
  grammar=None,
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # Dictionary mapping user-friendly tenors to their FRED IDs.
10
+ yield_curve_mappings = {
11
+ '1 month': 'DGS1MO',
12
+ '3 month': 'DGS3MO',
13
+ '6 month': 'DGS6MO',
14
+ '1 year': 'DGS1',
15
+ '2 year': 'DGS2',
16
+ '3 year': 'DGS3',
17
+ '5 year': 'DGS5',
18
+ '7 year': 'DGS7',
19
+ '10 year': 'DGS10',
20
+ '20 year': 'DGS20',
21
+ '30 year': 'DGS30',
22
+ 'sofr': 'SOFR'}
23
+
24
+ # reverse for english description
25
+ yc_rev = {}
26
+ for k,v in yield_curve_mappings.items():
27
+ yc_rev[v] = k
28
+
29
  @tool
30
+ def convert_to_fred_id(request: str) -> str:
31
+ """A tool that converts a natural language yield request into a FRED ID.
 
32
  Args:
33
+ request: A string representing a yield in natural language (e.g., '10 year yield').
34
+ Returns:
35
+ A string of the corresponding 'fred_id' or 'Not Found' if no match is found.
36
  """
37
+ request = request.lower()
38
+ for phrase, fred_id in yield_curve_mappings.items():
39
+ if phrase in request:
40
+ return fred_id
41
+ return "No matching FRED ID found for the given request."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  @tool
44
+ def pull_rates(fred_id: str) -> str:
45
+ """A tool that fetches the latest interest or treasury rate from FRED.
46
  Args:
47
+ fred_id: A string representing a valid FRED ID to pull from the FRED website.
48
+ Returns:
49
+ A string of the corresponding 'fred_id' and yield value.
50
  """
51
+ fred_api_url = f"https://fred.stlouisfed.org/graph/fredgraph.csv?id={fred_id}"
52
  try:
53
+ import pandas as pd
54
+ df = pd.read_csv(fred_api_url)
55
+ latest_rate = df.loc[df['observation_date']==df['observation_date'].max(), fred_id].values[0]
56
+ # Find the matching phrase for the FRED ID to return a human-readable name
57
+ for phrase, id in yield_curve_mappings.items():
58
+ if id == fred_id:
59
+ return f"The current {phrase} yield is {latest_rate}%"
60
+ return f"The current {fred_id} ({yc_rev[fred_id]}) yield is {latest_rate}%"
61
  except Exception as e:
62
+ return f"Error fetching rates from FRED for {fred_id}."
63
 
64
 
65
  final_answer = FinalAnswerTool()
 
85
 
86
  agent = CodeAgent(
87
  model=model,
88
+ tools=[final_answer, get_current_time_in_timezone, convert_to_fred_id, pull_rates], ## add your tools here (don't remove final answer)
89
  max_steps=10,
90
  verbosity_level=1,
91
  grammar=None,