-
Notifications
You must be signed in to change notification settings - Fork 0
story/HOP-8 #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Girik1105
wants to merge
22
commits into
develop
Choose a base branch
from
story/HOP-8
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−10
Open
story/HOP-8 #9
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
69108f9
[HOP-7] Added django-allauth, configured settings, added views and urls
Girik1105 ed39b3c
[HOP-7] Templates
Girik1105 66ac0c7
[HOP-7] Resolved merge conflicts
Girik1105 9f8a3ef
[HOP-7] Final templates, better comments in settings, uv lock
Girik1105 08ca48c
[HOP-8] Added models, views for QA storage and added to admin
Girik1105 e19ba98
[HOP-7] Resolved merge conflicts
Girik1105 5b6217f
[HOP-7] comments
Girik1105 dc5268c
Merge branch 'story/HOP-7' into story/HOP-8
Girik1105 878af42
[HOP-8] Removed debug print
Girik1105 0cea583
[HOP-7] Merge conflicts resolved
Girik1105 a808edf
[HOP-7] Added login view to /
Girik1105 be00571
[HOP-8] Resolved merge conflicts with HOP-7]
Girik1105 77534de
[HOP-8] missing try except block, added record before sending to llm …
Girik1105 8da315e
Merge branch 'develop' into story/HOP-8
Girik1105 215f66a
[HOP-8] Added toggle sidebar for nice ui, mock and real response is n…
Girik1105 821ba86
Revert "[HOP-8] Added toggle sidebar for nice ui, mock and real respo…
Girik1105 5e88a84
[HOP-8] Mock and real LLM use same response format
Girik1105 77688eb
[HOP-8] Logger added, exception would log full stack trace
Girik1105 4daf314
Merge branch 'develop' into story/HOP-8
Girik1105 a587991
Merge branch 'develop' into story/HOP-8
Girik1105 0dafc69
[HOP-11] Added loggers
Girik1105 5794f6e
[HOP-8] Added loggers (previous commit had wrong ticket number
Girik1105 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| from django.contrib import admin | ||
| from ask.models import QARecord | ||
|
|
||
| # Register your models here. | ||
|
|
||
| @admin.register(QARecord) | ||
| class QARecordAdmin(admin.ModelAdmin): | ||
| list_display = ["id", "user", "truncated_question", "question_timestamp", "answer_timestamp"] | ||
| list_filter = ["question_timestamp", "user"] | ||
| search_fields = ["question_text", "answer_text", "user__username"] | ||
| readonly_fields = ["question_timestamp", "answer_timestamp", "answer_raw_response"] | ||
| raw_id_fields = ["user"] | ||
| date_hierarchy = "question_timestamp" | ||
|
|
||
| def truncated_question(self, obj): | ||
| return obj.question_text[:75] + "..." if len(obj.question_text) > 75 else obj.question_text | ||
| truncated_question.short_description = "Question" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Generated by Django 6.0.1 on 2026-01-30 19:36 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='QARecord', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('question_text', models.TextField()), | ||
| ('question_timestamp', models.DateTimeField(auto_now_add=True)), | ||
| ('answer_text', models.TextField(blank=True, default='')), | ||
| ('answer_raw_response', models.JSONField(default=dict)), | ||
| ('answer_timestamp', models.DateTimeField(blank=True, null=True)), | ||
| ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='qa_records', to=settings.AUTH_USER_MODEL)), | ||
| ], | ||
| options={ | ||
| 'verbose_name': 'Q&A Record', | ||
| 'verbose_name_plural': 'Q&A Records', | ||
| 'ordering': ['-question_timestamp'], | ||
| 'indexes': [models.Index(fields=['user', '-question_timestamp'], name='ask_qarecor_user_id_f4353f_idx')], | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 6.0.1 on 2026-02-04 23:32 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('ask', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='qarecord', | ||
| name='is_error', | ||
| field=models.BooleanField(default=False), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,32 @@ | ||
| from django.db import models | ||
| from django.conf import settings | ||
|
|
||
| # Create your models here. | ||
|
|
||
| class QARecord(models.Model): | ||
| """ | ||
| Stores a question-answer pair from user interactions with the LLM. | ||
| """ | ||
| # Question fields | ||
| question_text = models.TextField() | ||
| question_timestamp = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| # Answer fields | ||
| answer_text = models.TextField(blank=True, default="") | ||
| answer_raw_response = models.JSONField(default=dict) | ||
| answer_timestamp = models.DateTimeField(null=True, blank=True) | ||
| is_error = models.BooleanField(default=False) | ||
|
|
||
| user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="qa_records") | ||
|
|
||
| class Meta: | ||
| ordering = ["-question_timestamp"] | ||
| verbose_name = "Q&A Record" | ||
| verbose_name_plural = "Q&A Records" | ||
| indexes = [ | ||
| models.Index(fields=["user", "-question_timestamp"]), | ||
| ] | ||
|
|
||
| def __str__(self): | ||
| truncated = self.question_text[:50] | ||
| suffix = "..." if len(self.question_text) > 50 else "" | ||
| return f"{self.user.username}: {truncated}{suffix}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| import logging | ||
| from django.shortcuts import render | ||
| from django.http import JsonResponse | ||
| from django.conf import settings | ||
| from django.contrib.auth.decorators import login_required | ||
| from django.utils import timezone | ||
| import ask.llm_connector | ||
| from ask.models import QARecord | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| @login_required | ||
| def index(request): | ||
|
|
@@ -12,17 +16,46 @@ 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</think>\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) | ||
| logger.exception("Failed to connect to server") | ||
| error_msg = f"Failed to connect to server: {e}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the exceptions need to be logged
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above |
||
|
|
||
| # 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the exception stack trace needs to be logged