-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: integrate system prompt #47
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Toggles for learning-assistant app. | ||
""" | ||
|
||
WAFFLE_NAMESPACE = 'learning_assistant' | ||
|
||
# .. toggle_name: learning_assistant.enable_course_content | ||
# .. toggle_implementation: CourseWaffleFlag | ||
# .. toggle_default: False | ||
# .. toggle_description: Waffle flag to enable the course content integration with the learning assistant | ||
# .. toggle_use_cases: temporary | ||
# .. toggle_creation_date: 2024-01-08 | ||
# .. toggle_target_removal_date: 2024-01-31 | ||
# .. toggle_tickets: COSMO-80 | ||
ENABLE_COURSE_CONTENT = 'enable_course_content' | ||
|
||
|
||
def _is_learning_assistant_waffle_flag_enabled(flag_name, course_key): | ||
""" | ||
Import and return Waffle flag for enabling the summary hook. | ||
""" | ||
# pylint: disable=import-outside-toplevel | ||
try: | ||
from openedx.core.djangoapps.waffle_utils import CourseWaffleFlag | ||
return CourseWaffleFlag(f'{WAFFLE_NAMESPACE}.{flag_name}', __name__).is_enabled(course_key) | ||
except ImportError: | ||
return False | ||
|
||
|
||
def course_content_enabled(course_key): | ||
""" | ||
Return whether the learning_assistant.enable_course_content WaffleFlag is on. | ||
""" | ||
return _is_learning_assistant_waffle_flag_enabled(ENABLE_COURSE_CONTENT, course_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Utils file for learning-assistant. | ||
""" | ||
import copy | ||
import json | ||
import logging | ||
|
||
|
@@ -9,6 +10,8 @@ | |
from requests.exceptions import ConnectTimeout | ||
from rest_framework import status as http_status | ||
|
||
from learning_assistant.platform_imports import get_cache_course_run_data | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -22,21 +25,31 @@ def _estimated_message_tokens(message): | |
return int((len(message) - message.count(' ')) / chars_per_token) + json_padding | ||
|
||
|
||
def get_reduced_message_list(system_list, message_list): | ||
def get_reduced_message_list(prompt_template, message_list): | ||
""" | ||
If messages are larger than allotted token amount, return a smaller list of messages. | ||
""" | ||
total_system_tokens = sum(_estimated_message_tokens(system_message['content']) for system_message in system_list) | ||
# 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you leave a comment about why we're estimating these and including them in the count, please? |
||
+ _estimated_message_tokens('.' * 116) # average number of characters for skill names is 116 | ||
) | ||
|
||
max_tokens = getattr(settings, 'CHAT_COMPLETION_MAX_TOKENS', 16385) | ||
response_tokens = getattr(settings, 'CHAT_COMPLETION_RESPONSE_TOKENS', 1000) | ||
remaining_tokens = max_tokens - response_tokens - total_system_tokens | ||
|
||
new_message_list = [] | ||
# use copy of list, as it is modified as part of the reduction | ||
message_list_copy = copy.deepcopy(message_list) | ||
total_message_tokens = 0 | ||
|
||
while total_message_tokens < remaining_tokens and len(message_list) != 0: | ||
new_message = message_list.pop() | ||
while total_message_tokens < remaining_tokens and len(message_list_copy) != 0: | ||
new_message = message_list_copy.pop() | ||
total_message_tokens += _estimated_message_tokens(new_message['content']) | ||
if total_message_tokens >= remaining_tokens: | ||
break | ||
|
@@ -47,7 +60,34 @@ def get_reduced_message_list(system_list, message_list): | |
return new_message_list | ||
|
||
|
||
def get_chat_response(system_list, message_list): | ||
def get_course_id(course_run_id): | ||
""" | ||
Given a course run id (str), return the associated course key. | ||
""" | ||
course_data = get_cache_course_run_data(course_run_id, ['course']) | ||
course_key = course_data['course'] | ||
return course_key | ||
|
||
|
||
def create_request_body(prompt_template, message_list, courserun_id): | ||
""" | ||
Form request body to be passed to the chat endpoint. | ||
""" | ||
response_body = { | ||
'context': { | ||
'content': prompt_template, | ||
'render': { | ||
'doc_id': get_course_id(courserun_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, courserun_id): | ||
""" | ||
Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting. | ||
""" | ||
|
@@ -58,8 +98,7 @@ def get_chat_response(system_list, message_list): | |
connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1) | ||
read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15) | ||
|
||
reduced_messages = get_reduced_message_list(system_list, message_list) | ||
body = {'message_list': system_list + reduced_messages} | ||
body = create_request_body(prompt_template, message_list, courserun_id) | ||
|
||
try: | ||
response = requests.post( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ djangorestframework | |
edx-drf-extensions | ||
edx-rest-api-client | ||
edx-opaque-keys | ||
jinja2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to leave some documentation about what we're doing here and why - that we need to get the course ID and not the course run ID, and that this is the easiest place to get it from the platform. I think this could be a potentially unclear choice to future readers. I feel like an ADR might be overkill, so what do you think about leaving a brief comment about why we're using the catalog cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see you left a comment in
utils.py::get_course_key
👍 . I think it might make a little more sense as a comment closer to the actual code that uses the catalog utility method, but thank you for including that comment!