-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelper.py
101 lines (89 loc) · 3.22 KB
/
Helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from sqlalchemy import create_engine
import pandas as pd
"""
os.makedirs("data",exist_ok=True)
!wget https://covidtracking.com/data/download/all-states-history.csv -P ./data/
file_url = "./data/all-states-history.csv"
"""
df = pd.read_csv("./data/all-states-history.csv").fillna(value = 0)
database_file_path = "./db/test.db"
engine = create_engine(f'sqlite:///{database_file_path}')
df.to_sql('all_states_history', con=engine, if_exists='replace', index=False)
tools_sql = [
{
"type": "function",
"function": {
"name": "get_hospitalized_increase_for_state_on_date",
"description": "Retrieves the daily increase in hospitalizations for a specific state on a specific date.",
"parameters": {
"type": "object",
"properties": {
"state_abbr": {
"type": "string",
"description": "The abbreviation of the state (e.g., 'NY', 'CA')."
},
"specific_date": {
"type": "string",
"description": "The specific date for the query in 'YYYY-MM-DD' format."
}
},
"required": ["state_abbr", "specific_date"]
}
}
},
{
"type": "function",
"function": {
"name": "get_positive_cases_for_state_on_date",
"description": "Retrieves the daily increase in positive cases for a specific state on a specific date.",
"parameters": {
"type": "object",
"properties": {
"state_abbr": {
"type": "string",
"description": "The abbreviation of the state (e.g., 'NY', 'CA')."
},
"specific_date": {
"type": "string",
"description": "The specific date for the query in 'YYYY-MM-DD' format."
}
},
"required": ["state_abbr", "specific_date"]
}
}
}
]
# define functions
import numpy as np
def get_hospitalized_increase_for_state_on_date(state_abbr, specific_date):
try:
query = f"""
SELECT date, hospitalizedIncrease
FROM all_states_history
WHERE state = '{state_abbr}' AND date = '{specific_date}';
"""
with engine.connect() as connection:
result = pd.read_sql_query(query, connection)
if not result.empty:
return result.to_dict('records')[0]
else:
return np.nan
except Exception as e:
print(e)
return np.nan
def get_positive_cases_for_state_on_date(state_abbr, specific_date):
try:
query = f"""
SELECT date, state, positiveIncrease AS positive_cases
FROM all_states_history
WHERE state = '{state_abbr}' AND date = '{specific_date}';
"""
with engine.connect() as connection:
result = pd.read_sql_query(query, connection)
if not result.empty:
return result.to_dict('records')[0]
else:
return np.nan
except Exception as e:
print(e)
return np.nan