Skip to content

Commit

Permalink
feat: update request to xpert backend (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Feb 29, 2024
1 parent dc9179c commit ec8cff5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Change Log
Unreleased
**********

4.2.0 - 2024-02-28
******************
* Modify call to Xpert backend to prevent use of course index.

4.1.0 - 2024-02-26
******************
* Use course cache to inject course title and course skill names into prompt template.
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Plugin for a learning assistant backend, intended for use within edx-platform.
"""

__version__ = '4.1.0'
__version__ = '4.2.0'

default_app_config = 'learning_assistant.apps.LearningAssistantConfig' # pylint: disable=invalid-name
27 changes: 7 additions & 20 deletions learning_assistant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ def get_reduced_message_list(prompt_template, message_list):
"""
If messages are larger than allotted token amount, return a smaller list of messages.
"""
# the total number of system tokens is a sum of estimated tokens that includes the prompt template, the
# course title, and the course skills. It is necessary to include estimations for the course title and
# course skills, as the chat endpoint the prompt is being passed to is responsible for filling in the values
# for both of those variables.
total_system_tokens = (
_estimated_message_tokens(prompt_template)
+ _estimated_message_tokens('.' * 40) # average number of characters per course name is 40
+ _estimated_message_tokens('.' * 116) # average number of characters for skill names is 116
)
total_system_tokens = _estimated_message_tokens(prompt_template)

max_tokens = getattr(settings, 'CHAT_COMPLETION_MAX_TOKENS', 16385)
response_tokens = getattr(settings, 'CHAT_COMPLETION_RESPONSE_TOKENS', 1000)
Expand All @@ -55,28 +47,23 @@ def get_reduced_message_list(prompt_template, message_list):
# insert message at beginning of list, because we are traversing the message list from most recent to oldest
new_message_list.insert(0, new_message)

return new_message_list
system_message = {'role': 'system', 'content': prompt_template}

return [system_message] + new_message_list

def create_request_body(prompt_template, message_list, course_id):

def create_request_body(prompt_template, message_list):
"""
Form request body to be passed to the chat endpoint.
"""
response_body = {
'context': {
'content': prompt_template,
'render': {
'doc_id': course_id,
'fields': ['skillNames', 'title']
}
},
'message_list': get_reduced_message_list(prompt_template, message_list)
}

return response_body


def get_chat_response(prompt_template, message_list, course_id):
def get_chat_response(prompt_template, message_list):
"""
Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting.
"""
Expand All @@ -87,7 +74,7 @@ def get_chat_response(prompt_template, message_list, course_id):
connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1)
read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15)

body = create_request_body(prompt_template, message_list, course_id)
body = create_request_body(prompt_template, message_list)

try:
response = requests.post(
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def post(self, request, course_run_id):

prompt_template = render_prompt_template(request, request.user.id, course_run_id, unit_id, course_id)

status_code, message = get_chat_response(prompt_template, message_list, course_id)
status_code, message = get_chat_response(prompt_template, message_list)

return Response(status=status_code, data=message)

Expand Down
27 changes: 13 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setUp(self):
self.course_id = 'edx+test'

def get_response(self):
return get_chat_response(self.prompt_template, self.message_list, self.course_id)
return get_chat_response(self.prompt_template, self.message_list)

@override_settings(CHAT_COMPLETION_API=None)
def test_no_endpoint_setting(self):
Expand Down Expand Up @@ -89,14 +89,7 @@ def test_post_request_structure(self, mock_requests):
headers = {'Content-Type': 'application/json', 'x-api-key': settings.CHAT_COMPLETION_API_KEY}

response_body = {
'context': {
'content': self.prompt_template,
'render': {
'doc_id': self.course_id,
'fields': ['skillNames', 'title']
}
},
'message_list': self.message_list
'message_list': [{'role': 'system', 'content': self.prompt_template}] + self.message_list
}

self.get_response()
Expand All @@ -120,21 +113,27 @@ def setUp(self):
{'role': 'user', 'content': 'Goodbye'},
]

@override_settings(CHAT_COMPLETION_MAX_TOKENS=90)
@override_settings(CHAT_COMPLETION_MAX_TOKENS=30)
@override_settings(CHAT_COMPLETION_RESPONSE_TOKENS=1)
def test_message_list_reduced(self):
"""
If the number of tokens in the message list is greater than allowed, assert that messages are removed
"""
# pass in copy of list, as it is modified as part of the reduction
reduced_message_list = get_reduced_message_list(self.prompt_template, self.message_list)
self.assertEqual(len(reduced_message_list), 1)
self.assertEqual(reduced_message_list, self.message_list[-1:])
self.assertEqual(len(reduced_message_list), 2)
self.assertEqual(
reduced_message_list,
[{'role': 'system', 'content': self.prompt_template}] + self.message_list[-1:]
)

def test_message_list(self):
reduced_message_list = get_reduced_message_list(self.prompt_template, self.message_list)
self.assertEqual(len(reduced_message_list), 2)
self.assertEqual(reduced_message_list, self.message_list)
self.assertEqual(len(reduced_message_list), 3)
self.assertEqual(
reduced_message_list,
[{'role': 'system', 'content': self.prompt_template}] + self.message_list
)


@ddt.ddt
Expand Down

0 comments on commit ec8cff5

Please sign in to comment.