Skip to content
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

Persist Structured Grading Instructions (Modeling) #373

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f510596
Refactor feedback reference
LeonWehrhahn Nov 16, 2024
ba85feb
Merge branch 'develop' into feature/modeling/reference
LeonWehrhahn Nov 16, 2024
6b98bb8
Enhance Apollon JSON transformer and parser for improved element ID m…
LeonWehrhahn Nov 18, 2024
2bba32f
Merge branch 'feature/modeling/reference' of https://github.com/ls1in…
LeonWehrhahn Nov 18, 2024
ac31664
Merge branch 'develop' into feature/modeling/reference
LeonWehrhahn Nov 18, 2024
bf5a80d
Merge branch 'develop' into feature/modeling/reference
LeonWehrhahn Nov 22, 2024
9de3f10
Add element_ids field to ModelingFeedback and update feedback convers…
LeonWehrhahn Nov 27, 2024
e15d11c
Merge branch 'feature/modeling/reference' of https://github.com/ls1in…
LeonWehrhahn Nov 28, 2024
1e8e21d
Add element_ids field to DBModelingFeedback model
LeonWehrhahn Nov 28, 2024
a5203ff
Add JSON type import to db_modeling_feedback.py
LeonWehrhahn Nov 29, 2024
2a169b8
Merge branch 'develop' into feature/modeling/reference
LeonWehrhahn Nov 29, 2024
b6af29a
Merge branch 'develop' into feature/modeling/reference
LeonWehrhahn Dec 3, 2024
7b488a3
add structured grading instruction cache
LeonWehrhahn Dec 3, 2024
f2604c9
Increase default max_tokens to 4000 in OpenAI model configuration
LeonWehrhahn Dec 3, 2024
87a66a7
Increase max_input_tokens to 5000 and update element_ids assignment i…
LeonWehrhahn Dec 3, 2024
322fd55
Refactor exercise models to implement polymorphism and establish rela…
LeonWehrhahn Dec 6, 2024
fda3f13
Merge branch 'feature/modeling/reference' into feature/modeling/caching
LeonWehrhahn Dec 6, 2024
a83ac8b
Refactor exercise storage and structured grading criterion handling; …
LeonWehrhahn Dec 6, 2024
55449ec
Merge branch 'develop' into feature/modeling/caching
LeonWehrhahn Dec 6, 2024
4292489
Fix pylint errors
LeonWehrhahn Dec 7, 2024
ca035d9
Merge branch 'develop' into feature/modeling/persist-structured-gradi…
LeonWehrhahn Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion athena/athena/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
return JSONResponse(
status_code=422,
content={"detail": exc.errors()},
)
)
3 changes: 2 additions & 1 deletion athena/athena/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
from .db_modeling_submission import DBModelingSubmission
from .db_programming_feedback import DBProgrammingFeedback
from .db_text_feedback import DBTextFeedback
from .db_modeling_feedback import DBModelingFeedback
from .db_modeling_feedback import DBModelingFeedback
from .db_structured_grading_criterion import DBStructuredGradingCriterion
17 changes: 14 additions & 3 deletions athena/athena/models/db_exercise.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
from sqlalchemy import Column, String, Float, JSON, Enum as SqlEnum

from sqlalchemy.orm import relationship
from athena.database import Base
from athena.schemas import ExerciseType
from .model import Model
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement


class DBExercise(Model):
class DBExercise(Model, Base):
__tablename__ = "exercise"
id = Column(BigIntegerWithAutoincrement, primary_key=True, index=True, nullable=False)
lms_url = Column(String, index=True, nullable=False)
title = Column(String, index=True, nullable=False)
type = Column(SqlEnum(ExerciseType), index=True, nullable=False)
max_points = Column(Float, index=True, nullable=False)
bonus_points = Column(Float, index=True, nullable=False)
grading_instructions = Column(String)
problem_statement = Column(String)
grading_criteria = Column(JSON, nullable=True)
meta = Column(JSON, nullable=False)

structured_grading_criterion = relationship("DBStructuredGradingCriterion", back_populates="exercise", uselist=False) # Use uselist=False for one-to-one

# Polymorphism, discriminator attribute
type = Column(SqlEnum(ExerciseType), index=True, nullable=False)

__mapper_args__ = {
'polymorphic_identity': 'exercise',
'polymorphic_on': 'type'
}
19 changes: 11 additions & 8 deletions athena/athena/models/db_modeling_exercise.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from sqlalchemy import Column, String
from athena.schemas.exercise_type import ExerciseType
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship

from athena.database import Base
from .db_exercise import DBExercise


class DBModelingExercise(DBExercise, Base):
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement
class DBModelingExercise(DBExercise):
__tablename__ = "modeling_exercises"

example_solution: str = Column(String) # type: ignore

example_solution = Column(String) # type: ignore

id = Column(BigIntegerWithAutoincrement, ForeignKey('exercise.id'), primary_key=True)
submissions = relationship("DBModelingSubmission", back_populates="exercise")
feedbacks = relationship("DBModelingFeedback", back_populates="exercise")

__mapper_args__ = {
'polymorphic_identity': ExerciseType.modeling.value
}
12 changes: 9 additions & 3 deletions athena/athena/models/db_programming_exercise.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from sqlalchemy import Column, String
from athena.schemas.exercise_type import ExerciseType
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship

from athena.database import Base
from .db_exercise import DBExercise
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement


class DBProgrammingExercise(DBExercise, Base):
class DBProgrammingExercise(DBExercise):
__tablename__ = "programming_exercises"

id = Column(BigIntegerWithAutoincrement, ForeignKey('exercise.id'), primary_key=True)
programming_language: str = Column(String, nullable=False) # type: ignore
solution_repository_uri: str = Column(String, nullable=False) # type: ignore
template_repository_uri: str = Column(String, nullable=False) # type: ignore
tests_repository_uri: str = Column(String, nullable=False) # type: ignore

submissions = relationship("DBProgrammingSubmission", back_populates="exercise")
feedbacks = relationship("DBProgrammingFeedback", back_populates="exercise")

__mapper_args__ = {
'polymorphic_identity': ExerciseType.programming.value
}
17 changes: 17 additions & 0 deletions athena/athena/models/db_structured_grading_criterion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from sqlalchemy import Column, JSON, String, ForeignKey
from sqlalchemy.orm import relationship

from athena.database import Base
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement


class DBStructuredGradingCriterion(Base):
__tablename__ = "structured_grading_criterion"
id = Column(BigIntegerWithAutoincrement, primary_key=True, index=True,
autoincrement=True)
exercise_id = Column(BigIntegerWithAutoincrement, ForeignKey("exercise.id", ondelete="CASCADE"), index=True, unique=True) # Only one cached instruction per exercise
instructions_hash = Column(String, nullable=False)
structured_grading_criterion = Column(JSON, nullable=False)
lms_url = Column(String, nullable=False)

exercise = relationship("DBExercise", back_populates="structured_grading_criterion")
12 changes: 9 additions & 3 deletions athena/athena/models/db_text_exercise.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from sqlalchemy import Column, String
from athena.schemas.exercise_type import ExerciseType
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship

from athena.database import Base
from .db_exercise import DBExercise
from .big_integer_with_autoincrement import BigIntegerWithAutoincrement


class DBTextExercise(DBExercise, Base):
class DBTextExercise(DBExercise):
__tablename__ = "text_exercises"
id = Column(BigIntegerWithAutoincrement, ForeignKey('exercise.id'), primary_key=True)

example_solution: str = Column(String) # type: ignore

submissions = relationship("DBTextSubmission", back_populates="exercise")
feedbacks = relationship("DBTextFeedback", back_populates="exercise")

__mapper_args__ = {
'polymorphic_identity': ExerciseType.text.value
}
3 changes: 2 additions & 1 deletion athena/athena/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
from .modeling_feedback import ModelingFeedback
from .modeling_exercise import ModelingExercise
from .modeling_submission import ModelingSubmission
from .grading_criterion import GradingCriterion, StructuredGradingInstruction, StructuredGradingCriterion
from .grading_criterion import GradingCriterion, StructuredGradingInstruction
from .structured_grading_criterion import StructuredGradingCriterion
5 changes: 1 addition & 4 deletions athena/athena/schemas/grading_criterion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ class GradingCriterion(Schema, ABC):
title: Optional[str] = Field(None, example="Some instructions")
structured_grading_instructions: List[StructuredGradingInstruction] = Field(
[], example=[{"credits": 1.0, "gradingScale": "Good", "instructionDescription": "Some instructions", "feedback": "Nicely done!", "usageCount": 1},
{"credits": 0.0, "gradingScale": "Bad", "instructionDescription": "Some instructions", "feedback": "Try again!", "usageCount": 0}])

class StructuredGradingCriterion(BaseModel):
criteria: List[GradingCriterion]
{"credits": 0.0, "gradingScale": "Bad", "instructionDescription": "Some instructions", "feedback": "Try again!", "usageCount": 0}])
7 changes: 7 additions & 0 deletions athena/athena/schemas/structured_grading_criterion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import List
from athena.schemas.grading_criterion import GradingCriterion
from pydantic import BaseModel


class StructuredGradingCriterion(BaseModel):
criteria: List[GradingCriterion]
31 changes: 31 additions & 0 deletions athena/athena/storage/structured_grading_criterion_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Optional
from athena.contextvars import get_lms_url
from athena.database import get_db

from athena.models import DBStructuredGradingCriterion
from athena.schemas import StructuredGradingCriterion

def get_structured_grading_criterion(exercise_id: int, current_hash: Optional[str] = None) -> Optional[StructuredGradingCriterion]:
lms_url = get_lms_url()
with get_db() as db:
cache_entry = db.query(DBStructuredGradingCriterion).filter_by(
exercise_id=exercise_id, lms_url=lms_url
).first()
if cache_entry is not None and (current_hash is None or cache_entry.instructions_hash == current_hash): # type: ignore
return StructuredGradingCriterion.parse_obj(cache_entry.structured_grading_criterion)
return None

def store_structured_grading_criterion(
exercise_id: int, hash: str, structured_instructions: StructuredGradingCriterion
):
lms_url = get_lms_url()
with get_db() as db:
db.merge(
DBStructuredGradingCriterion(
exercise_id=exercise_id,
lms_url=lms_url,
instructions_hash=hash,
structured_grading_criterion=structured_instructions.dict(),
)
)
db.commit()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from athena.schemas.grading_criterion import StructuredGradingCriterion
from athena.schemas.structured_grading_criterion import StructuredGradingCriterion
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import List, Optional
import hashlib
import json
from typing import Any, Dict, List, Optional
from athena import logger
from athena.metadata import emit_meta
from athena.storage.structured_grading_criterion_storage import get_structured_grading_criterion, store_structured_grading_criterion
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate

from athena.schemas.grading_criterion import GradingCriterion, StructuredGradingCriterion
from athena.schemas import GradingCriterion, StructuredGradingCriterion
from llm_core.utils.predict_and_parse import predict_and_parse
from module_modeling_llm.config import BasicApproachConfig
from module_modeling_llm.models.exercise_model import ExerciseModel
Expand All @@ -19,6 +23,13 @@ async def get_structured_grading_instructions(

if grading_criteria:
return StructuredGradingCriterion(criteria=grading_criteria)

# Check if we have cached instructions for this exercise
current_hash = get_grading_instructions_hash(exercise_model)
cached_instructions = get_structured_grading_criterion(exercise_model.exercise_id, current_hash)
if cached_instructions:
print("Using cached instructions")
return cached_instructions

chat_prompt = ChatPromptTemplate.from_messages([
("system", config.generate_suggestions_prompt.structured_grading_instructions_system_message),
Expand Down Expand Up @@ -53,5 +64,22 @@ async def get_structured_grading_instructions(

if not grading_instruction_result:
raise ValueError("No structured grading instructions were returned by the model.")

# Cache the grading instructions
hash = get_grading_instructions_hash(exercise_model)
store_structured_grading_criterion(exercise_model.exercise_id, hash, grading_instruction_result)

return grading_instruction_result

def get_grading_instructions_hash(exercise: ExerciseModel) -> str:

hashable_data = {
"problem_statement": exercise.problem_statement,
"grading_instructions": exercise.grading_instructions,
"sample_solution": exercise.transformed_example_solution,
"max_points": exercise.max_points,
"bonus_points": exercise.bonus_points,
}

return grading_instruction_result
json_string = json.dumps(hashable_data, sort_keys=True, default=str)
return hashlib.sha256(json_string.encode()).hexdigest()
Loading