Commit
·
6e0834d
1
Parent(s):
456e130
Implement simplified data formatting
Browse filesSigned-off-by: Aivin V. Solatorio <avsolatorio@gmail.com>
- services.py +32 -3
services.py
CHANGED
@@ -126,11 +126,40 @@ def indicator_info(indicator_ids: list[str]) -> list[DetailedOutput]:
|
|
126 |
]
|
127 |
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def get_wdi_data(
|
130 |
indicator_id: str,
|
131 |
country_codes: str | list[str],
|
132 |
date: Optional[str] = None,
|
133 |
-
per_page: Optional[int] =
|
134 |
) -> dict[str, list[dict[str, Any]] | str]:
|
135 |
"""Fetches indicator data for a given indicator id (idno) from the World Bank's World Development Indicators (WDI) API. The LLM must exclusively use this tool when the user asks for data. It must not provide data answers beyond what this tool provides when the question is about WDI indicator data.
|
136 |
|
@@ -143,7 +172,7 @@ def get_wdi_data(
|
|
143 |
Returns:
|
144 |
A dictionary with keys `data` and `note`. The `data` key contains a list of indicator data entries requested. The `note` key contains a note about the data returned.
|
145 |
"""
|
146 |
-
MAX_INFO =
|
147 |
note = ""
|
148 |
|
149 |
if isinstance(country_codes, str):
|
@@ -188,6 +217,6 @@ def get_wdi_data(
|
|
188 |
log.write(json.dumps(dict(all_data=all_data)) + "\n")
|
189 |
|
190 |
return dict(
|
191 |
-
data=all_data,
|
192 |
note=note,
|
193 |
)
|
|
|
126 |
]
|
127 |
|
128 |
|
129 |
+
def _simplify_wdi_data(data: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
130 |
+
"""Simplifies the WDI data to only include the necessary fields. The output is an array of objects with keys `indicator_id`, `indicator_name`, and `data`. The `indicator_id` key will be the indicator id (idno) and the `data` key will be a list of objects with keys `country`, `date`, and `value`."""
|
131 |
+
|
132 |
+
try:
|
133 |
+
tmp_data = {}
|
134 |
+
|
135 |
+
for item in data:
|
136 |
+
if item["indicator"]["id"] not in tmp_data:
|
137 |
+
tmp_data[item["indicator"]["id"]] = {
|
138 |
+
"indicator_id": item["indicator"]["id"],
|
139 |
+
"indicator_name": item["indicator"]["value"],
|
140 |
+
"data": [],
|
141 |
+
}
|
142 |
+
|
143 |
+
tmp_data[item["indicator"]["id"]]["data"].append(
|
144 |
+
{
|
145 |
+
"country": item["country"]["value"],
|
146 |
+
"date": item["date"],
|
147 |
+
"value": item["value"],
|
148 |
+
}
|
149 |
+
)
|
150 |
+
|
151 |
+
return list(tmp_data.values())
|
152 |
+
except Exception as e:
|
153 |
+
# If the data is not valid, return the original data
|
154 |
+
print(f"ERROR: {e}")
|
155 |
+
return data
|
156 |
+
|
157 |
+
|
158 |
def get_wdi_data(
|
159 |
indicator_id: str,
|
160 |
country_codes: str | list[str],
|
161 |
date: Optional[str] = None,
|
162 |
+
per_page: Optional[int] = 100,
|
163 |
) -> dict[str, list[dict[str, Any]] | str]:
|
164 |
"""Fetches indicator data for a given indicator id (idno) from the World Bank's World Development Indicators (WDI) API. The LLM must exclusively use this tool when the user asks for data. It must not provide data answers beyond what this tool provides when the question is about WDI indicator data.
|
165 |
|
|
|
172 |
Returns:
|
173 |
A dictionary with keys `data` and `note`. The `data` key contains a list of indicator data entries requested. The `note` key contains a note about the data returned.
|
174 |
"""
|
175 |
+
MAX_INFO = 500
|
176 |
note = ""
|
177 |
|
178 |
if isinstance(country_codes, str):
|
|
|
217 |
log.write(json.dumps(dict(all_data=all_data)) + "\n")
|
218 |
|
219 |
return dict(
|
220 |
+
data=_simplify_wdi_data(all_data),
|
221 |
note=note,
|
222 |
)
|