Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 56 additions & 12 deletions rag_service/core/assignment_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from datetime import datetime, timedelta
from typing import Any, Dict
from frappe.utils import now_datetime
import time
from ..monitoring import record_tap_lms_api_call

class AssignmentContextManager:
def __init__(self):
Expand Down Expand Up @@ -117,13 +119,34 @@ async def _fetch_assignment_from_api(self, assignment_id: str) -> Dict:
payload = {
"assignment_id": assignment_id
}
response = requests.post(
api_url,
headers=self.headers,
json=payload,
timeout=30
_api_t0 = time.monotonic()
_api_status = None
try:
response = requests.post(
api_url,
headers=self.headers,
json=payload,
timeout=30
)
_api_status = response.status_code
except Exception as _conn_err:
record_tap_lms_api_call(
submission_id=None,
endpoint="get_assignment_context",
duration_ms=(time.monotonic() - _api_t0) * 1000,
cache_hit=False,
status_code=None,
)
raise

record_tap_lms_api_call(
submission_id=None,
endpoint="get_assignment_context",
duration_ms=(time.monotonic() - _api_t0) * 1000,
cache_hit=False,
status_code=_api_status,
)

if response.status_code != 200:
error_msg = f"API request failed with status {response.status_code}: {response.text}"
print(f"Error: {error_msg}")
Expand Down Expand Up @@ -152,13 +175,34 @@ async def _fetch_student_from_api(self, student_id: str) -> Dict:
payload = {
"student_id": student_id
}
response = requests.post(
api_url,
headers=self.headers,
json=payload,
timeout=30
_api_t0 = time.monotonic()
_api_status = None
try:
response = requests.post(
api_url,
headers=self.headers,
json=payload,
timeout=30
)
_api_status = response.status_code
except Exception as _conn_err:
record_tap_lms_api_call(
submission_id=None,
endpoint="get_student_context",
duration_ms=(time.monotonic() - _api_t0) * 1000,
cache_hit=False,
status_code=None,
)
raise

record_tap_lms_api_call(
submission_id=None,
endpoint="get_student_context",
duration_ms=(time.monotonic() - _api_t0) * 1000,
cache_hit=False,
status_code=_api_status,
)

if response.status_code != 200:
error_msg = f"API request failed with status {response.status_code}: {response.text}"
print(f"Error: {error_msg}")
Expand Down
37 changes: 37 additions & 0 deletions rag_service/core/feedback_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# rag_service/rag_service/core/feedback_handler.py

import time
import frappe
from ..monitoring import (
record_rag_submission_received,
record_rag_feedback_complete,
record_rag_feedback_failed,
)
import json
from datetime import datetime
from typing import Dict, Optional
Expand All @@ -18,6 +24,18 @@ def __init__(self):
async def handle_submission(self, message_data: Dict) -> None:
"""Handle a new submission from plagiarism queue"""
request_id = None
_t0 = time.monotonic()
submission_id = message_data.get("submission_id")
student_id = message_data.get("student_id")
assignment_id = message_data.get("assignment_id")

# SRE: pipeline trace — step 1 in rag_service
record_rag_submission_received(
submission_id=submission_id,
student_id=student_id,
assignment_id=assignment_id,
)

try:
submission_data = normalize_submission_payload(message_data)

Expand Down Expand Up @@ -47,6 +65,15 @@ async def handle_submission(self, message_data: Dict) -> None:
error_msg = f"Error handling submission: {str(e)}"
print(f"\nError: {error_msg}")
frappe.log_error(error_msg, "Submission Handler Error")
# SRE: pipeline trace — failure
try:
record_rag_feedback_failed(
submission_id=submission_id,
error=error_msg,
duration_ms=(time.monotonic() - _t0) * 1000,
)
except Exception:
pass

# Mark request as failed if it exists
if request_id and frappe.db.exists("Feedback Request", request_id):
Expand All @@ -62,6 +89,16 @@ async def handle_submission(self, message_data: Dict) -> None:
# Process and deliver feedback
await self.feedback_service.process_feedback(request_id, feedback, model_used, template_used)
print("\nFeedback processing completed")
# SRE: pipeline trace — complete (always emitted, success or error)
try:
record_rag_feedback_complete(
submission_id=submission_id,
model_used=model_used,
template_used=template_used,
duration_ms=(time.monotonic() - _t0) * 1000,
)
except Exception:
pass

def _attach_plagiarism_defaults(self, feedback: Dict) -> Dict:
feedback["plagiarism_output"] = {
Expand Down
27 changes: 24 additions & 3 deletions rag_service/core/feedback_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import Dict
from ..feedback_utils.evaluation_generation import EvaluationGenerator
from ..utils.queue_manager import QueueManager
import time
from ..monitoring import record_llm_call

class FeedbackService:
def __init__(self):
Expand Down Expand Up @@ -47,9 +49,28 @@ async def generate_feedback( self, assignment_context: Dict, submission_data: Di
# Continue with normal feedback generation for original work
else:
result_status = "Success - Original"
feedback, model_used, tempalate_used = await self.evaluation_generator.generate_ai_feedback(
assignment_context, submission_data, submission_id
)
_llm_t0 = time.monotonic()
try:
feedback, model_used, tempalate_used = await self.evaluation_generator.generate_ai_feedback(
assignment_context, submission_data, submission_id
)
record_llm_call(
submission_id=submission_id,
provider=model_used,
model=model_used,
status="success",
duration_ms=(time.monotonic() - _llm_t0) * 1000,
)
except Exception as _llm_err:
record_llm_call(
submission_id=submission_id,
provider="unknown",
model="unknown",
status="error",
duration_ms=(time.monotonic() - _llm_t0) * 1000,
error=str(_llm_err),
)
raise

feedback["translation_language"] = assignment_context["student"].get("language", "English")
await self._update_result_status(feedback_request_id, result_status)
Expand Down
Loading