AI-based personalized meditation and relaxation system
Meditation apps (Calm, Headspace) use AI to select practices based on the user's current state. There's no point in offering a 30-minute meditation to someone "5 minutes before a meeting." AI identifies the context and suggests a practice the user can realistically complete.
Contextual recommendation of meditation practices
from anthropic import Anthropic
import json
from datetime import datetime
def recommend_meditation_session(user_state: dict,
user_history: list[dict]) -> dict:
"""
Контекстная рекомендация медитации.
user_state: mood (1-5), stress_level (1-5), available_minutes, time_of_day
"""
llm = Anthropic()
# Анализ истории: какие практики пользователь завершает
if user_history:
completed = [s for s in user_history if s.get('completed')]
preferred_types = {}
for session in completed:
t = session.get('type', 'breathing')
preferred_types[t] = preferred_types.get(t, 0) + 1
top_type = max(preferred_types, key=preferred_types.get) if preferred_types else 'breathing'
completion_rate = len(completed) / max(len(user_history), 1)
else:
top_type = 'breathing'
completion_rate = 0.5
# Правила выбора практики
mood = user_state.get('mood', 3)
stress = user_state.get('stress_level', 3)
available_min = user_state.get('available_minutes', 10)
time_of_day = user_state.get('time_of_day', 'afternoon')
if stress >= 4:
session_type = 'breathing' # Быстрее всего снижает стресс
elif mood <= 2:
session_type = 'body_scan' # Для усталости
elif time_of_day == 'morning':
session_type = 'energizing'
elif time_of_day == 'evening':
session_type = 'sleep_preparation'
else:
session_type = top_type
# Длительность по доступному времени
if available_min <= 5:
duration = 3
elif available_min <= 15:
duration = 10
else:
duration = min(available_min, 20)
# LLM для персонализированного введения
response = llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=150,
messages=[{
"role": "user",
"content": f"""Write a personalized intro for a meditation session in Russian.
User state: mood {mood}/5, stress {stress}/5, available time {available_min} min
Time of day: {time_of_day}
Session type: {session_type}, duration: {duration} min
Completion rate: {completion_rate:.0%}
Write 2-3 sentences:
1. Acknowledge their current state
2. Explain why this specific practice will help right now
Be warm, non-judgmental, concise."""
}]
)
return {
'session_type': session_type,
'duration_minutes': duration,
'personalized_intro': response.content[0].text,
'completion_prediction': min(0.95, completion_rate + 0.1) if session_type == top_type else completion_rate,
}
Personalized meditation recommendations increase session completion rates from 35-45% to 60-75%. Key insight: a short session completed to the end is more valuable than a long one abandoned halfway through—AI should optimize for realistic capabilities, not ideal ones.







