Teddy Xinyuan Chen
commited on
Commit
Β·
9fbb706
1
Parent(s):
99cd51c
2024-10-08T20-23-09Z
Browse files
src/quizzes/migrations/0005_llmgradedanswer_correct_answer.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Generated by Django 4.2.7 on 2024-10-09 00:19
|
| 2 |
+
|
| 3 |
+
from django.db import migrations, models
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Migration(migrations.Migration):
|
| 7 |
+
|
| 8 |
+
dependencies = [
|
| 9 |
+
("quizzes", "0004_remove_llmgradedanswer_rubrics_question_rubrics"),
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
operations = [
|
| 13 |
+
migrations.AddField(
|
| 14 |
+
model_name="llmgradedanswer",
|
| 15 |
+
name="correct_answer",
|
| 16 |
+
field=models.CharField(default="", max_length=200),
|
| 17 |
+
),
|
| 18 |
+
]
|
src/quizzes/models.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import typing
|
| 3 |
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from django.contrib.postgres import fields
|
| 7 |
from django.db import models
|
|
@@ -74,10 +76,9 @@ class LLMGradedAnswer(Answer):
|
|
| 74 |
from dotenv import load_dotenv
|
| 75 |
|
| 76 |
load_dotenv()
|
| 77 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 78 |
prompt = f"Grade the following answer based on the rubric:\nRubric: {self.question.rubrics}\nAnswer: {user_answer}"
|
| 79 |
|
| 80 |
-
response =
|
| 81 |
model="gpt-4o",
|
| 82 |
messages=[
|
| 83 |
{"role": "system", "content": "You are a helpful assistant."},
|
|
@@ -85,7 +86,7 @@ class LLMGradedAnswer(Answer):
|
|
| 85 |
],
|
| 86 |
)
|
| 87 |
|
| 88 |
-
return {"result": "success", "message": response.choices[0].message
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print(f"An error occurred: {e}")
|
|
@@ -98,3 +99,38 @@ class MultipleChoiceAnswer(Answer):
|
|
| 98 |
|
| 99 |
def __str__(self) -> str:
|
| 100 |
return f"{self.correct_answer} from {self.choices}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import typing
|
| 3 |
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
from django.contrib.postgres import fields
|
| 9 |
from django.db import models
|
|
|
|
| 76 |
from dotenv import load_dotenv
|
| 77 |
|
| 78 |
load_dotenv()
|
|
|
|
| 79 |
prompt = f"Grade the following answer based on the rubric:\nRubric: {self.question.rubrics}\nAnswer: {user_answer}"
|
| 80 |
|
| 81 |
+
response = client.chat.completions.create(
|
| 82 |
model="gpt-4o",
|
| 83 |
messages=[
|
| 84 |
{"role": "system", "content": "You are a helpful assistant."},
|
|
|
|
| 86 |
],
|
| 87 |
)
|
| 88 |
|
| 89 |
+
return {"result": "success", "message": response.choices[0].message.content}
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
print(f"An error occurred: {e}")
|
|
|
|
| 99 |
|
| 100 |
def __str__(self) -> str:
|
| 101 |
return f"{self.correct_answer} from {self.choices}"
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
class LLMGradedAnswer(Answer):
|
| 105 |
+
correct_answer = models.CharField(max_length=200, default="")
|
| 106 |
+
|
| 107 |
+
def grade(self, user_answer) -> dict:
|
| 108 |
+
"""
|
| 109 |
+
Grades the user's answer by calling the grading API.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
user_answer (str): The answer provided by the user.
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
dict: The result of the grading.
|
| 116 |
+
"""
|
| 117 |
+
try:
|
| 118 |
+
# use dotenv to load env
|
| 119 |
+
from dotenv import load_dotenv
|
| 120 |
+
|
| 121 |
+
load_dotenv()
|
| 122 |
+
prompt = f"Grade the following answer based on the rubric:\nRubric: {self.question.rubrics}\nAnswer: {user_answer}"
|
| 123 |
+
|
| 124 |
+
response = client.chat.completions.create(
|
| 125 |
+
model="gpt-4o",
|
| 126 |
+
messages=[
|
| 127 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 128 |
+
{"role": "user", "content": prompt},
|
| 129 |
+
],
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
return {"result": "success", "message": response.choices[0].message.content}
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"An error occurred: {e}")
|
| 136 |
+
return {"result": "error", "message": str(e)}
|
src/quizzes/templates/quizzes/partial.html
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
{% if error %}
|
| 4 |
<div class="alert alert-danger" role="alert">
|
| 5 |
{{ error }}
|
|
@@ -7,15 +5,16 @@
|
|
| 7 |
{% else %}
|
| 8 |
|
| 9 |
{% load markdownify %}
|
| 10 |
-
{% if
|
| 11 |
-
β
You got it!
|
| 12 |
-
{% elif llm_answer %}
|
| 13 |
β
LLM's results based on rubrics:
|
| 14 |
|
| 15 |
-
{{
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
{% else %}
|
| 18 |
β Sorry! The correct answer is {{ correct_answer }}
|
| 19 |
{% endif %}
|
| 20 |
|
| 21 |
-
{% endif %}
|
|
|
|
|
|
|
|
|
|
| 1 |
{% if error %}
|
| 2 |
<div class="alert alert-danger" role="alert">
|
| 3 |
{{ error }}
|
|
|
|
| 5 |
{% else %}
|
| 6 |
|
| 7 |
{% load markdownify %}
|
| 8 |
+
{% if llm_answer %}
|
|
|
|
|
|
|
| 9 |
β
LLM's results based on rubrics:
|
| 10 |
|
| 11 |
+
{{ llm_answer|markdownify }}
|
| 12 |
+
|
| 13 |
+
{% elif is_correct %}
|
| 14 |
+
β
You got it!
|
| 15 |
|
| 16 |
{% else %}
|
| 17 |
β Sorry! The correct answer is {{ correct_answer }}
|
| 18 |
{% endif %}
|
| 19 |
|
| 20 |
+
{% endif %}
|
src/quizzes/views.py
CHANGED
|
@@ -67,3 +67,57 @@ def grade_question(request, question_id):
|
|
| 67 |
"quizzes/partial.html",
|
| 68 |
{"is_correct": is_correct, "correct_answer": correct_answer},
|
| 69 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"quizzes/partial.html",
|
| 68 |
{"is_correct": is_correct, "correct_answer": correct_answer},
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def grade_question(request, question_id):
|
| 73 |
+
question = get_object_or_404(Question, pk=question_id)
|
| 74 |
+
answer = question.get_answer()
|
| 75 |
+
if answer is None:
|
| 76 |
+
return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)
|
| 77 |
+
|
| 78 |
+
user_answer = request.POST.get("answer")
|
| 79 |
+
llm_answer = None
|
| 80 |
+
if hasattr(answer, "grade"):
|
| 81 |
+
result = answer.grade(user_answer)
|
| 82 |
+
is_correct = result.get("result") == "success"
|
| 83 |
+
llm_answer = result.get("message")
|
| 84 |
+
else:
|
| 85 |
+
is_correct = answer.is_correct(user_answer)
|
| 86 |
+
llm_answer = None
|
| 87 |
+
|
| 88 |
+
return render(
|
| 89 |
+
request,
|
| 90 |
+
"quizzes/partial.html",
|
| 91 |
+
{
|
| 92 |
+
"is_correct": is_correct,
|
| 93 |
+
"correct_answer": answer.correct_answer,
|
| 94 |
+
"llm_answer": llm_answer,
|
| 95 |
+
},
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def grade_question(request, question_id):
|
| 100 |
+
question = get_object_or_404(Question, pk=question_id)
|
| 101 |
+
answer = question.get_answer()
|
| 102 |
+
if answer is None:
|
| 103 |
+
return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)
|
| 104 |
+
|
| 105 |
+
user_answer = request.POST.get("answer")
|
| 106 |
+
llm_answer = None
|
| 107 |
+
if hasattr(answer, "grade"):
|
| 108 |
+
result = answer.grade(user_answer)
|
| 109 |
+
is_correct = result.get("result") == "success"
|
| 110 |
+
llm_answer = result.get("message")
|
| 111 |
+
else:
|
| 112 |
+
is_correct = answer.is_correct(user_answer)
|
| 113 |
+
llm_answer = None
|
| 114 |
+
|
| 115 |
+
return render(
|
| 116 |
+
request,
|
| 117 |
+
"quizzes/partial.html",
|
| 118 |
+
{
|
| 119 |
+
"is_correct": is_correct,
|
| 120 |
+
"correct_answer": answer.correct_answer,
|
| 121 |
+
"llm_answer": llm_answer,
|
| 122 |
+
},
|
| 123 |
+
)
|