Skip to content

Commit

Permalink
feat: add auto response for development
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto committed Aug 21, 2023
1 parent b401572 commit e42c377
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions learning_assistant/dev_utils.py
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions learning_assistant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
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__)


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'}
Expand Down Expand Up @@ -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')
4 changes: 4 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e42c377

Please sign in to comment.