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

blender rollout; changed configs a bit #88

Merged
merged 4 commits into from
Oct 13, 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
70 changes: 5 additions & 65 deletions src/recommendations/meme_queue.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import random
from typing import Any, Optional

from src import redis
from src.recommendations.blender import blend
from src.recommendations.candidates import (
CandidatesRetriever,
classic,
get_best_memes_from_each_source,
get_fast_dopamine,
get_lr_smoothed,
get_most_shared_memes,
get_selected_sources,
less_seen_meme_and_source,
like_spread_and_recent_memes,
uploaded_memes,
)
from src.storage.schemas import MemeData
from src.tgbot.user_info import get_user_info
Expand Down Expand Up @@ -81,53 +75,7 @@ async def generate_cold_start_recommendations(user_id, limit=10):
await redis.add_memes_to_queue_by_key(queue_key, candidates)


async def generate_recommendations(user_id: int, limit: int):
# if (user_id + 25) % 100 < 50:
# await generate_with_blender(user_id, limit)
# return

queue_key = redis.get_meme_queue_key(user_id)
memes_in_queue = await redis.get_all_memes_in_queue_by_key(queue_key)
meme_ids_in_queue = [meme["id"] for meme in memes_in_queue]

candidates = []

r = random.random()

if r < 0.3:
candidates = await classic(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)
elif r < 0.6:
candidates = await uploaded_memes(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)
elif r < 0.9:
candidates = await like_spread_and_recent_memes(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)
else:
candidates = await get_most_shared_memes(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)

if len(candidates) == 0:
candidates = await get_lr_smoothed(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)

if len(candidates) == 0:
candidates = await less_seen_meme_and_source(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)

if len(candidates) == 0:
return

await redis.add_memes_to_queue_by_key(queue_key, candidates)


async def generate_with_blender(
async def generate_recommendations(
user_id: int,
limit: int,
nmemes_sent: Optional[int] = None,
Expand Down Expand Up @@ -183,18 +131,11 @@ async def get_candidates(user_id, limit):
"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
weights.keys(), user_id, limit, exclude_mem_ids=meme_ids_in_queue
)

fixed_pos = {0: "lr_smoothed", 1: "lr_smoothed"}
fixed_pos = {0: "lr_smoothed"}
return blend(candidates_dict, weights, fixed_pos, limit, random_seed)

# >=100
Expand All @@ -204,12 +145,11 @@ async def get_candidates(user_id, limit):
"lr_smoothed": 0.4,
}

engines = ["uploaded_memes", "like_spread_and_recent_memes", "lr_smoothed"]
candidates_dict = await retriever.get_candidates_dict(
engines, user_id, limit, exclude_mem_ids=meme_ids_in_queue
weights.keys(), user_id, limit, exclude_mem_ids=meme_ids_in_queue
)

fixed_pos = {0: "lr_smoothed", 1: "lr_smoothed"}
fixed_pos = {0: "lr_smoothed"}
candidates = blend(candidates_dict, weights, fixed_pos, limit, random_seed)

if len(candidates) == 0 and nmemes_sent > 1000:
Expand Down
2 changes: 1 addition & 1 deletion src/tgbot/senders/next_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def get_next_meme_for_user(
if meme and not await user_meme_reaction_exists(user_id, meme.id):
return meme
if not meme:
await meme_queue.generate_recommendations(user_id, limit=5)
await meme_queue.generate_recommendations(user_id, limit=7)

logging.warning(
f"Failed to find unseen meme for user {user_id} after {max_attempts} attempts"
Expand Down
73 changes: 34 additions & 39 deletions tests/recommendations/test_meme_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import pytest

from src.recommendations.candidates import CandidatesRetriever
from src.recommendations.meme_queue import generate_with_blender
from src.recommendations.meme_queue import generate_recommendations


@pytest.mark.asyncio
async def test_generate_with_blender_below_30():
async def test_generate_below_30():
async def get_fast_dopamine(
self,
user_id: int,
Expand Down Expand Up @@ -43,26 +43,26 @@ class TestRetriever(CandidatesRetriever):
"fast_dopamine": get_fast_dopamine,
"best_meme_from_each_source": get_best_memes_from_each_source,
}

candidates = await generate_with_blender(1, 10, 10, TestRetriever())
candidates = await generate_recommendations(1, 10, 10, TestRetriever())
assert len(candidates) == 2
assert candidates[0]["id"] == 1
assert candidates[1]["id"] == 2
assert candidates[0]['id'] in [1, 2]
assert candidates[1]['id'] in [1, 2]

class TestRetriever(CandidatesRetriever):
engine_map = {
"fast_dopamine": get_fast_dopamine_empty,
"best_memes_from_each_source": get_best_memes_from_each_source,
}

candidates = await generate_with_blender(1, 10, 10, TestRetriever())
candidates = await generate_recommendations(1, 10, 10, TestRetriever())
assert len(candidates) == 2
assert candidates[0]["id"] == 3
assert candidates[1]["id"] == 4
assert candidates[0]['id'] in [3, 4]
assert candidates[1]['id'] in [3, 4]


@pytest.mark.asyncio
async def test_generate_with_blender_below_100():
async def test_generate_below_100():
async def uploaded_memes(
self,
user_id: int,
Expand Down Expand Up @@ -105,6 +105,15 @@ async def get_lr_smoothed(
return [
{"id": 7},
{"id": 8},
]

async def get_recentrly_liked(
self,
user_id: int,
limit: int = 10,
exclude_meme_ids: list[int] = [],
) -> list[dict[str, Any]]:
return [
{"id": 9},
{"id": 10},
]
Expand All @@ -115,21 +124,15 @@ class TestRetriever(CandidatesRetriever):
"fast_dopamine": get_fast_dopamine,
"best_memes_from_each_source": get_best_memes_from_each_source,
"lr_smoothed": get_lr_smoothed,
"recently_liked": get_recentrly_liked,
}

candidates = await generate_with_blender(1, 10, 40, TestRetriever())
candidates = await generate_recommendations(1, 10, 40, TestRetriever())
assert len(candidates) == 10
# hardcoded values
assert candidates[0]["id"] == 7
assert candidates[1]["id"] == 8
assert candidates[2]["id"] == 9
assert candidates[3]["id"] == 1
assert candidates[4]["id"] == 3
assert candidates[5]["id"] == 4

assert candidates[0]['id'] in [7, 8, 9, 10]

@pytest.mark.asyncio
async def test_generate_with_blender_above_100():
async def test_generate_above_100():
async def uploaded_memes(
self,
user_id: int,
Expand Down Expand Up @@ -173,22 +176,14 @@ class TestRetriever(CandidatesRetriever):
"like_spread_and_recent_memes": like_spread_and_recent_memes,
"lr_smoothed": get_lr_smoothed,
}

candidates = await generate_with_blender(
1, 10, 200, TestRetriever(), random_seed=102
)

candidates = await generate_recommendations(1, 10, 200, TestRetriever(), random_seed=102)
assert len(candidates) == 10
# hardcoded values
assert candidates[0]["id"] == 7
assert candidates[1]["id"] == 8
assert candidates[2]["id"] == 1
assert candidates[3]["id"] == 9
assert candidates[4]["id"] == 2
assert candidates[5]["id"] == 10
assert candidates[0]['id'] in [7, 8, 9, 10]


@pytest.mark.asyncio
async def test_generate_with_blender_empty_above_100():
async def test_generate_empty_above_100():
async def uploaded_memes(
self,
user_id: int,
Expand Down Expand Up @@ -243,11 +238,11 @@ class TestRetriever(CandidatesRetriever):
"less_seen_meme_and_source": top_memes_from_less_seen_sources,
"best_memes_from_each_source": get_best_memes_from_each_source,
}

candidates = await generate_with_blender(1, 10, 200, TestRetriever())
candidates = await generate_recommendations(1, 10, 200, TestRetriever())
assert len(candidates) == 2
assert candidates[0]["id"] == 3
assert candidates[0]['id'] in [3, 4]

candidates = await generate_with_blender(1, 10, 1200, TestRetriever())
candidates = await generate_recommendations(1, 10, 1200, TestRetriever())
assert len(candidates) == 2
assert candidates[0]["id"] == 1
assert candidates[0]['id'] in [1, 2]
Loading