-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathmain.py
More file actions
285 lines (251 loc) · 10.5 KB
/
main.py
File metadata and controls
285 lines (251 loc) · 10.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import json
from datetime import datetime
import requests
from src.agent.capability import MatchingCapability
from src.agent.capability_worker import CapabilityWorker
from src.main import AgentWorker
# =============================================================================
# ASTRONOMY & STARGAZING GUIDE
# Provides stargazing information using IPGeolocation Astronomy API and
# NASA APOD. Reports moon phase, sun/moon times, and LLM-enriched planet
# visibility info. Set API keys in the placeholders below.
# =============================================================================
EXIT_WORDS = {
"stop", "exit", "quit", "done", "cancel", "bye", "goodbye", "leave",
}
GEOCODE_URL = "https://nominatim.openstreetmap.org/search"
ASTRONOMY_URL = "https://api.ipgeolocation.io/astronomy"
NASA_APOD_URL = "https://api.nasa.gov/planetary/apod"
# Replace placeholders with real keys before deploying.
IPGEO_API_KEY = "YOUR_IPGEO_API_KEY_HERE"
NASA_API_KEY = "YOUR_NASA_API_KEY_HERE"
STARGAZING_PROMPT = (
"You are an astronomy and stargazing guide. Given the following astronomical "
"data for a location, create an engaging spoken summary for someone wanting "
"to stargaze tonight. Include:\n"
"1) Moon phase and how bright the sky will be\n"
"2) Best time to observe (after astronomical twilight)\n"
"3) What planets and constellations are likely visible based on the season "
"and location\n"
"Keep it to 3-4 conversational sentences. Be enthusiastic about astronomy."
)
APOD_PROMPT = (
"Briefly describe this NASA Astronomy Picture of the Day in one "
"conversational sentence suitable for voice. Title: {title}. "
"Description: {explanation}"
)
class AstronomyGuideCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
# Do not change following tag of register capability
# {{register capability}}
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
self.worker.session_tasks.create(self.run())
async def run(self):
try:
self.worker.editor_logging_handler.info(
"[AstronomyGuide] Ability started"
)
ipgeo_key = self._resolve_ipgeo_api_key()
await self.capability_worker.speak(
"Welcome to the stargazing guide! "
"What's your location? I'll tell you what's in the sky tonight."
)
user_input = await self.capability_worker.user_response()
if not user_input or not user_input.strip():
await self.capability_worker.speak(
"I didn't catch that. Try again later."
)
return
if any(w in user_input.lower() for w in EXIT_WORDS):
await self.capability_worker.speak("Clear skies! Goodbye.")
return
location = user_input.strip()
await self.capability_worker.speak(
f"Looking up the sky above {location}."
)
coords = self._geocode(location)
if not coords:
await self.capability_worker.speak(
"I couldn't find that location. Try a different city name."
)
return
lat, lon = coords
astro_data = self._fetch_astronomy(lat, lon, ipgeo_key)
if astro_data:
summary = self._build_stargazing_summary(
astro_data, location, lat, lon
)
await self.capability_worker.speak(summary)
else:
fallback = self._build_fallback_summary(location, lat, lon)
await self.capability_worker.speak(fallback)
apod = self._fetch_apod()
if apod:
await self.capability_worker.speak(
f"Also, today's NASA astronomy picture: {apod}"
)
await self.capability_worker.speak(
"Want to know about a specific object in the sky? "
"Ask me, or say done to exit."
)
follow_up = await self.capability_worker.user_response()
if follow_up and follow_up.strip() and not any(
w in follow_up.lower() for w in EXIT_WORDS
):
try:
response = self.capability_worker.text_to_text_response(
f"The user is stargazing from {location} (lat {lat}, "
f"lon {lon}) on {datetime.now().strftime('%B %d, %Y')}. "
f"They asked: {follow_up}",
system_prompt=(
"You are an astronomy expert. Answer the user's "
"question about the night sky concisely in 2-3 "
"sentences for voice. Include practical observation "
"tips if relevant."
),
)
await self.capability_worker.speak(response)
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Follow-up error: {e}"
)
await self.capability_worker.speak(
"Happy stargazing! Clear skies to you."
)
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Unexpected error: {e}"
)
await self.capability_worker.speak(
"Something went wrong. Closing the astronomy guide."
)
finally:
self.worker.editor_logging_handler.info(
"[AstronomyGuide] Ability ended"
)
self.capability_worker.resume_normal_flow()
def _geocode(self, location: str) -> tuple:
try:
resp = requests.get(
GEOCODE_URL,
params={"q": location, "format": "json", "limit": 1},
headers={"User-Agent": "OpenHome-Astronomy-Ability"},
timeout=5,
)
data = resp.json()
if data:
return float(data[0]["lat"]), float(data[0]["lon"])
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Geocoding error: {e}"
)
return None
def _fetch_astronomy(self, lat: float, lon: float, api_key: str) -> dict:
if not api_key:
self.worker.editor_logging_handler.info(
"[AstronomyGuide] No IPGEO_API_KEY, skipping astronomy API"
)
return None
try:
today = datetime.now().strftime("%Y-%m-%d")
resp = requests.get(
ASTRONOMY_URL,
params={
"apiKey": api_key,
"lat": lat,
"long": lon,
"date": today,
},
timeout=5,
)
if resp.status_code == 200:
return resp.json()
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Astronomy API returned {resp.status_code}"
)
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Astronomy API error: {e}"
)
return None
def _fetch_apod(self) -> str:
try:
nasa_key = self._resolve_nasa_api_key()
resp = requests.get(
NASA_APOD_URL,
params={"api_key": nasa_key},
timeout=5,
)
if resp.status_code == 200:
data = resp.json()
title = data.get("title", "")
explanation = data.get("explanation", "")[:300]
try:
result = self.capability_worker.text_to_text_response(
APOD_PROMPT.format(title=title, explanation=explanation)
)
return result.strip()
except Exception:
return f"{title}."
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] NASA APOD error: {e}"
)
return ""
def _resolve_ipgeo_api_key(self) -> str:
api_key = IPGEO_API_KEY.strip()
if not api_key or api_key == "YOUR_IPGEO_API_KEY_HERE":
return ""
return api_key
def _resolve_nasa_api_key(self) -> str:
api_key = NASA_API_KEY.strip()
if not api_key or api_key == "YOUR_NASA_API_KEY_HERE":
return "DEMO_KEY"
return api_key
def _build_stargazing_summary(
self, astro_data: dict, location: str, lat: float, lon: float
) -> str:
data_text = json.dumps(astro_data, indent=2)
today = datetime.now().strftime("%B %d, %Y")
prompt = (
f"Location: {location} (lat {lat}, lon {lon})\n"
f"Date: {today}\n"
f"Astronomical data:\n{data_text}"
)
try:
response = self.capability_worker.text_to_text_response(
prompt, system_prompt=STARGAZING_PROMPT
)
return response
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Summary error: {e}"
)
moon_phase = astro_data.get("moon_phase", "unknown")
return (
f"Tonight in {location}, the moon phase is {moon_phase}. "
"Check the sky after sunset for the best viewing."
)
def _build_fallback_summary(
self, location: str, lat: float, lon: float
) -> str:
today = datetime.now().strftime("%B %d, %Y")
try:
response = self.capability_worker.text_to_text_response(
f"Location: {location} (lat {lat}, lon {lon}), Date: {today}. "
f"What would be visible in the night sky tonight?",
system_prompt=STARGAZING_PROMPT,
)
return response
except Exception as e:
self.worker.editor_logging_handler.error(
f"[AstronomyGuide] Fallback summary error: {e}"
)
return (
f"I couldn't get detailed astronomy data for {location}, "
"but the best stargazing is usually after 9 PM when the sky "
"is fully dark. Look for prominent constellations like Orion."
)