-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_parser.py
More file actions
346 lines (281 loc) · 12.8 KB
/
Copy pathllm_parser.py
File metadata and controls
346 lines (281 loc) · 12.8 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
"""
LLM Parser for Life OS
Uses local Ollama to parse natural language into structured data.
"""
import os
import json
import logging
import re
from datetime import datetime, timedelta
from typing import Dict, Optional, Union, List
import httpx
logger = logging.getLogger(__name__)
class LLMParser:
def __init__(self):
self.ollama_url = os.getenv('OLLAMA_URL', 'http://localhost:11434')
self.model = os.getenv('LLM_MODEL', 'llama3.1:8b')
async def parse_message(self, message: str) -> Optional[Union[Dict, List[Dict]]]:
"""Parse a natural language message into structured data"""
system_prompt = self._get_system_prompt()
prompt = f"""
Analyze this user input and extract structured data:
"{message}"
Current time: {datetime.now().strftime('%Y-%m-%d %H:%M')}
Return ONLY a JSON object. No preamble, no explanation, just the JSON.
"""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.ollama_url}/api/generate",
json={
"model": self.model,
"prompt": prompt,
"system": system_prompt,
"stream": False,
"format": "json"
}
)
if response.status_code != 200:
logger.error(f"Ollama error: {response.status_code}")
return None
result = response.json()
response_text = result.get('response', '')
# Clean and parse JSON
parsed = self._extract_json(response_text)
if parsed:
# Post-process the parsed data
if isinstance(parsed, list):
parsed = [self._post_process(entry, message) for entry in parsed if isinstance(entry, dict)]
elif isinstance(parsed, dict) and isinstance(parsed.get("entries"), list):
parsed["entries"] = [
self._post_process(entry, message)
for entry in parsed["entries"]
if isinstance(entry, dict)
]
elif isinstance(parsed, dict):
parsed = self._post_process(parsed, message)
logger.info(f"Parsed: {json.dumps(parsed, indent=2)}")
return parsed
else:
logger.error(f"Could not parse JSON from: {response_text}")
return None
except Exception as e:
logger.error(f"Error calling Ollama: {e}")
return None
def _get_system_prompt(self) -> str:
"""Get the system prompt for the LLM"""
return """You are a life-logging data extraction AI. Your ONLY job is to analyze short chat messages and return structured JSON data.
CRITICAL RULES:
1. Return ONLY valid JSON - no markdown, no explanation, no preamble
2. If the user mentions multiple things in one message, return:
{"entries": [entry1, entry2, ...]}
3. Categorize each entry into one of these types:
- task_complete: User finished a task
- task_pending: User needs to do something
- food_log: User ate/drank something
- health_metric: User logged supplements or health data
- energy_level: User described their energy state
4. Do not log negative statements as positive entries. If the user says they did not take supplements, do not create a supplements entry.
JSON SCHEMAS:
task_complete:
{
"type": "task_complete",
"description": "clear task description",
"timestamp": "ISO datetime or null"
}
task_pending:
{
"type": "task_pending",
"description": "clear task description",
"deadline": "ISO datetime or null",
"priority": "low/medium/high",
"focus_required": true/false
}
food_log:
{
"type": "food_log",
"timestamp": "ISO datetime",
"items": ["item1", "item2"],
"macros": {
"carbs": "low/medium/high",
"protein": "low/medium/high",
"fat": "low/medium/high"
},
"energy_prediction": {
"status": "stable/crash_warning/boost_expected",
"time_of_crash": "ISO datetime or null",
"message": "explanation"
}
}
health_metric:
{
"type": "health_metric",
"timestamp": "ISO datetime",
"supplements": ["supplement1", "supplement2"],
"metrics": {"steps": 8000}
}
energy_level:
{
"type": "energy_level",
"level": 1-10,
"context": "why this energy level",
"timestamp": "ISO datetime"
}
ENERGY PREDICTION RULES:
- High carbs (rice, bread, pasta) → crash_warning in 30-60 mins
- High sugar (sweets, soda) → crash_warning in 20-45 mins
- Balanced protein+fat+carbs → stable
- High protein+fat → boost_expected
- Coffee/tea → boost_expected for 2-3 hours
TIME PARSING:
- "at 3 PM" → 15:00 today
- "yesterday" → yesterday's date
- "lunch" → 12:00-14:00
- "breakfast" → 07:00-09:00
- "dinner" → 18:00-21:00
- No time specified → current time
PRIORITY DETECTION:
- Words like "urgent", "ASAP", "critical" → high
- Words like "when you get a chance", "eventually" → low
- Default → medium
FOCUS REQUIRED:
- Tasks with "draft", "write", "design", "analyze", "research" → true
- Simple tasks like "call", "email", "buy" → false
Examples:
Input: "Finished drafting the affidavit"
Output: {"type": "task_complete", "description": "Draft the affidavit", "timestamp": null}
Input: "Ate dal and rice at 3 PM"
Output: {"type": "food_log", "timestamp": "2024-01-15T15:00:00", "items": ["dal", "rice"], "macros": {"carbs": "high", "protein": "medium", "fat": "low"}, "energy_prediction": {"status": "crash_warning", "time_of_crash": "2024-01-15T15:45:00", "message": "Heavy carbs detected. Energy dip expected in 45 mins."}}
Input: "Need to file petition by Friday"
Output: {"type": "task_pending", "description": "File petition", "deadline": "2024-01-19T17:00:00", "priority": "high", "focus_required": true}
Input: "Took vitamin D and magnesium"
Output: {"type": "health_metric", "timestamp": "2024-01-15T09:00:00", "supplements": ["vitamin D", "magnesium"], "metrics": {}}
Input: "Feeling sluggish after lunch"
Output: {"type": "energy_level", "level": 4, "context": "Sluggish feeling after lunch", "timestamp": "2024-01-15T13:30:00"}
Input: "ate tarri poha half plate and also did 8000 steps today and need to file circulation for tomorrow for x case"
Output: {"entries":[{"type":"food_log","timestamp":"2024-01-15T13:30:00","items":["tarri poha"],"macros":{"carbs":"high","protein":"medium","fat":"high"},"energy_prediction":{"status":"crash_warning","time_of_crash":"2024-01-15T14:15:00","message":"Tarri poha can be carb-heavy and oily."}},{"type":"health_metric","timestamp":"2024-01-15T13:30:00","supplements":[],"metrics":{"steps":8000}},{"type":"task_pending","description":"File circulation for x case","deadline":"2024-01-16T17:00:00","priority":"high","focus_required":true}]}
Remember: ONLY return the JSON object. Nothing else."""
def _extract_json(self, text: str):
"""Extract JSON from possibly messy text"""
# Try direct parse first
try:
return json.loads(text)
except json.JSONDecodeError:
pass
# Try to find JSON in markdown code blocks
json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL)
if json_match:
try:
return json.loads(json_match.group(1))
except json.JSONDecodeError:
pass
# Try to find any JSON object
json_match = re.search(r'\{.*\}', text, re.DOTALL)
if json_match:
try:
return json.loads(json_match.group(0))
except json.JSONDecodeError:
pass
return None
def _post_process(self, data: Dict, original_message: str) -> Dict:
"""Post-process parsed data for consistency"""
# Ensure timestamps are ISO format
if 'timestamp' in data and data['timestamp']:
if isinstance(data['timestamp'], str):
try:
dt = datetime.fromisoformat(data['timestamp'].replace('Z', '+00:00'))
data['timestamp'] = dt.isoformat()
except:
data['timestamp'] = datetime.now().isoformat()
# Handle relative times
if 'deadline' in data and data['deadline']:
data['deadline'] = self._parse_deadline(data['deadline'])
# Ensure energy level is integer
if 'level' in data:
try:
data['level'] = int(data['level'])
data['level'] = max(1, min(10, data['level'])) # Clamp to 1-10
except:
data['level'] = 5
return data
def _parse_deadline(self, deadline_str: str) -> Optional[str]:
"""Parse deadline string to ISO datetime"""
try:
# If already ISO format, return as-is
dt = datetime.fromisoformat(deadline_str.replace('Z', '+00:00'))
return dt.isoformat()
except:
pass
# Try relative parsing
now = datetime.now()
deadline_lower = deadline_str.lower()
if 'today' in deadline_lower:
return now.replace(hour=17, minute=0, second=0).isoformat()
elif 'tomorrow' in deadline_lower:
return (now + timedelta(days=1)).replace(hour=17, minute=0, second=0).isoformat()
elif 'monday' in deadline_lower:
days_ahead = 0 - now.weekday()
if days_ahead <= 0:
days_ahead += 7
return (now + timedelta(days=days_ahead)).replace(hour=17, minute=0).isoformat()
# Add more day parsing as needed
# Default: 1 week from now
return (now + timedelta(days=7)).replace(hour=17, minute=0).isoformat()
async def generate_daily_summary(self, data: Dict) -> str:
"""Generate a daily summary from aggregated data"""
summary_prompt = f"""Generate a brief, encouraging daily summary based on this data:
Tasks Completed: {data.get('tasks_completed', 0)}
Tasks Pending: {data.get('tasks_pending', 0)}
Meals Logged: {data.get('meals_logged', 0)}
Average Energy: {data.get('avg_energy', 'N/A')}/10
Energy Range: {data.get('min_energy', 'N/A')} - {data.get('max_energy', 'N/A')}
Top Foods: {', '.join(data.get('top_foods', []))}
Supplements Taken: {', '.join(data.get('supplements', []))}
Write a 2-3 sentence summary that:
1. Celebrates accomplishments
2. Notes energy patterns
3. Gives a supportive tip for tomorrow
Keep it personal, warm, and actionable. No bullet points."""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.ollama_url}/api/generate",
json={
"model": self.model,
"prompt": summary_prompt,
"stream": False
}
)
if response.status_code == 200:
result = response.json()
return result.get('response', 'No summary available.')
else:
return 'Summary generation unavailable.'
except Exception as e:
logger.error(f"Error generating summary: {e}")
return 'Summary generation failed.'
async def suggest_optimal_time(self, task_description: str, energy_history: list) -> str:
"""Suggest optimal time for a task based on energy patterns"""
if not energy_history:
return "No energy data available yet."
# Find average energy by hour
hourly_energy = {}
for entry in energy_history:
hour = entry['hour']
level = entry['level']
if hour not in hourly_energy:
hourly_energy[hour] = []
hourly_energy[hour].append(level)
# Calculate averages
avg_by_hour = {
hour: sum(levels) / len(levels)
for hour, levels in hourly_energy.items()
}
# Find peak hours
sorted_hours = sorted(avg_by_hour.items(), key=lambda x: x[1], reverse=True)
if sorted_hours:
best_hour = sorted_hours[0][0]
best_energy = sorted_hours[0][1]
return f"Based on your patterns, you're usually at {best_energy:.1f}/10 energy around {best_hour}:00. Great time for: {task_description}"
return "Track your energy for a few more days to get personalized suggestions!"