Skip to content

Commit 94957ad

Browse files
shanbadyCopilot
andauthored
Improvements and fixes for issues in the vector search app (#3505)
* making resource type a distinct list * add test * move tune collections out of hot path * specify prefetch limit to outer prefetch * adjust gauss decay param for custom score boost * moving get collection outside of loop section * fix indexed param * check for collection before tuning Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * moving get collection outside of loop section --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent d22f862 commit 94957ad

6 files changed

Lines changed: 47 additions & 6 deletions

File tree

main/settings_celery.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@
186186
"task": "vector_search.tasks.sync_topics",
187187
"schedule": crontab(minute=0, hour="6,18,23"), # 2am 2pm and 7pm EST
188188
},
189+
"daily_tune_qdrant_collections": {
190+
"task": "vector_search.tasks.tune_qdrant_collections",
191+
"schedule": crontab(minute=0, hour=10), # once per day
192+
},
189193
"weekly_check_missing_embeddings": {
190194
"task": "vector_search.tasks.embeddings_healthcheck",
191195
"schedule": crontab(

vector_search/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"course_feature": models.PayloadSchemaType.KEYWORD,
6969
"topics[].name": models.PayloadSchemaType.KEYWORD,
7070
"ocw_topics": models.PayloadSchemaType.KEYWORD,
71-
"runs[].level.code": models.PayloadSchemaType.KEYWORD,
71+
"runs[].level[].code": models.PayloadSchemaType.KEYWORD,
7272
"departments[].department_id": models.PayloadSchemaType.KEYWORD,
7373
"platform.code": models.PayloadSchemaType.KEYWORD,
7474
"offered_by.code": models.PayloadSchemaType.KEYWORD,

vector_search/tasks.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,22 @@
4747
vector_point_id,
4848
vector_point_key,
4949
)
50+
from vector_search.utils import (
51+
tune_qdrant_collections as tune_qdrant_collections_util,
52+
)
5053

5154
log = logging.getLogger(__name__)
5255

5356

57+
@app.task
58+
def tune_qdrant_collections():
59+
"""
60+
Tune optimizer settings for Qdrant collections.
61+
"""
62+
log.info("Running Qdrant collection tuning task")
63+
tune_qdrant_collections_util()
64+
65+
5466
def _replace_with_chain(task, task_signatures):
5567
"""
5668
Replace a task with a chain only when there is work to do.
@@ -347,7 +359,9 @@ def embed_new_learning_resources(self):
347359
).exclude(resource_type=CONTENT_FILE_TYPE)
348360

349361
resource_types = list(
350-
new_learning_resources.values_list("resource_type", flat=True)
362+
new_learning_resources.order_by("resource_type")
363+
.values_list("resource_type", flat=True)
364+
.distinct()
351365
)
352366
tasks = []
353367
for resource_type in resource_types:

vector_search/tasks_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def test_embed_new_learning_resources(mocker, mocked_celery):
168168
embed_new_learning_resources.delay()
169169
list(mocked_celery.group.call_args[0][0])
170170

171+
assert generate_embeddings_mock.si.call_count == 1
171172
embedded_ids = generate_embeddings_mock.si.mock_calls[0].args[0]
172173
assert sorted(new_resource_ids) == sorted(embedded_ids)
173174

vector_search/utils.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ def create_qdrant_collections(force_recreate):
204204
update_qdrant_indexes()
205205

206206

207+
def tune_qdrant_collections():
208+
"""Tune optimizer settings for Qdrant collections."""
209+
if not all([settings.QDRANT_HOST, settings.QDRANT_BASE_COLLECTION_NAME]):
210+
logger.warning(
211+
"Skipping Qdrant collection tuning: "
212+
"QDRANT_HOST and QDRANT_BASE_COLLECTION_NAME must be set"
213+
)
214+
return
215+
216+
client = qdrant_client()
217+
collections = [
218+
RESOURCES_COLLECTION_NAME,
219+
CONTENT_FILES_COLLECTION_NAME,
220+
TOPICS_COLLECTION_NAME,
221+
]
222+
for collection_name in collections:
223+
if not client.collection_exists(collection_name=collection_name):
224+
continue
225+
tune_collection(client, collection_name)
226+
227+
207228
def create_qdrant_collection(collection_name, force_recreate):
208229
"""
209230
Create or recreate a QDrant collection
@@ -244,7 +265,6 @@ def create_qdrant_collection(collection_name, force_recreate):
244265
),
245266
hnsw_config=models.HnswConfigDiff(on_disk=False),
246267
)
247-
tune_collection(client, collection_name)
248268

249269

250270
def update_qdrant_indexes():
@@ -260,8 +280,8 @@ def update_qdrant_indexes():
260280
]:
261281
indexes = index[0]
262282
collection_name = index[1]
283+
collection = client.get_collection(collection_name=collection_name)
263284
for index_field in indexes:
264-
collection = client.get_collection(collection_name=collection_name)
265285
if (
266286
index_field not in collection.payload_schema
267287
or indexes[index_field]
@@ -1317,9 +1337,9 @@ def custom_score_formula(collection_name: str) -> list[models.MultExpression]:
13171337
models.GaussDecayExpression(
13181338
gauss_decay=models.DecayParamsExpression(
13191339
x="$score", # decay over the relevance score itself
1320-
target=1.0, # cosine "perfect match" — full boost
1340+
target=0.4, # full boost at this target
13211341
scale=0.2,
1322-
midpoint=0.5,
1342+
midpoint=0.2,
13231343
)
13241344
),
13251345
]

vector_search/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ async def _build_search_params( # noqa: PLR0913
187187
prefetch_params = [
188188
models.Prefetch(
189189
query=custom_formula_query,
190+
limit=prefetch_limit,
190191
prefetch=[
191192
models.Prefetch(
192193
filter=search_filter,
@@ -198,6 +199,7 @@ async def _build_search_params( # noqa: PLR0913
198199
),
199200
models.Prefetch(
200201
query=custom_formula_query,
202+
limit=prefetch_limit,
201203
prefetch=[
202204
models.Prefetch(
203205
filter=search_filter,

0 commit comments

Comments
 (0)