Skip to content

Commit

Permalink
Merge branch 'main' into jenkins/zshkoor/setup-py-updated-5811f7a
Browse files Browse the repository at this point in the history
  • Loading branch information
BilalQamar95 authored Dec 10, 2024
2 parents 81f8c32 + ed0061e commit 5b929f6
Show file tree
Hide file tree
Showing 57 changed files with 3,933 additions and 675 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[run]
branch = True
data_file = .coverage
relative_files = True
source=learning_assistant
omit =
test_settings
*migrations*
*admin.py
*static*
*templates*
learning_assistant/plugins.py
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-20.04]
python-version: ['3.8']
toxenv: [quality, pii_check, django32, django40]
python-version: ['3.12']
toxenv: [quality, pii_check, django42]

steps:
- uses: actions/checkout@v2
Expand All @@ -37,8 +37,11 @@ jobs:
run: tox

- name: Run coverage
if: matrix.python-version == '3.8' && matrix.toxenv == 'django32'
uses: codecov/codecov-action@v1
if: matrix.python-version == '3.12' && matrix.toxenv == 'django42'
uses: py-cov-action/python-coverage-comment-action@v3
with:
flags: unittests
fail_ci_if_error: true
GITHUB_TOKEN: ${{ github.token }}
MINIMUM_GREEN: 95
MINIMUM_ORANGE: 84
ANNOTATE_MISSING_LINES: true
ANNOTATION_TYPE: error
2 changes: 1 addition & 1 deletion .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: setup python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.12

- name: Install pip
run: pip install -r requirements/pip.txt
Expand Down
126 changes: 126 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,132 @@ Change Log

.. There should always be an "Unreleased" section for changes pending release.
Unreleased
**********

4.5.0 - 2024-12-04
******************
* Add local setup to readme
* Add a BFFE chat summary endpoint for Learning Assistant, including information about whether the Learning Assistant is
enabled, Learning Assistant message history, and Learning Assistant audit trial data.

4.4.7 - 2024-11-25
******************
* Fixes the Course Chat View CourseMode concatenation issue

4.4.6 - 2024-11-22
******************
* Gates the chat history endpoint behind a waffle flag
* Add LearningAssistantAuditTrial model

4.4.5 - 2024-11-12
******************
* Updated Learning Assistant History payload to return in ascending order

4.4.4 - 2024-11-06
******************
* Fixed Learning Assistant History endpoint
* Added timestamp to the Learning Assistant History payload

4.4.3 - 2024-11-06
******************
* Fixed package version

4.4.2 - 2024-11-04
******************
* Added chat messages to the DB

4.4.1 - 2024-10-31
******************
* Add management command to remove expired messages

4.4.0 - 2024-10-30
******************
* Add LearningAssistantMessage model
* Add new GET endpoint to retrieve a user's message history in a given course.

4.4.0 - 2024-10-25
******************
* Upgraded to use ``Python 3.12``

4.3.3 - 2024-10-15
******************
* Use `LEARNING_ASSISTANT_PROMPT_TEMPLATE` for prompt

4.3.2 - 2024-09-19
******************
* Add error handling for invalid unit usage keys

4.3.1 - 2024-09-10
******************
* Remove GPT model field as part of POST request to Xpert backend

4.3.0 - 2024-07-01
******************
* Adds optional parameter to use updated prompt and model for the chat response.

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.

4.0.0 - 2024-02-21
******************
* Remove use of course waffle flag. Use the django setting LEARNING_ASSISTANT_AVAILABLE
to enable the learning assistant feature.

3.6.0 - 2024-02-13
******************
* Enable backend access by course waffle flag or django setting.

3.4.0 - 2024-01-30
******************
* Add new GET endpoint to retrieve whether Learning Assistant is enabled in a given course.

3.3.0 - 2024-01-30
******************
* Fix release version

3.2.0 - 2024-01-30
******************
* Remove audit access to chat view.

3.0.1 - 2024-01-29
******************
* Modify gating of learning assistant based on waffle flag and enabled value.

3.0.0 - 2024-01-23
******************
* Remove and drop the course prompt model.

2.0.3 - 2024-01-22
******************
* Remove references to the course prompt model.

2.0.1 - 2024-01-08
******************
* Gate content integration with waffle flag

2.0.0 - 2024-01-03
******************
* Add content cache
* Integrate system prompt setting

1.5.0 - 2023-10-18
******************
* Add management command to generate course prompts

1.4.0 - 2023-09-11
******************
* Send reduced message list if needed to avoid going over token limit

1.3.3 - 2023-09-07
******************
* Allow any enrolled learner to access API.

1.3.2 - 2023-08-25
******************
* Remove deserialization of prompt field, as it is represented in the python
Expand Down
25 changes: 24 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,37 @@ One Time Setup
--------------
.. code-block::
# Clone the repository
# Clone the repository (in the ../src relative to devstack repo)
git clone [email protected]:openedx/learning-assistant.git
cd learning-assistant
# Set up a virtualenv with the same name as the repo and activate it
# Here's how you might do that if you have virtualenvwrapper setup.
mkvirtualenv -p python3.8 learning-assistant
In your ``requirements/edx/private.txt`` requirements file in edx-platform, add:

.. code-block::
-e /edx/src/learning-assistant
In your ``lms/envs/private.py`` settings file in edx-platform (create file if necessary), add the below settings. The value of the API key shouldn't matter, because it's not being used at this point, but the setting needs to be there.

.. code-block::
CHAT_COMPLETION_API = '' # copy url from edx-internal
CHAT_COMPLETION_API_KEY = '' # add value though value itself does not matter
LEARNING_ASSISTANT_PROMPT_TEMPLATE = '' # copy value from edx-internal
LEARNING_ASSISTANT_AVAILABLE = True
In devstack, run ``make lms-shell`` and run the following command: ``paver install_prereqs;exit``. This will install anything included in your ``private.txt`` requirements file.

In django admin, add the following waffle flag ``learning_assistant.enable_course_content`` and make sure it is turned on for Everyone. The flag should be checked on for: Superusers, Staff, and Authenticated.

This plugin depends on the lms and discovery - both should be running.

Every time you develop something in this repo
---------------------------------------------
.. code-block::
Expand Down
12 changes: 0 additions & 12 deletions codecov.yml

This file was deleted.

96 changes: 96 additions & 0 deletions docs/decisions/0002-system-prompt-design-changes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
2. System Prompt Design Changes
###############################

Status
******

**Accepted** *2023-12-13*

Context
*******

A system prompt in the Learning Assistant context refers to the text-based instructions provided to the large language
model (LLM) that describe the objective and proper behavior of the Learning Assistant. This prompt is provided to the
LLM via 2U's Xpert Platform generic chat completion endpoint as the first set of elements in the ``message_list``
payload key.

Currently, the system prompt is stored in the `CoursePrompt model`_ on a per-course basis. For each course in which the
Learning Assistant is enabled, a system prompt must be stored in the associated database table.

The original intention behind storing the system prompt in this way was to enable an expedited release, a greater degree
of per-course customization, and a flexibility to modify the system prompt quickly in the early stages of the project.

The process of releasing the Learning Assistant to a new course involves either the manual creation of the model
instance via the Django admin or the use of the `set_course_prompts management command`_. The latter requires that a
member of 2U's Site Reliability Engineering (SRE) team runs this command in the proper environment.

The next iteration of the Learning Assistant will enable the integration of unit content into the system prompt to
provide the LLM with more information about the context in which the learner is asking a question. This will require a
change to the system prompt to accommodate the unit content, and will, thus, require manual work on the part of an
engineer to update the existing system prompts and to create new system prompts as we approach a full roll out. This
presents an opportunity to reconsider the way that we store and process the system prompt.

Decision
********

* We will store a system prompt template in a Django setting in the form of a Jinja template.
* We will use Jinja constructs, such as variables and control structures, to implement a single system prompt template
for all courses.
* The system prompt template will be rendered in two steps.

* The first render step will be performed by the `learning-assistant`_ code. This step will interpolate any variables
for which this code has a value (e.g. unit content).
* The second render step will be performed by the 2U Xpert Platform generic chat completion endpoint. This step will
interpolate any variable for which the platform has a value (e.g. course title and skill names).

* After the first render step, the resulting value will be a Jinja template that has been partially rendered. This
template will be sent to 2U's Xpert Platform generic chat completion endpoint to be completely rendered.
* We will remove the `CoursePrompt model`_ following the instructions documented in
`Everything About Database Migrations`_.

Consequences
************

* Changes to the system prompt will require pull requests to the appropriate repository in which the prompt is stored.
* Anyone with access to the appropriate repository in which the prompt is stored can change the system prompt. Members
of the team will no longer require assistance from the SRE team.
* The transition to a system prompt template required the 2U Xpert Platform team to provide support for accepting and
rendering a system prompt template and the integration of Discovery ``skill_names`` into their index.
* The use of a system prompt template will require cross-team collaboration to ensure that the same variable names are
use in the system prompt template, in the `learning-assistant`_ code, and the 2U Xpert Platform generic chat
completion endpoint.
* Because the system prompt template will be stored in a Django setting in a private repository, and the code that
renders the template is stored here, changes to the template will require careful coordination to ensure that the
template is rendered properly.

* For example, if a new variable is added to the template, the code must be modified and deployed in advanced of
changes to the template. Otherwise, if changes to template are deployed before the related code changes, then the
rendered template will contain uninterpolated variables.

* It will become more difficult to enable per-course customizations because all courses will be served by a single
system prompt template.

Rejected Alternatives
*********************

* Status Quo

* The main alternative to this change is to continue to use manual entry and the `set_course_prompts management command`_
to manage system prompts.
* To enable unit content integration, we would need to modify the JSON string stored in the `CoursePrompt model`_ to
store a JSON string with format string variable for the content, which would be interpolated at runtime.
* The main advantage of this alternative is that it will require less engineering work.
* The main disadvantage of this alternative is that it will become challenging to manage which prompt a course should
use. For example, to do a stage released of unit content integration, we would be required to manage different
templates manually. Later, a full release would require additional changes. This would make management of the
templates even more tedious.
* Managing system prompts in a full release would become intractable.
* Additionally, in order to better operationalize the use of the management command and to reduce our reliance on the
SRE team, we would likely want to invest time in setting up a Jenkins job to run the management command ad-hoc. If
we will be investing engineering resources in this area anyway, we felt it was a more future-proof approach to
pursue the solution described above.

.. _set_course_prompts management command: https://github.com/edx/learning-assistant/blob/main/learning_assistant/management/commands/set_course_prompts.py
.. _CoursePrompt model: https://github.com/edx/learning-assistant/blob/34604a0775f7bd79adb465e0ca51c7759197bfa9/learning_assistant/models.py
.. _Everything About Database Migrations: https://openedx.atlassian.net/wiki/spaces/AC/pages/23003228/Everything+About+Database+Migrations#EverythingAboutDatabaseMigrations-Howtodropatable
.. _learning-assistant: https://github.com/edx/learning-assistant
62 changes: 62 additions & 0 deletions docs/decisions/0003-courseapp-use.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
3. CourseApp Use
################

Status
******

**Accepted** *2024-01-08*

Context
*******

We are adding the ability for course teams to manually enable and disable the Learning Assistant in their courses via
a card in the `Pages & Resources page`_ of Studio, provided that the Learning Assistant is enabled and available in
a given course.

The `Pages & Resources page`_ is powered on the backend by the edX Platform `CourseApp Django app`_. With one
exception in the case of Xpert Unit Summaries, each card on the `Pages & Resources page`_ has a corresponding instance
of the `CourseApp plugin`_ configured in the `edx-platform repository`_. The `Pages & Resources page`_ can then make
calls to the `backend CourseApp REST API`_ to get necessary information about the feature described by the plugin.

The one exception to this behavior is the ``Xpert unit summaries card``, which uses a `Javascript object`_ stored in a
static file in the `frontend-app-course-authoring repository`_ to describe the options that would otherwise be returned
by the `backend CourseApp REST API`_ using a corresponding ``CourseApp`` plugin.

Currently, the only ``CourseApp`` plugins registered in the platfrom are those that are defined within the
`edx-platform repository`_. There are currently no plugins that are defined outside of the `edx-platform repository`_.

Decision
********

* We will define an instance of the `CourseApp plugin`_ in this repository to describe the Learning Assistant feature.
* We will register the `CourseApp plugin`_ as an entrypoint with the ``openedx.course_app`` key so that the `CourseApp
Django app`_ will be able to pick up the plugin if the Learning Assistant plugin is installed into ``edx-platform``.

Consequences
************

* The Learning Assistant ``CourseApp`` plugin will only be registered with the `CourseApp Django app`_ if, and,
therefore, the Learning Assistant card on the `Pages & Resources page`_ will only be visible if, the Learning
Assistant plugin is installed into ``edx-platform``.
* It will become slightly more difficult to know which ``CourseApps`` are available simply by reading the code, because
this ``CourseApp`` plugin is not stored in the `edx-platform repository`_.
* Because the `CourseApps` API is registered under the CMS application, the Learning Assistant needs to be registered as
a plugin to the CMS as well. Otherwise, the plugin is not included in the CMS application's ``INSTALLED_APPS`` list.
This causes a runtime error, because the Learning Assistant CourseApp plugin will refer to the Learning Assistant's
models, and this are not available in the CMS if the Learning Assistant plugin is not installed.

Rejected Alternatives
*********************

* We decided not to add in a custom backend REST API for exposing the ability to introspect the Learning Assistant
feature and enable and disable it. There already exists the `CourseApp Django app`_ for this purpose, and using it
allows us to avoid writing a lot of ad hoc code.

.. _backend CourseApp REST API: https://github.com/openedx/edx-platform/blob/master/openedx/core/djangoapps/course_apps/rest_api/v1/views.py#L80
.. _CourseApp Django app: https://github.com/openedx/edx-platform/tree/master/openedx/core/djangoapps/course_apps
.. _CourseApp plugin: https://github.com/openedx/edx-platform/blob/master/openedx/core/djangoapps/course_apps/plugins.py#L15
.. _edx-platform: https://github.com/openedx/edx-platform
.. _edx-platform repository: https://github.com/openedx/edx-platform
.. _frontend-app-course-authoring repository: https://github.com/openedx/frontend-app-course-authoring/tree/master
.. _Javascript object: https://github.com/openedx/frontend-app-course-authoring/blob/master/src/pages-and-resources/xpert-unit-summary/appInfo.js
.. _Pages & Resources page: https://github.com/openedx/frontend-app-course-authoring/tree/master/src/pages-and-resources
Loading

0 comments on commit 5b929f6

Please sign in to comment.