+ Are you sure you want to delete all your question history? This action is irreversible.
+
+
+
+
+
diff --git a/hospexplorer/ask/urls.py b/hospexplorer/ask/urls.py
index 2a75452..aa9dcf1 100644
--- a/hospexplorer/ask/urls.py
+++ b/hospexplorer/ask/urls.py
@@ -7,4 +7,5 @@
path("", views.index, name="index"),
path("mock", views.mock_response, name="mock-response"),
path("query", views.query, name="query-llm"),
+ path("delete-history", views.delete_history, name="delete-history"),
]
\ No newline at end of file
diff --git a/hospexplorer/ask/views.py b/hospexplorer/ask/views.py
index b948680..dc935ae 100644
--- a/hospexplorer/ask/views.py
+++ b/hospexplorer/ask/views.py
@@ -1,9 +1,15 @@
-from django.shortcuts import render
+import logging
+from django.shortcuts import render, redirect
+from django.views.decorators.http import require_POST
from django.http import JsonResponse
from django.conf import settings
from django.contrib.auth.decorators import login_required
+from django.utils import timezone
+from django.contrib import messages
import ask.llm_connector
+from ask.models import QARecord
+logger = logging.getLogger(__name__)
@login_required
def index(request):
@@ -12,17 +18,53 @@ def index(request):
@login_required
def mock_response(request):
+ """Returns a mock LLM response in the same format as the real server."""
return JsonResponse({
- "message": "Okay, the user wants a three-sentence bedtime story about a unicorn. Let's start by thinking about the key elements of a good bedtime story. They usually have a peaceful setting, a gentle conflict or quest, and a happy ending.\n\nFirst sentence needs to set the scene. Maybe a magical forest with a unicorn. Luna is a common unicorn name, sounds soft. Moonlight and stars could add a calming effect.\n\nSecond sentence should introduce a small problem or something the unicorn does. Healing powers are typical for unicorns. Maybe she finds an injured animal, like a fox. Using her horn to heal adds magic.\n\nThird sentence wraps it up with a happy ending. The fox recovers, they become friends, and the forest is peaceful. Emphasize safety and dreams to make it soothing for bedtime.\n\nCheck if it's exactly three sentences. Yes. Language is simple and comforting, suitable for a child. Avoid any scary elements. Make sure it flows smoothly and conveys warmth.\n\n\nUnder the shimmering moonlit sky, a silver-maned unicorn named Luna trotted through the enchanted forest, her hooves leaving trails of stardust. When she discovered a wounded fox whimpering beneath an ancient oak, she touched her glowing horn to its paw, weaving magic that healed the hurt. With the fox curled beside her, Luna rested on a bed of moss, her heart full as the forest whispered lullabies, ensuring all creatures drifted into dreams of peace."
+ "choices": [{
+ "message": {
+ "content": "Under the shimmering moonlit sky, a silver-maned unicorn named Luna trotted through the enchanted forest, her hooves leaving trails of stardust. When she discovered a wounded fox whimpering beneath an ancient oak, she touched her glowing horn to its paw, weaving magic that healed the hurt. With the fox curled beside her, Luna rested on a bed of moss, her heart full as the forest whispered lullabies, ensuring all creatures drifted into dreams of peace."
+ }
+ }]
})
@login_required
def query(request):
+ query_text = request.GET.get("query", "")
+ record = QARecord.objects.create(
+ question_text=query_text,
+ user=request.user,
+ )
try:
- llm_response = ask.llm_connector.query_llm(request.GET["query"])
- content = llm_response["choices"][0]["message"]["content"]
- return JsonResponse({"message": content})
- except (KeyError, IndexError, TypeError) as e:
- return JsonResponse({"error": f"Unexpected response from server: {e}"}, status=500)
+ llm_response = ask.llm_connector.query_llm(query_text)
+
+ # Mock and real LLM use the same response format
+ if "choices" not in llm_response or not llm_response["choices"]:
+ raise ValueError("LLM response is missing structure")
+ answer_text = llm_response["choices"][0].get("message", {}).get("content", "")
+
+ record.answer_text = answer_text
+ record.answer_raw_response = llm_response
+ record.answer_timestamp = timezone.now()
+ record.save()
+
+ return JsonResponse({"message": answer_text})
+ except (KeyError, IndexError, TypeError, ValueError) as e:
+ logger.exception("Unexpected response from server")
+ error_msg = f"Unexpected response from server: {e}"
except Exception as e:
- return JsonResponse({"error": f"Failed to connect to server: {e}"}, status=500)
\ No newline at end of file
+ logger.exception("Failed to connect to server")
+ error_msg = f"Failed to connect to server: {e}"
+
+ # The try block returns on success, so this only runs on error.
+ record.is_error = True
+ record.answer_text = error_msg
+ record.answer_timestamp = timezone.now()
+ record.save()
+ return JsonResponse({"error": error_msg}, status=500)
+
+@login_required
+@require_POST
+def delete_history(request):
+ request.user.qa_records.all().delete()
+ messages.success(request, "Question history deleted successfully!")
+ return redirect("ask:index")
diff --git a/mockoon/llm-mock.json b/mockoon/llm-mock.json
index f52caae..b31015a 100644
--- a/mockoon/llm-mock.json
+++ b/mockoon/llm-mock.json
@@ -17,7 +17,7 @@
"responses": [
{
"uuid": "chat-completions-response",
- "body": "{\n \"message\": \"This is a mock response from the local mockserver. Your query was received successfully.\"\n}",
+ "body": "{\n \"choices\": [{\n \"message\": {\n \"content\": \"Under the shimmering moonlit sky, a silver-maned unicorn named Luna trotted through the enchanted forest, her hooves leaving trails of stardust. When she discovered a wounded fox whimpering beneath an ancient oak, she touched her glowing horn to its paw, weaving magic that healed the hurt. With the fox curled beside her, Luna rested on a bed of moss, her heart full as the forest whispered lullabies, ensuring all creatures drifted into dreams of peace.\"\n }\n }]\n}",
"latency": 0,
"statusCode": 200,
"label": "Success response",