File size: 2,351 Bytes
d74863e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b27958e
d74863e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Recommendation Module 🎯

## Responsibility
This module handles **suggesting new educational videos** based on user interests.

## Functionality
1. Analyze user's saved notes.
2. Extract topics and categories.
3. Search **YouTube** for related videos.
4. Return a list of suggested educational videos.

## Files

### 1. `recommender.py`
- **Purpose:** Suggest educational videos from YouTube.
- **Main Class:** `RecommendationService`
- **Key Methods:**
  - `get_recommendations_for_user(user_id)` - Get general recommendations for user.
  - `get_youtube_recommendations(query)` - Search YouTube based on keywords.
  - `get_similar_notes(note_id)` - Get similar notes from the same category.

## How It Works
1. **Fetch Notes:** Read the user's last 5 saved notes.
2. **Extract Topics:** Use categories or titles.
3. **Enhance Search:** Add keywords like "educational", "tutorial", "lecture".
4. **Filter Results:** Select only embeddable videos.

## Proposed Enhancements
- [ ] Add caching for recommendations to reduce YouTube API calls.
- [ ] Use Machine Learning to improve recommendation accuracy.
- [ ] Add filter for video duration (avoid very long videos).
- [ ] Prioritize well-known educational channels.

## Testing
```python
from src.ai_modules.recommendation.recommender import RecommendationService
from src.db.database import get_session

recommender = RecommendationService()

# Get recommendations for user
async def test_recommendations():
    async with get_session() as session:
        recs = await recommender.get_recommendations_for_user(session, user_id=1)
        for video in recs:
            print(f"{video['title']} - {video['url']}")
```

## Libraries Used
- `google-api-python-client` - Search YouTube.
- `groq` - Extract topics via LLM.

## Important Notes
- YouTube API has a **daily quota limit**, use caching to reduce requests.
- Enhanced search adds "educational lecture tutorial" keywords for better educational results.
- System prioritizes videos from the same category.

## API Quota Management
It's recommended to cache results temporarily to avoid exhausting YouTube API quota:
```python
# Simple cache example
cache = {}

def get_cached_recommendations(query):
    if query in cache:
        return cache[query]
    
    results = youtube_search(query)
    cache[query] = results
    return results
```