Skip to content

Commit f0693ad

Browse files
authored
Add weather, climate, air quality and geocoding tool from Measure Space (#18909)
1 parent 3cbe715 commit f0693ad

File tree

11 files changed

+4395
-0
lines changed

11 files changed

+4395
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
llama_index/_static
2+
.DS_Store
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
bin/
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
etc/
21+
include/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
share/
27+
var/
28+
wheels/
29+
pip-wheel-metadata/
30+
share/python-wheels/
31+
*.egg-info/
32+
.installed.cfg
33+
*.egg
34+
MANIFEST
35+
36+
# PyInstaller
37+
# Usually these files are written by a python script from a template
38+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
39+
*.manifest
40+
*.spec
41+
42+
# Installer logs
43+
pip-log.txt
44+
pip-delete-this-directory.txt
45+
46+
# Unit test / coverage reports
47+
htmlcov/
48+
.tox/
49+
.nox/
50+
.coverage
51+
.coverage.*
52+
.cache
53+
nosetests.xml
54+
coverage.xml
55+
*.cover
56+
*.py,cover
57+
.hypothesis/
58+
.pytest_cache/
59+
.ruff_cache
60+
61+
# Translations
62+
*.mo
63+
*.pot
64+
65+
# Django stuff:
66+
*.log
67+
local_settings.py
68+
db.sqlite3
69+
db.sqlite3-journal
70+
71+
# Flask stuff:
72+
instance/
73+
.webassets-cache
74+
75+
# Scrapy stuff:
76+
.scrapy
77+
78+
# Sphinx documentation
79+
docs/_build/
80+
81+
# PyBuilder
82+
target/
83+
84+
# Jupyter Notebook
85+
.ipynb_checkpoints
86+
notebooks/
87+
88+
# IPython
89+
profile_default/
90+
ipython_config.py
91+
92+
# pyenv
93+
.python-version
94+
95+
# pipenv
96+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
97+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
98+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
99+
# install all needed dependencies.
100+
#Pipfile.lock
101+
102+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
103+
__pypackages__/
104+
105+
# Celery stuff
106+
celerybeat-schedule
107+
celerybeat.pid
108+
109+
# SageMath parsed files
110+
*.sage.py
111+
112+
# Environments
113+
.env
114+
.venv
115+
env/
116+
venv/
117+
ENV/
118+
env.bak/
119+
venv.bak/
120+
pyvenv.cfg
121+
122+
# Spyder project settings
123+
.spyderproject
124+
.spyproject
125+
126+
# Rope project settings
127+
.ropeproject
128+
129+
# mkdocs documentation
130+
/site
131+
132+
# mypy
133+
.mypy_cache/
134+
.dmypy.json
135+
dmypy.json
136+
137+
# Pyre type checker
138+
.pyre/
139+
140+
# Jetbrains
141+
.idea
142+
modules/
143+
*.swp
144+
145+
# VsCode
146+
.vscode
147+
148+
# pipenv
149+
Pipfile
150+
Pipfile.lock
151+
152+
# pyright
153+
pyrightconfig.json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CHANGELOG
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) Jerry Liu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)
2+
3+
help: ## Show all Makefile targets.
4+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'
5+
6+
format: ## Run code autoformatters (black).
7+
pre-commit install
8+
git ls-files | xargs pre-commit run black --files
9+
10+
lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
11+
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files
12+
13+
test: ## Run tests via pytest.
14+
pytest tests
15+
16+
watch-docs: ## Build and watch documentation.
17+
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Measure Space Weather, Climate, Air Quality and Geocoding Tool
2+
3+
```bash
4+
pip install llama-index-tools-measurespace
5+
```
6+
7+
This tool connects to the [MeasureSpace](https://measurespace.io/documentation)'s API, using the [measure-space-api](https://pypi.org/project/measure-space-api/) Python package. You must initialize the tool with corresponding API keys from [MeasureSpace](https://measurespace.io/pricing) and [OpenAI](https://platform.openai.com/api-keys) (if you use OpenAI).
8+
9+
The tool has access to the following functions:
10+
11+
- hourly weather forecast (next 5 days)
12+
- daily weather forecast (next 15 days)
13+
- daily climate forecast (next 10 months)
14+
- daily air quality forecast (next 5 days)
15+
- get latitude and longitude from given city names
16+
- get nearest city for given latitude and longitude
17+
18+
## Usage
19+
20+
Assume you have an `.env` file with the following content:
21+
22+
```env
23+
GEOCODING_API_KEY=<your-geocoding-api-key>
24+
HOURLY_WEATHER_API_KEY=<your-hourly-weather-api-key>
25+
DAILY_WEATHER_API_KEY=R<your-daily-weather-api-key>
26+
DAILY_CLIMATE_API_KEY=<your-daily-climate-api-key>
27+
AIR_QUALITY_API_KEY=<your-air-quality-api-key>
28+
OPENAI_API_KEY=<your-openai-api-key>
29+
```
30+
31+
Note that you only need the API keys if you need the services.
32+
33+
```python
34+
from llama_index.tools.measurespace import MeasureSpaceToolSpec
35+
from llama_index.agent.openai import OpenAIAgent
36+
from dotenv import load_dotenv
37+
import os
38+
39+
load_dotenv()
40+
41+
api_keys = {
42+
"hourly_weather": os.getenv("HOURLY_WEATHER_API_KEY"),
43+
"daily_weather": os.getenv("DAILY_WEATHER_API_KEY"),
44+
"daily_climate": os.getenv("DAILY_CLIMATE_API_KEY"),
45+
"air_quality": os.getenv("AIR_QUALITY_API_KEY"),
46+
"geocoding": os.getenv("GEOCODING_API_KEY"),
47+
}
48+
49+
tool_spec = MeasureSpaceToolSpec(api_keys)
50+
agent = OpenAIAgent.from_tools(tool_spec.to_tool_list())
51+
52+
agent.chat("How's the temperature for New York in next 3 days?")
53+
agent.chat("What's the latitude and longitude of New York?")
54+
55+
# get a list of tools
56+
for tool in tool_spec.to_tool_list():
57+
print(tool.metadata.name)
58+
59+
# Use a specific tool
60+
tool_spec.get_daily_weather_forecast("New York")
61+
tool_spec.get_latitude_longitude_from_location("New York")
62+
```
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "8a937a74",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from llama_index.tools.measurespace import MeasureSpaceToolSpec\n",
11+
"from llama_index.agent.openai import OpenAIAgent\n",
12+
"from dotenv import load_dotenv\n",
13+
"import os\n",
14+
"\n",
15+
"load_dotenv()\n",
16+
"\n",
17+
"api_keys = {\n",
18+
" 'hourly_weather': os.getenv('HOURLY_WEATHER_API_KEY'),\n",
19+
" 'daily_weather': os.getenv('DAILY_WEATHER_API_KEY'),\n",
20+
" 'daily_climate': os.getenv('DAILY_CLIMATE_API_KEY'),\n",
21+
" 'air_quality': os.getenv('AIR_QUALITY_API_KEY'),\n",
22+
" 'geocoding': os.getenv('GEOCODING_API_KEY'),\n",
23+
"}\n",
24+
"\n",
25+
"tool_spec = MeasureSpaceToolSpec(api_keys)"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"id": "39c37351",
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"name": "stdout",
36+
"output_type": "stream",
37+
"text": [
38+
"get_hourly_weather_forecast\n",
39+
"get_daily_weather_forecast\n",
40+
"get_daily_climate_forecast\n",
41+
"get_daily_air_quality_forecast\n",
42+
"get_latitude_longitude_from_location\n",
43+
"get_location_from_latitude_longitude\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"for tool in tool_spec.to_tool_list():\n",
49+
" print(tool.metadata.name)\n"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"id": "a61e1618",
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"data": {
60+
"text/plain": [
61+
"[Document(id_='dfe2020a-f0e8-4625-b5a7-265fd9aaf8f5', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-05-30'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 18.81 mm,daily minimum temperature: 16.06 C,daily maximum temperature: 25.05 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
62+
" Document(id_='89fb5a5f-03f4-463a-ab8c-b0d3eb5cc556', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-05-31'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 104.25 mm,daily minimum temperature: 13.16 C,daily maximum temperature: 20.74 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
63+
" Document(id_='cb4d21d8-dadb-40c0-a73d-5934f22820e8', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-01'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.0 mm,daily minimum temperature: 11.83 C,daily maximum temperature: 19.86 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
64+
" Document(id_='d23e486a-6af9-4b61-a469-aeab2cfc8bea', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-02'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.25 mm,daily minimum temperature: 12.08 C,daily maximum temperature: 23.35 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
65+
" Document(id_='d0818797-81a6-46fe-863e-d0ae0cf22f13', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-03'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 1.19 mm,daily minimum temperature: 15.05 C,daily maximum temperature: 28.45 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
66+
" Document(id_='893b5272-6a59-49e6-806d-de655cd10c72', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-04'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.0 mm,daily minimum temperature: 19.89 C,daily maximum temperature: 32.15 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
67+
" Document(id_='5d8b77c6-d0df-4291-8fdb-b1c6085f6c3d', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-05'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.25 mm,daily minimum temperature: 21.11 C,daily maximum temperature: 30.47 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
68+
" Document(id_='b718fb88-29cb-45b1-9039-7549750b73e3', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-06'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.44 mm,daily minimum temperature: 18.31 C,daily maximum temperature: 23.11 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
69+
" Document(id_='5ebc2b10-6ec0-4e08-bbd5-108fb36064a0', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-07'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 14.19 mm,daily minimum temperature: 16.9 C,daily maximum temperature: 21.35 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
70+
" Document(id_='ac4ae720-2246-4482-babd-9cc131eb9d17', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-08'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 2.0 mm,daily minimum temperature: 19.74 C,daily maximum temperature: 26.92 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
71+
" Document(id_='ef2a553f-edb3-4398-bd79-09777a43da93', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-09'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.12 mm,daily minimum temperature: 20.44 C,daily maximum temperature: 29.53 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
72+
" Document(id_='b7d01619-0956-40f6-9336-075220132a2f', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-10'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 4.62 mm,daily minimum temperature: 22.0 C,daily maximum temperature: 31.05 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
73+
" Document(id_='1aefe45c-04ae-47aa-8d89-2add1ab22afb', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-11'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 27.81 mm,daily minimum temperature: 20.97 C,daily maximum temperature: 26.78 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
74+
" Document(id_='4ecd2db3-f0f4-4821-9496-7dd06a0b4915', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-12'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 3.12 mm,daily minimum temperature: 21.89 C,daily maximum temperature: 26.22 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
75+
" Document(id_='0fe23a8c-da07-4f1f-9002-a6529f0c3487', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-13'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 1.19 mm,daily minimum temperature: 21.6 C,daily maximum temperature: 28.62 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}')]"
76+
]
77+
},
78+
"execution_count": null,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"tool_spec.get_daily_weather_forecast('New York')"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"id": "a6b1429e",
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"data": {
95+
"text/plain": [
96+
"[Document(id_='6b841c56-a3a8-45ff-ab7f-c8c02d4392e7', embedding=None, metadata={'Latitude and longitude for location': 'New York'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='latitude: 40.714, longitude: -74.006', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}')]"
97+
]
98+
},
99+
"execution_count": null,
100+
"metadata": {},
101+
"output_type": "execute_result"
102+
}
103+
],
104+
"source": [
105+
"tool_spec.get_latitude_longitude_from_location('New York')"
106+
]
107+
}
108+
],
109+
"metadata": {
110+
"kernelspec": {
111+
"display_name": ".venv",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 5
129+
}

0 commit comments

Comments
 (0)