File size: 2,342 Bytes
eeb0f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from config.settings import client, MODEL

rules_file = os.path.join(os.path.dirname(__file__), 'rules.json')

def get_basic_recommendations(user_data, rules_file=rules_file):
    with open(rules_file, 'r', encoding='utf-8') as f:
        rules_data = json.load(f)

    recommendations = []
    for cond in user_data.get('health_conditions', []):
        if cond.lower() in rules_data.get('conditions', {}):
            recommendations.extend(rules_data['conditions'][cond.lower()])

    goal = user_data.get('goal', '').lower()
    for key, recs in rules_data.get('goals', {}).items():
        if key in goal:
            recommendations.extend(recs)

    level = user_data.get('fitness_level', 'beginner').lower()
    if level in rules_data.get('fitness_level', {}):
        recommendations.extend(rules_data['fitness_level'][level])

    return recommendations

def generate_exercise_plan(user_data):
    rules = get_basic_recommendations(user_data)
    system_prompt = f"""
Bạn là huấn luyện viên cá nhân vui tính 💪🔥.

Thông tin người dùng:
- Tuổi: {user_data.get('age', 'chưa biết')}
- Giới tính: {user_data.get('gender', 'chưa biết')}
- Cân nặng: {user_data.get('weight', 'chưa biết')}
- Chiều cao: {user_data.get('height', 'chưa biết')}
- Thể lực hiện tại: {user_data.get('fitness_level', 'nhẹ')}
- Mục tiêu: {user_data.get('goal', 'cải thiện sức khỏe')}
- Thời gian rảnh mỗi ngày: {user_data.get('available_time', 30)} phút
- Bệnh nền: {', '.join(user_data.get('health_conditions', []))}

Rule cần tuân theo:
{chr(10).join(f"- {r}" for r in rules)}

Yêu cầu:
- Tạo kế hoạch 7 ngày dạng bảng
- Friendly intro trước khi xuất plan
- Mỗi ngày có: Tên buổi tập, thời gian, danh sách bài tập, số hiệp, số lần / thời gian, lưu ý an toàn
- Không hỏi lại thông tin, nếu thiếu dùng default
- Không nhét link hoặc placeholder
"""

    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": "Hãy tạo kế hoạch 7 ngày theo yêu cầu."}
        ],
        temperature=0.7,
        max_tokens=3000
    )
    return response.choices[0].message.content