-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add json field to store prompt message content
- Loading branch information
Showing
7 changed files
with
97 additions
and
24 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
""" | ||
Library for the learning_assistant app. | ||
""" | ||
import json | ||
|
||
from learning_assistant.models import CoursePrompt | ||
|
||
|
||
def get_prompt_by_course_id(course_id): | ||
def get_deserialized_prompt_content_by_course_id(course_id): | ||
""" | ||
Return a deserialized prompt given a course_id | ||
""" | ||
json_prompt = CoursePrompt.get_json_prompt_content_by_course_id(course_id) | ||
if json_prompt: | ||
prompt_messages = json.loads(json_prompt) | ||
return prompt_messages | ||
return None | ||
|
||
|
||
def get_setup_messages(course_id): | ||
""" | ||
Return a prompt associated with a given course id. | ||
Return a list of setup messages given a course id | ||
""" | ||
return CoursePrompt.get_prompt_by_course_id(course_id) | ||
message_content = get_deserialized_prompt_content_by_course_id(course_id) | ||
if message_content: | ||
setup_messages = [{'role': 'system', 'content': x} for x in message_content] | ||
return setup_messages | ||
return None |
18 changes: 18 additions & 0 deletions
18
learning_assistant/migrations/0002_courseprompt_json_prompt.py
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,18 @@ | ||
# Generated by Django 3.2.20 on 2023-08-24 09:56 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('learning_assistant', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='courseprompt', | ||
name='json_prompt_content', | ||
field=models.JSONField(null=True), | ||
), | ||
] |
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,41 @@ | ||
""" | ||
Test cases for the learning-assistant api module. | ||
""" | ||
import json | ||
from django.test import TestCase | ||
|
||
from learning_assistant.api import get_deserialized_prompt_content_by_course_id, get_setup_messages | ||
from learning_assistant.models import CoursePrompt | ||
|
||
|
||
class LearningAssistantAPITests(TestCase): | ||
""" | ||
Test suite for the api module | ||
""" | ||
|
||
def setUp(self): | ||
self.course_id = 'course-v1:edx+test+23' | ||
self.prompt = json.dumps('["This is a Prompt", "This is another Prompt"]') | ||
self.course_prompt = CoursePrompt.objects.create( | ||
course_id=self.course_id, | ||
json_prompt_content=self.prompt, | ||
) | ||
return super().setUp() | ||
|
||
def test_get_deserialized_prompt_valid_course_id(self): | ||
prompt_content = get_deserialized_prompt_content_by_course_id(self.course_id) | ||
expected_content = json.loads(self.prompt) | ||
self.assertEqual(prompt_content, expected_content) | ||
|
||
def test_get_deserialized_prompt_invalid_course_id(self): | ||
prompt_content = get_deserialized_prompt_content_by_course_id('course-v1:edx+fake+19') | ||
self.assertIsNone(prompt_content) | ||
|
||
def test_get_setup_messages(self): | ||
setup_messages = get_setup_messages(self.course_id) | ||
expected_messages = [{'role': 'system', 'content': x} for x in json.loads(self.prompt)] | ||
self.assertEqual(setup_messages, expected_messages) | ||
|
||
def test_get_setup_messages_invalid_course_id(self): | ||
setup_messages = get_setup_messages('course-v1:edx+fake+19') | ||
self.assertIsNone(setup_messages) |
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