Skip to content

Commit d8df7d8

Browse files
committed
reworking recommended videos...again?
1 parent 29bdb61 commit d8df7d8

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

app/api/v1/endpoints/video_catalog.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,6 @@ async def get_related_videos_for_video(
363363
] = 5,
364364
):
365365
"""Return a list of videos related to the given video.
366-
367-
The underlying implementation is currently stubbed out and will return the
368-
latest videos (excluding the source video) with a random relevance score.
369366
"""
370367

371368
related_items = await recommendation_service.get_related_videos(

app/services/recommendation_service.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
async def get_related_videos(
1717
video_id: VideoID, limit: int = 10
1818
) -> List[RecommendationItem]:
19-
"""Return a stubbed *related videos* list.
20-
21-
In a future iteration this will call into a real recommendation engine that
22-
analyses the content of the referenced video to find similar items. For the
23-
moment we simply return the latest videos (excluding the reference video)
24-
and assign each a random relevance score.
25-
"""
2619

2720
from opentelemetry import trace
2821
import time
@@ -42,18 +35,28 @@ async def get_related_videos(
4235
if target_video is None:
4336
return []
4437

45-
latest_summaries, _total = await video_service.list_latest_videos(
46-
page=1, page_size=limit + 5
38+
latest_summaries, _total = await video_service.get_recommended_videos(
39+
query_vector=target_video.content_features, page=1, page_size=limit + 5
4740
)
4841

4942
related_items: List[RecommendationItem] = []
43+
unique_video_names: List[str] = []
5044

5145
for summary in latest_summaries:
5246
if summary.videoId == video_id:
5347
# Skip the source video itself
5448
continue
49+
50+
if summary.title in unique_video_names:
51+
# Skip if we've already added this video to the list
52+
# ...sometimes we get duplicate rows
53+
continue
54+
5555
if len(related_items) >= limit:
5656
break
57+
58+
unique_video_names.append(summary.title)
59+
5760
related_items.append(
5861
RecommendationItem(
5962
videoId=summary.videoId,

app/services/video_service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from bdb import effective
1011
import re
1112
from datetime import datetime, timezone, timedelta
1213
import asyncio
@@ -556,13 +557,33 @@ async def list_latest_videos(
556557
source_table_name=VIDEOS_TABLE_NAME,
557558
)
558559

560+
async def get_recommended_videos(
561+
query_vector: Optional[List[float]],
562+
page: int,
563+
page_size: int,
564+
db_table: Optional[AstraDBCollection] = None,
565+
) -> Tuple[List[VideoSummary], int]:
566+
567+
if query_vector is None:
568+
return [], 0
569+
570+
return await list_videos_with_query(
571+
{},
572+
page,
573+
page_size=page_size,
574+
sort_options={"content_features": query_vector},
575+
db_table=db_table,
576+
source_table_name=VIDEOS_TABLE_NAME
577+
)
578+
559579

560580
async def list_videos_by_tag(
561581
tag: str,
562582
page: int,
563583
page_size: int,
564584
db_table: Optional[AstraDBCollection] = None,
565585
) -> Tuple[List[VideoSummary], int]:
586+
566587
query_filter = {
567588
"tags": {"$in": [tag]},
568589
}

0 commit comments

Comments
 (0)