diff --git a/blueprints/conversation/agents.py b/blueprints/conversation/agents.py index 9c0fb08..6d57c7e 100644 --- a/blueprints/conversation/agents.py +++ b/blueprints/conversation/agents.py @@ -1369,13 +1369,29 @@ async def beli_search_restaurants( if name: qs = qs.filter(name__icontains=name) - restaurants = ( - await qs.order_by("friend_beli_user_id", "rank_index", "name") - .limit(15) - .all() - ) + if friends_only: + # Fetch broadly ordered by rank, then sample up to 3 top picks per friend + rows = await qs.order_by("rank_index", "name").limit(500).all() + if cuisine: + cuisine_lower = cuisine.lower() + rows = [ + r + for r in rows + if any(cuisine_lower in c.lower() for c in (r.cuisine or [])) + ] + seen: dict[str, int] = {} + restaurants = [] + for r in rows: + fid = r.friend_beli_user_id + if seen.get(fid, 0) < 3: + restaurants.append(r) + seen[fid] = seen.get(fid, 0) + 1 + if len(restaurants) >= 15: + break + else: + restaurants = await qs.order_by("rank_index", "name").limit(15).all() - if cuisine: + if cuisine and not friends_only: cuisine_lower = cuisine.lower() restaurants = [ r