Skip to content

Commit

Permalink
feat: Added save_chat_message() to API
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Oct 30, 2024
1 parent 5c3427a commit c91ea21
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
22 changes: 21 additions & 1 deletion learning_assistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import logging

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from edx_django_utils.cache import get_cache_key
from jinja2 import BaseLoader, Environment
from opaque_keys import InvalidKeyError

from learning_assistant.constants import ACCEPTED_CATEGORY_TYPES, CATEGORY_TYPE_MAP
from learning_assistant.data import LearningAssistantCourseEnabledData
from learning_assistant.models import LearningAssistantCourseEnabled
from learning_assistant.models import LearningAssistantCourseEnabled, LearningAssistantMessage
from learning_assistant.platform_imports import (
block_get_children,
block_leaf_filter,
Expand All @@ -24,6 +25,7 @@
from learning_assistant.text_utils import html_to_text

log = logging.getLogger(__name__)
User = get_user_model()


def _extract_block_contents(child, category):
Expand Down Expand Up @@ -187,3 +189,21 @@ def get_course_id(course_run_id):
course_data = get_cache_course_run_data(course_run_id, ['course'])
course_key = course_data['course']
return course_key

def save_chat_message(user_id, chat_role, message):
"""
Saves the chat message to the database.
"""

user = None
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
raise Exception("User does not exists.")

# Save the user message to the database.
LearningAssistantMessage.objects.create(
user=user,
role=chat_role,
content=message,
)
30 changes: 28 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import ddt
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import TestCase, override_settings
from opaque_keys import InvalidKeyError
Expand All @@ -19,13 +20,14 @@
learning_assistant_available,
learning_assistant_enabled,
render_prompt_template,
save_chat_message,
set_learning_assistant_enabled,
)
from learning_assistant.data import LearningAssistantCourseEnabledData
from learning_assistant.models import LearningAssistantCourseEnabled
from learning_assistant.models import LearningAssistantCourseEnabled, LearningAssistantMessage

fake_transcript = 'This is the text version from the transcript'

User = get_user_model()

class FakeChild:
"""Fake child block for testing"""
Expand Down Expand Up @@ -230,6 +232,30 @@ def test_render_prompt_template_invalid_unit_key(self, mock_get_content):

self.assertNotIn('The following text is useful.', prompt_text)

@ddt.ddt
class TestLearningAssistantCourseEnabledApi(TestCase):
"""
Test suite for save_chat_message.
"""
def setUp(self):
super().setUp()

self.test_user = User.objects.create(username='username', password='password')

@ddt.data(
(LearningAssistantMessage.USER_ROLE, 'What is the meaning of life, the universe and everything?'),
(LearningAssistantMessage.ASSISTANT_ROLE, '42'),
)
@ddt.unpack
def test_save_chat_message(self, chat_role, message):
save_chat_message(self.test_user.id, chat_role, message)

row = LearningAssistantMessage.objects.all().last()

self.assertEqual(row.role, chat_role)
self.assertEqual(row.content, message)



@ddt.ddt
class LearningAssistantCourseEnabledApiTests(TestCase):
Expand Down

0 comments on commit c91ea21

Please sign in to comment.