Coverage for apps / ai / migrations / 0007_update_search_ranking_prompt.py: 55%
20 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 00:40 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 00:40 +0000
1"""Update search_ranking prompt to prioritize images."""
3from django.db import migrations
6def update_search_ranking_prompt(apps, schema_editor):
7 """Update the search_ranking prompt to heavily prioritize results with images."""
8 AIPrompt = apps.get_model('ai', 'AIPrompt')
10 try:
11 prompt = AIPrompt.objects.get(prompt_type='search_ranking')
12 prompt.system_prompt = '''You are a recipe search ranker.
14CRITICAL RULE: ALL results with [has image] MUST appear BEFORE any results without images. This is NON-NEGOTIABLE.
16Within each group (with images vs without images), rank by:
171. Relevance to the search query
182. Recipe completeness (ratings, reviews)
193. Source reliability
21Output format: JSON array of 0-based indices in ranked order.
22Example: [2, 0, 4, 1, 3]
24IMPORTANT: Output ONLY the JSON array. No explanation or text.'''
25 prompt.save()
26 except AIPrompt.DoesNotExist:
27 pass
30def reverse_update(apps, schema_editor):
31 """Revert to original prompt."""
32 AIPrompt = apps.get_model('ai', 'AIPrompt')
34 try:
35 prompt = AIPrompt.objects.get(prompt_type='search_ranking')
36 prompt.system_prompt = '''You are a recipe quality evaluator.
37Given a list of recipe search results, rank them by relevance and quality.
39Always respond with valid JSON as an array of indices (0-based) in ranked order:
40[2, 0, 4, 1, 3]
42Consider:
43- Relevance to the search query
44- Recipe completeness (has image, ratings, reviews)
45- Source reliability
46- Clarity of title and description
48IMPORTANT: Respond with ONLY the JSON, no additional text, explanation, or commentary.'''
49 prompt.save()
50 except AIPrompt.DoesNotExist:
51 pass
54class Migration(migrations.Migration):
56 dependencies = [
57 ('ai', '0006_add_ai_discovery_suggestion'),
58 ]
60 operations = [
61 migrations.RunPython(update_search_ranking_prompt, reverse_update),
62 ]