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 21fc3b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions learning_assistant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 = {'role': 'assistant', 'content': '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 +48,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 21fc3b9

Please sign in to comment.