diff --git a/core/concepts/views.py b/core/concepts/views.py index c544d574..653402ed 100644 --- a/core/concepts/views.py +++ b/core/concepts/views.py @@ -1006,6 +1006,7 @@ def post(self, request, **kwargs): # pylint: disable=unused-argument name_key = self.request.data.get('name_key', None) or 'display_name' text = self.request.data.get('q', None) score_key = self.request.data.get('score_key', None) + encoder_model = self.request.data.get('encoder_model', None) if not isinstance(rows, list) or not rows: return Response( @@ -1017,7 +1018,9 @@ def post(self, request, **kwargs): # pylint: disable=unused-argument {'detail': 'Missing "q" in request body.'}, status=status.HTTP_400_BAD_REQUEST ) - - return Response( - Reranker().rerank(hits=rows, name_key=name_key, txt=text, score_key=score_key, order_results=True) - ) + try: + reranker = Reranker(model_name=encoder_model) + results = reranker.rerank(hits=rows, name_key=name_key, txt=text, score_key=score_key, order_results=True) + return Response(results) + except Exception as e: + return Response({'detail': str(e)}, status=status.HTTP_400_BAD_REQUEST) diff --git a/core/map_projects/migrations/0031_mapproject_encoder_model.py b/core/map_projects/migrations/0031_mapproject_encoder_model.py new file mode 100644 index 00000000..4c8fa45c --- /dev/null +++ b/core/map_projects/migrations/0031_mapproject_encoder_model.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.5 on 2026-04-06 05:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('map_projects', '0030_auto_20260221_0250'), + ] + + operations = [ + migrations.AddField( + model_name='mapproject', + name='encoder_model', + field=models.TextField(blank=True, default='BAAI/bge-reranker-v2-m3', null=True), + ), + ] diff --git a/core/map_projects/models.py b/core/map_projects/models.py index 2975b081..1098870f 100644 --- a/core/map_projects/models.py +++ b/core/map_projects/models.py @@ -1,6 +1,7 @@ import json from collections import Counter +from django.conf import settings from django.contrib.postgres.fields import ArrayField from django.core.exceptions import ValidationError from django.db import models, IntegrityError @@ -38,6 +39,7 @@ class MapProject(BaseModel): candidates = models.JSONField(default=dict, null=True, blank=True) lookup_config = models.JSONField(default=dict, null=True, blank=True) analysis = models.JSONField(default=dict, null=True, blank=True) + encoder_model = models.TextField(null=True, blank=True, default=settings.ENCODER_MODEL_NAME) class Meta: db_table = 'map_projects' @@ -195,6 +197,8 @@ def clean(self): self.clean_filters() if not self.include_retired: self.include_retired = False + if not self.encoder_model: + self.encoder_model = settings.ENCODER_MODEL_NAME if self.matches: try: self.matches = json.loads(self.matches) diff --git a/core/map_projects/serializers.py b/core/map_projects/serializers.py index 15527343..ee85669a 100644 --- a/core/map_projects/serializers.py +++ b/core/map_projects/serializers.py @@ -22,7 +22,7 @@ class Meta: 'created_by', 'updated_by', 'created_at', 'updated_at', 'url', 'is_active', 'public_access', 'file', 'user_id', 'organization_id', 'description', 'target_repo_url', 'include_retired', 'score_configuration', - 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis' + 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis', 'encoder_model' ] def prepare_object(self, validated_data, instance=None, file=None): @@ -36,7 +36,8 @@ def prepare_object(self, validated_data, instance=None, file=None): instance.columns = columns for attr in [ 'name', 'description', 'extras', 'target_repo_url', 'include_retired', - 'score_configuration', 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis' + 'score_configuration', 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis', + 'encoder_model' ]: setattr(instance, attr, validated_data.get(attr, get(instance, attr))) if not instance.id: @@ -109,7 +110,8 @@ class Meta: 'created_by', 'updated_by', 'created_at', 'updated_at', 'url', 'is_active', 'owner', 'owner_type', 'owner_url', 'public_access', 'target_repo_url', 'summary', 'logs', 'include_retired', - 'score_configuration', 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis' + 'score_configuration', 'filters', 'candidates', 'algorithms', 'lookup_config', 'analysis', + 'encoder_model' ] def __init__(self, *args, **kwargs):