Enhance LLM question import process with preview and confirmation steps, including error handling and M2M record creation

This commit is contained in:
Ross
2025-10-20 11:01:47 +01:00
parent 06b7239daa
commit 01f67f3e32
6 changed files with 211 additions and 4 deletions
@@ -2,10 +2,14 @@
{% block content %}
<h1>Import LLM Questions</h1>
<form method="post" enctype="multipart/form-data">
<form id="llm-import-form" method="post" enctype="multipart/form-data" hx-post="{% url 'sbas:import_llm_questions' %}" hx-target="#import-preview" hx-swap="innerHTML">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-primary" type="submit">Upload and Import</button>
<div class="d-flex gap-2">
<button class="btn btn-outline-primary" type="submit">Preview (dry run)</button>
<button class="btn btn-secondary" type="button" onclick="document.getElementById('llm-import-form').reset()">Reset</button>
</div>
</form>
<p>Upload a single JSON array, a single object, or JSONL (one JSON object per line) matching the agreed schema.</p>
<p>Upload a single JSON array, a single object, or JSONL (one JSON object per line) matching the agreed schema. Use the Preview button to inspect results before importing.</p>
<div id="import-preview" class="mt-3"></div>
{% endblock %}
@@ -0,0 +1,8 @@
<div class="alert alert-danger">
<h5>Import error</h5>
<ul>
{% for e in errors %}
<li>{{ e }}</li>
{% endfor %}
</ul>
</div>
@@ -0,0 +1,42 @@
{% for item in items %}
<div class="card mb-2">
<div class="card-body">
<h5 class="card-title">Question {{ item.index }}</h5>
{% with payload=payloads|slice:item.index|first %}
{% if payload %}
<p><strong>Stem:</strong> {{ payload.stem|default:''|safe }}</p>
<p><strong>Answers:</strong>
<ul>
<li>A: {{ payload.a_answer|default:''|safe }}</li>
<li>B: {{ payload.b_answer|default:''|safe }}</li>
<li>C: {{ payload.c_answer|default:''|safe }}</li>
<li>D: {{ payload.d_answer|default:''|safe }}</li>
<li>E: {{ payload.e_answer|default:''|safe }}</li>
</ul>
</p>
{% endif %}
{% endwith %}
<p><strong>Missing M2M:</strong>
{% if item.missing_m2m %}
<ul>
{% for m in item.missing_m2m %}
<li>{{ m.model }}: {{ m.values|join:", " }}</li>
{% endfor %}
</ul>
{% else %}
None
{% endif %}
</p>
<div>
<label><input type="checkbox" name="selected" value="{{ item.index }}" checked> Include</label>
</div>
</div>
</div>
{% endfor %}
<div class="d-flex gap-2">
<button class="btn btn-success" hx-post="{% url 'sbas:import_llm_confirm' %}" hx-target="#import-result" hx-include="closest form">Import selected</button>
<button class="btn btn-secondary" hx-get="{% url 'sbas:import_llm_questions' %}" hx-target="#import-result">Cancel</button>
</div>
<div id="import-result"></div>
@@ -0,0 +1,21 @@
<div class="alert alert-info">Imported {{ created }} questions.</div>
{% if errors %}
<div class="alert alert-danger">
<h5>Errors</h5>
<ul>
{% for e in errors %}
<li>Item {{ e.index }}: {{ e.errors|join:', ' }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% if actually_created %}
<div class="alert alert-secondary">
<h5>Created M2M records</h5>
<ul>
{% for k,v in actually_created.items %}
<li>{{ k }}: {{ v|join:', ' }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
+5
View File
@@ -94,6 +94,11 @@ urlpatterns = [
views.import_llm_questions,
name="import_llm_questions",
),
path(
"import_llm_confirm/",
views.import_llm_confirm,
name="import_llm_confirm",
),
#path(
# "exam/<int:pk>/scores/<int:cid>/<str:passcode>/",
# views.exam_scores_cid_user,
+128 -1
View File
@@ -35,6 +35,7 @@ import re
import logging
logger = logging.getLogger(__name__)
import uuid
from loguru import logger
@@ -761,4 +762,130 @@ def import_llm_questions(request):
for k, v in aggregated_actually_created.items():
report["actually_created_m2m"][k] = v
return JsonResponse({"ok": True, "report": report})
# If this was a dry-run and the request came from HTMX, render a preview partial and store the parsed candidates in session
if dry_run and request.headers.get("HX-Request"):
session_key = f"llm_import_{uuid.uuid4().hex}"
# store minimal necessary data in session (the raw payloads)
request.session[session_key] = candidates
request.session.modified = True
preview_ctx = {
"report": report,
"items": report["per_item"],
"payloads": candidates,
"session_key": session_key,
"allow_create_m2m": allow_create_m2m,
}
return render(request, "sbas/partials/import_preview.html", preview_ctx)
# Non-HTMX callers get JSON
return JsonResponse({"ok": True, "report": report})
@login_required
@user_passes_test(lambda u: u.is_superuser)
@require_http_methods(["POST"])
def import_llm_confirm(request):
"""Confirm import of previously-previewed candidates stored in session.
Expects POST with 'session_key' and 'selected' (comma-separated indices) and optional allow_create_m2m.
Returns an HTML fragment suitable for HTMX replacement summarising the import results.
"""
session_key = request.POST.get("session_key")
# support both a comma-separated 'selected' string or multiple 'selected' values
selected_raw = request.POST.get("selected")
selected_list = request.POST.getlist("selected")
allow_create_m2m = request.POST.get("allow_create_m2m") in ("on", "true", "True", "1")
if selected_list:
# selected_list contains strings of indices
selected = ",".join(selected_list)
else:
selected = selected_raw
if not session_key or session_key not in request.session:
return render(request, "sbas/partials/import_error.html", {"errors": ["Session expired or invalid; please re-run preview."]}, status=400)
candidates = request.session.get(session_key, [])
indices = []
if selected:
for part in selected.split(','):
try:
indices.append(int(part))
except Exception:
continue
# default: import all if none specified
if not indices:
indices = list(range(len(candidates)))
# perform actual import for selected indices
from atlas.models import Finding, Structure, Condition, Presentation, Subspecialty
created = 0
errors = []
actually_created = defaultdict(list)
for idx in indices:
try:
payload = candidates[idx]
except Exception:
errors.append({"index": idx, "errors": ["Missing candidate"]})
continue
try:
with transaction.atomic():
q = Question()
q.stem = payload.get("stem", "").strip()
title = payload.get("title")
if title:
q.stem = f"<strong>{title}</strong>\n" + q.stem
q.a_answer = payload.get("a_answer", "").strip()
q.a_feedback = payload.get("a_feedback", "")
q.b_answer = payload.get("b_answer", "").strip()
q.b_feedback = payload.get("b_feedback", "")
q.c_answer = payload.get("c_answer", "").strip()
q.c_feedback = payload.get("c_feedback", "")
q.d_answer = payload.get("d_answer", "").strip()
q.d_feedback = payload.get("d_feedback", "")
q.e_answer = payload.get("e_answer", "").strip()
q.e_feedback = payload.get("e_feedback", "")
q.feedback = payload.get("feedback", "")
q.best_answer = payload.get("best_answer")
cat_val = payload.get("category")
if isinstance(cat_val, str):
cat_obj, _ = Category.objects.get_or_create(category=cat_val)
q.category = cat_obj
q.save()
for key, model_cls in (("finding", Finding), ("structure", Structure), ("condition", Condition), ("presentation", Presentation), ("subspecialty", Subspecialty)):
vals = payload.get(key) or []
final_objs = []
for v in vals:
if isinstance(v, int):
try:
final_objs.append(model_cls.objects.get(pk=v))
except model_cls.DoesNotExist:
continue
elif isinstance(v, str):
qs = model_cls.objects.filter(name__iexact=v)
if not qs.exists():
qs = model_cls.objects.filter(name__icontains=v)
if qs.exists():
final_objs.extend(list(qs[:5]))
else:
if allow_create_m2m:
obj, created_flag = model_cls.objects.get_or_create(name=v)
final_objs.append(obj)
actually_created[model_cls.__name__].append(obj.pk)
if final_objs:
getattr(q, key).add(*[o.pk for o in final_objs])
created += 1
except Exception as e:
errors.append({"index": idx, "errors": [str(e)]})
# cleanup session
try:
del request.session[session_key]
request.session.modified = True
except Exception:
pass
return render(request, "sbas/partials/import_result.html", {"created": created, "errors": errors, "actually_created": dict(actually_created)})