63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
"""Generate AI activity summaries using Gemini."""
|
|
|
|
from backend.app.models.activity import Activity, ActivityMetrics
|
|
from backend.app.services.gemini_client import chat_async
|
|
|
|
SYSTEM_PROMPT = """You are VeloBrain, an AI cycling coach.
|
|
Analyze the cycling activity data and provide a concise, insightful summary in 3-5 sentences.
|
|
Focus on: performance highlights, pacing strategy, areas for improvement, and training effect.
|
|
Be specific with numbers. Use a friendly, coaching tone.
|
|
Respond in Russian.
|
|
Use a HTML text formatting."""
|
|
|
|
|
|
def _build_activity_prompt(activity: Activity, rider_ftp: float | None = None) -> str:
|
|
m: ActivityMetrics | None = activity.metrics
|
|
lines = [
|
|
f"Activity: {activity.name or 'Ride'}",
|
|
f"Type: {activity.activity_type}",
|
|
f"Duration: {activity.duration // 3600}h {(activity.duration % 3600) // 60}m",
|
|
]
|
|
|
|
if activity.distance:
|
|
lines.append(f"Distance: {activity.distance / 1000:.1f} km")
|
|
if activity.elevation_gain:
|
|
lines.append(f"Elevation: {activity.elevation_gain:.0f} m")
|
|
|
|
if m:
|
|
if m.avg_power:
|
|
lines.append(f"Avg Power: {m.avg_power:.0f} W")
|
|
if m.normalized_power:
|
|
lines.append(f"Normalized Power: {m.normalized_power:.0f} W")
|
|
if m.tss:
|
|
lines.append(f"TSS: {m.tss:.0f}")
|
|
if m.intensity_factor:
|
|
lines.append(f"IF: {m.intensity_factor:.2f}")
|
|
if m.variability_index:
|
|
lines.append(f"VI: {m.variability_index:.2f}")
|
|
if m.avg_hr:
|
|
lines.append(f"Avg HR: {m.avg_hr} bpm")
|
|
if m.max_hr:
|
|
lines.append(f"Max HR: {m.max_hr} bpm")
|
|
if m.avg_cadence:
|
|
lines.append(f"Avg Cadence: {m.avg_cadence} rpm")
|
|
|
|
if rider_ftp:
|
|
lines.append(f"Rider FTP: {rider_ftp:.0f} W")
|
|
|
|
intervals = activity.intervals or []
|
|
work_intervals = [i for i in intervals if i.interval_type == "work"]
|
|
if work_intervals:
|
|
lines.append(f"Work intervals: {len(work_intervals)}")
|
|
powers = [i.avg_power for i in work_intervals if i.avg_power]
|
|
if powers:
|
|
lines.append(f"Interval avg powers: {', '.join(f'{p:.0f}W' for p in powers)}")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
async def generate_summary(activity: Activity, rider_ftp: float | None = None) -> str:
|
|
prompt = _build_activity_prompt(activity, rider_ftp)
|
|
messages = [{"role": "user", "text": prompt}]
|
|
return await chat_async(messages, system_instruction=SYSTEM_PROMPT, temperature=0.5)
|