Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recently liked algo #86

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ jobs:
# - name: Lint with mypy
# run: |
# mypy src tests
# - name: Run tests
# run: |
# pytest
- name: Run tests
run: |
pytest
49 changes: 49 additions & 0 deletions src/recommendations/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,54 @@ async def get_fast_dopamine(
return res


async def get_recently_liked(
user_id: int,
limit: int = 10,
exclude_meme_ids: list[int] = [],
):
query = f"""
WITH EVENTS AS (
SELECT *
FROM user_meme_reaction UMR
WHERE reaction_id = 1
ORDER BY sent_at DESC
LIMIT 10000
)
, CANDIDATES AS (
SELECT meme_id AS id
FROM EVENTS
GROUP BY meme_id
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
)

SELECT
M.id
, M.type, M.telegram_file_id, M.caption
, 'recently_liked' AS recommended_by

FROM CANDIDATES C
INNER JOIN meme M
ON M.id = C.id

INNER JOIN user_language L
ON L.language_code = M.language_code
AND L.user_id = {user_id}

LEFT JOIN user_meme_reaction R
ON R.meme_id = M.id
AND R.user_id = {user_id}

WHERE 1=1
AND M.status = 'ok'
AND R.meme_id IS NULL
{exclude_meme_ids_sql_filter(exclude_meme_ids)}
LIMIT {limit}
"""
res = await fetch_all(text(query))
return res


class CandidatesRetriever:
"""CandidatesRetriever class is used for unit testing"""

Expand All @@ -656,6 +704,7 @@ class CandidatesRetriever:
"selected_sources": get_selected_sources,
"best_memes_from_each_source": get_best_memes_from_each_source,
"like_spread_and_recent_memes": like_spread_and_recent_memes,
"recently_liked": get_recently_liked,
}

async def get_candidates(
Expand Down
4 changes: 3 additions & 1 deletion src/recommendations/meme_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,16 @@ async def get_candidates(user_id, limit):
"uploaded_memes": 0.2,
"fast_dopamine": 0.2,
"best_memes_from_each_source": 0.2,
"lr_smoothed": 0.4,
"lr_smoothed": 0.2,
"recently_liked": 0.2,
}

engines = [
"uploaded_memes",
"fast_dopamine",
"best_memes_from_each_source",
"lr_smoothed",
"recently_liked",
]
candidates_dict = await retriever.get_candidates_dict(
engines, user_id, limit, exclude_mem_ids=meme_ids_in_queue
Expand Down
Loading