From e42c37708f0267be46ceea0bc181a71368388d25 Mon Sep 17 00:00:00 2001 From: Alie Langston Date: Mon, 21 Aug 2023 14:08:18 -0400 Subject: [PATCH] feat: add auto response for development --- learning_assistant/dev_utils.py | 25 +++++++++++++++++++++++++ learning_assistant/utils.py | 16 ++++++++++++++++ test_settings.py | 4 ++++ tests/test_utils.py | 6 ++++++ 4 files changed, 51 insertions(+) create mode 100644 learning_assistant/dev_utils.py diff --git a/learning_assistant/dev_utils.py b/learning_assistant/dev_utils.py new file mode 100644 index 0000000..bb0d1ab --- /dev/null +++ b/learning_assistant/dev_utils.py @@ -0,0 +1,25 @@ +""" +Utils for development testing. +""" +import random + +words = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit,', 'sed', 'do', 'eiusmod', + 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua.', 'Ut', 'enim', 'ad', 'minim', + 'veniam,', 'quis', 'nostrud', 'exercitation', 'ullamco', 'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea', + 'commodo', 'consequat', 'Duis', 'aute', 'irure', 'dolor', 'in', 'reprehenderit', 'in', 'voluptate', 'velit', + 'esse', 'cillum', 'dolore', 'eu', 'fugiat', 'nulla', 'pariatur.', 'Excepteur', 'sint', 'occaecat', + 'cupidatat', 'non', 'proident,', 'sunt', 'in', 'culpa', 'qui', 'officia', 'deserunt', 'mollit', 'anim', + 'id', 'est', 'laborum'] + + +def generate_response(): + """ + Generate a random text response for development testing. + """ + # generate a list of 50 random words + content = random.choices(words, k=50) + + return { + 'role': 'assistant', + 'content': " ".join(content) + } diff --git a/learning_assistant/utils.py b/learning_assistant/utils.py index 9b47394..9fe9fc8 100644 --- a/learning_assistant/utils.py +++ b/learning_assistant/utils.py @@ -8,6 +8,8 @@ from requests.exceptions import ConnectTimeout from rest_framework import status as http_status +from learning_assistant.dev_utils import generate_response + log = logging.getLogger(__name__) @@ -15,6 +17,10 @@ def get_chat_response(message_list): """ Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting. """ + if auto_response_for_testing_enabled(): + auto_response = generate_response() + return http_status.HTTP_200_OK, auto_response + completion_endpoint = getattr(settings, 'CHAT_COMPLETION_API', None) if completion_endpoint: headers = {'Content-Type': 'application/json'} @@ -44,3 +50,13 @@ def get_chat_response(message_list): chat = 'Completion endpoint is not defined.' return response_status, chat + + +def auto_response_for_testing_enabled(): + """ + If AUTOMATIC_CHAT_RESPONSE_FOR_TESTING is True, we want to skip making a request to the chat completion endpoint. + + Bypass passing anything to the chat completion endpoint, as that endpoint is defined via a setting that + is not available in the dev environment. + """ + return settings.FEATURES.get('AUTOMATIC_CHAT_RESPONSE_FOR_TESTING') diff --git a/test_settings.py b/test_settings.py index e6a3c04..ec8e736 100644 --- a/test_settings.py +++ b/test_settings.py @@ -63,3 +63,7 @@ def root(*args): CHAT_COMPLETION_API = 'https://test.edx.org/' CHAT_COMPLETION_API_CONNECT_TIMEOUT = 0.5 CHAT_COMPLETION_API_READ_TIMEOUT = 10 + +FEATURES = { + 'AUTOMATIC_CHAT_RESPONSE_FOR_TESTING': False, +} diff --git a/tests/test_utils.py b/tests/test_utils.py index d46d18d..1857ac5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -68,3 +68,9 @@ def test_timeout(self, exception, mock_requests): mock_requests.post = MagicMock(side_effect=exception()) status_code, _ = get_chat_response(self.message_list) self.assertEqual(status_code, 502) + + @patch.dict(settings.FEATURES, {'AUTOMATIC_CHAT_RESPONSE_FOR_TESTING': True}) + def test_automatic_response(self): + status_code, message = get_chat_response(self.message_list) + self.assertEqual(status_code, 200) + self.assertIsNotNone(message)