-
Notifications
You must be signed in to change notification settings - Fork 37
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
Update file expiry with RAG chat #435
Merged
rachaelcodes
merged 4 commits into
main
from
feature/REDBOX-234-update-file-expiry-on-rag
May 23, 2024
+75
−21
Merged
Changes from all commits
Commits
Show all changes
4 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
26 changes: 26 additions & 0 deletions
26
django_app/redbox_app/redbox_core/migrations/0010_rename_expiry_date_file_last_referenced.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,26 @@ | ||
# Generated by Django 5.0.6 on 2024-05-22 14:07 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def reset_last_referenced(apps, schema_editor): | ||
# Required as we're moving from expiry date, which is 30 days further forward | ||
File = apps.get_model("redbox_core", "File") | ||
for file in File.objects.all(): | ||
file.last_referenced = file.created_at | ||
file.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("redbox_core", "0009_file_expiry_date"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="file", | ||
old_name="expiry_date", | ||
new_name="last_referenced", | ||
), | ||
migrations.RunPython(reset_last_referenced, migrations.RunPython.noop) | ||
] |
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 |
---|---|---|
@@ -1,21 +1,30 @@ | ||
from datetime import UTC, datetime, timedelta | ||
from datetime import datetime, timedelta, timezone | ||
|
||
import pytest | ||
from django.conf import settings | ||
from redbox_app.redbox_core.models import File | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from redbox_app.redbox_core.models import File, ProcessingStatusEnum | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_file_model_expiry_date(uploaded_file: File): | ||
# Tests the initial value of the expiry_date | ||
expected_expiry_date = uploaded_file.created_at + timedelta(seconds=settings.FILE_EXPIRY_IN_SECONDS) | ||
# The expiry_date should be FILE_EXPIRY_IN_SECONDS ahead of created_at | ||
def test_file_model_last_referenced(peter_rabbit, s3_client): # noqa: ARG001 | ||
mock_file = SimpleUploadedFile("test.txt", b"these are the file contents") | ||
|
||
new_file = File.objects.create( | ||
processing_status=ProcessingStatusEnum.uploaded, | ||
original_file=mock_file, | ||
user=peter_rabbit, | ||
original_file_name="test.txt", | ||
) | ||
|
||
# Tests the initial value of the last_referenced | ||
expected_last_referenced = new_file.created_at | ||
# The last_referenced should be FILE_EXPIRY_IN_SECONDS ahead of created_at | ||
# these fields are set during the same save() process | ||
# this test accounts for a delay between creating the fields | ||
assert abs(uploaded_file.expiry_date - expected_expiry_date) < timedelta(seconds=1) | ||
assert abs(new_file.last_referenced - expected_last_referenced) < timedelta(seconds=1) | ||
|
||
# Tests that the expiry_date can be updated | ||
new_date = datetime(2028, 1, 1, tzinfo=UTC) | ||
uploaded_file.expiry_date = new_date | ||
uploaded_file.save() | ||
assert uploaded_file.expiry_date == new_date | ||
# Tests that the last_referenced field can be updated | ||
new_date = datetime(2028, 1, 1, tzinfo=timezone.utc) | ||
new_file.last_referenced = new_date | ||
new_file.save() | ||
assert new_file.last_referenced == new_date |
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 |
---|---|---|
|
@@ -244,16 +244,22 @@ def test_post_message_to_new_session(alice: User, client: Client, requests_mock: | |
|
||
|
||
@pytest.mark.django_db | ||
def test_post_message_to_existing_session(chat_history: ChatHistory, client: Client, requests_mock: Mocker): | ||
def test_post_message_to_existing_session( | ||
chat_history: ChatHistory, client: Client, requests_mock: Mocker, uploaded_file: File | ||
): | ||
# Given | ||
client.force_login(chat_history.users) | ||
session_id = chat_history.id | ||
rag_url = f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/chat/rag" | ||
requests_mock.register_uri( | ||
"POST", | ||
rag_url, | ||
json={"output_text": "Good afternoon, Mr. Amor.", "source_documents": []}, | ||
json={ | ||
"output_text": "Good afternoon, Mr. Amor.", | ||
"source_documents": [{"file_uuid": str(uploaded_file.core_file_uuid)}], | ||
}, | ||
) | ||
initial_file_expiry_date = File.objects.get(core_file_uuid=uploaded_file.core_file_uuid).expiry_date | ||
|
||
# When | ||
response = client.post("/post-message/", {"message": "Are you there?", "session-id": session_id}) | ||
|
@@ -264,6 +270,10 @@ def test_post_message_to_existing_session(chat_history: ChatHistory, client: Cli | |
assert ( | ||
ChatMessage.objects.get(chat_history__id=session_id, role=ChatRoleEnum.ai).text == "Good afternoon, Mr. Amor." | ||
) | ||
assert ( | ||
ChatMessage.objects.get(chat_history__id=session_id, role=ChatRoleEnum.ai).source_files.first() == uploaded_file | ||
) | ||
assert initial_file_expiry_date != File.objects.get(core_file_uuid=uploaded_file.core_file_uuid).expiry_date | ||
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 we make this more utilitarian by adding a last_used date, so it can be used for more than expiry? 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. Yeah, good idea :) |
||
|
||
|
||
@pytest.mark.django_db | ||
|
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 wonder if we need this part if we have the migration? (or vice versa)