-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4322 from vkWeb/embedds-model
Welcome Embeddings 🚀
- Loading branch information
Showing
12 changed files
with
231 additions
and
68 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
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 |
---|---|---|
|
@@ -4,11 +4,13 @@ | |
|
||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
from django.db import connection | ||
from django.db import Error as DBError | ||
from le_utils.constants import content_kinds | ||
from le_utils.constants import file_formats | ||
from le_utils.constants import format_presets | ||
from le_utils.constants import licenses | ||
from pgvector.django import VectorExtension | ||
|
||
from contentcuration.models import ContentNode | ||
from contentcuration.models import ContentTag | ||
|
@@ -40,11 +42,20 @@ | |
SORT_ORDER = 0 | ||
|
||
|
||
def enable_pgvector_extension(connection): | ||
""" | ||
Enables pgvector extension in postgres. | ||
""" | ||
with connection.cursor() as cursor: | ||
cursor.execute("CREATE EXTENSION IF NOT EXISTS %s;" % VectorExtension().name) | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--email', dest="email", default="[email protected]") | ||
parser.add_argument('--password', dest="password", default="a") | ||
parser.add_argument('--clean-data-state', action='store_true', default=False, help='Sets database in clean state.') | ||
|
||
def handle(self, *args, **options): | ||
# Validate email | ||
|
@@ -65,6 +76,9 @@ def handle(self, *args, **options): | |
except DBError as e: | ||
logging.error('Error creating cache table: {}'.format(str(e))) | ||
|
||
# Enable pgvector extension. | ||
enable_pgvector_extension(connection) | ||
|
||
# Run migrations | ||
call_command('migrate') | ||
|
||
|
@@ -79,67 +93,68 @@ def handle(self, *args, **options): | |
user2 = create_user("[email protected]", "b", "User", "B") | ||
user3 = create_user("[email protected]", "c", "User", "C") | ||
|
||
# Create channels | ||
|
||
channel1 = create_channel("Published Channel", DESCRIPTION, editors=[admin], bookmarkers=[user1, user2], public=True) | ||
channel2 = create_channel("Ricecooker Channel", DESCRIPTION, editors=[admin, user1], bookmarkers=[user2], viewers=[user3]) | ||
channel3 = create_channel("Empty Channel", editors=[user3], viewers=[user2]) | ||
channel4 = create_channel("Imported Channel", editors=[admin]) | ||
|
||
# Invite admin to channel 3 | ||
try: | ||
invitation, _new = Invitation.objects.get_or_create( | ||
invited=admin, | ||
sender=user3, | ||
channel=channel3, | ||
email=admin.email, | ||
) | ||
invitation.share_mode = "edit" | ||
invitation.save() | ||
except MultipleObjectsReturned: | ||
# we don't care, just continue | ||
pass | ||
|
||
# Create pool of tags | ||
tags = [] | ||
for t in TAGS: | ||
tag, _new = ContentTag.objects.get_or_create(tag_name=t, channel=channel1) | ||
|
||
# Generate file objects | ||
document_file = create_file("Sample Document", format_presets.DOCUMENT, file_formats.PDF, user=admin) | ||
video_file = create_file("Sample Video", format_presets.VIDEO_HIGH_RES, file_formats.MP4, user=admin) | ||
subtitle_file = create_file("Sample Subtitle", format_presets.VIDEO_SUBTITLE, file_formats.VTT, user=admin) | ||
audio_file = create_file("Sample Audio", format_presets.AUDIO, file_formats.MP3, user=admin) | ||
html5_file = create_file("Sample HTML", format_presets.HTML5_ZIP, file_formats.HTML5, user=admin) | ||
|
||
# Populate channel 1 with content | ||
generate_tree(channel1.main_tree, document_file, video_file, subtitle_file, audio_file, html5_file, user=admin, tags=tags) | ||
|
||
# Populate channel 2 with staged content | ||
channel2.ricecooker_version = "0.0.0" | ||
channel2.save() | ||
generate_tree(channel2.staging_tree, document_file, video_file, subtitle_file, audio_file, html5_file, user=admin, tags=tags) | ||
|
||
# Import content from channel 1 into channel 4 | ||
channel1.main_tree.children.first().copy_to(channel4.main_tree) | ||
|
||
# Get validation to be reflected in nodes properly | ||
ContentNode.objects.all().update(complete=True) | ||
call_command('mark_incomplete') | ||
|
||
# Mark this node as incomplete even though it is complete | ||
# for testing purposes | ||
node = ContentNode.objects.get(tree_id=channel1.main_tree.tree_id, title="Sample Audio") | ||
node.complete = False | ||
node.save() | ||
|
||
# Publish | ||
publish_channel(admin.id, channel1.pk) | ||
|
||
# Add nodes to clipboard in legacy way | ||
legacy_clipboard_nodes = channel1.main_tree.get_children() | ||
for legacy_node in legacy_clipboard_nodes: | ||
legacy_node.copy_to(target=user1.clipboard_tree) | ||
# Only create additional data when clean-data-state is False (i.e. default behaviour). | ||
if options["clean_data_state"] is False: | ||
# Create channels | ||
channel1 = create_channel("Published Channel", DESCRIPTION, editors=[admin], bookmarkers=[user1, user2], public=True) | ||
channel2 = create_channel("Ricecooker Channel", DESCRIPTION, editors=[admin, user1], bookmarkers=[user2], viewers=[user3]) | ||
channel3 = create_channel("Empty Channel", editors=[user3], viewers=[user2]) | ||
channel4 = create_channel("Imported Channel", editors=[admin]) | ||
|
||
# Invite admin to channel 3 | ||
try: | ||
invitation, _new = Invitation.objects.get_or_create( | ||
invited=admin, | ||
sender=user3, | ||
channel=channel3, | ||
email=admin.email, | ||
) | ||
invitation.share_mode = "edit" | ||
invitation.save() | ||
except MultipleObjectsReturned: | ||
# we don't care, just continue | ||
pass | ||
|
||
# Create pool of tags | ||
tags = [] | ||
for t in TAGS: | ||
tag, _new = ContentTag.objects.get_or_create(tag_name=t, channel=channel1) | ||
|
||
# Generate file objects | ||
document_file = create_file("Sample Document", format_presets.DOCUMENT, file_formats.PDF, user=admin) | ||
video_file = create_file("Sample Video", format_presets.VIDEO_HIGH_RES, file_formats.MP4, user=admin) | ||
subtitle_file = create_file("Sample Subtitle", format_presets.VIDEO_SUBTITLE, file_formats.VTT, user=admin) | ||
audio_file = create_file("Sample Audio", format_presets.AUDIO, file_formats.MP3, user=admin) | ||
html5_file = create_file("Sample HTML", format_presets.HTML5_ZIP, file_formats.HTML5, user=admin) | ||
|
||
# Populate channel 1 with content | ||
generate_tree(channel1.main_tree, document_file, video_file, subtitle_file, audio_file, html5_file, user=admin, tags=tags) | ||
|
||
# Populate channel 2 with staged content | ||
channel2.ricecooker_version = "0.0.0" | ||
channel2.save() | ||
generate_tree(channel2.staging_tree, document_file, video_file, subtitle_file, audio_file, html5_file, user=admin, tags=tags) | ||
|
||
# Import content from channel 1 into channel 4 | ||
channel1.main_tree.children.first().copy_to(channel4.main_tree) | ||
|
||
# Get validation to be reflected in nodes properly | ||
ContentNode.objects.all().update(complete=True) | ||
call_command('mark_incomplete') | ||
|
||
# Mark this node as incomplete even though it is complete | ||
# for testing purposes | ||
node = ContentNode.objects.get(tree_id=channel1.main_tree.tree_id, title="Sample Audio") | ||
node.complete = False | ||
node.save() | ||
|
||
# Publish | ||
publish_channel(admin.id, channel1.pk) | ||
|
||
# Add nodes to clipboard in legacy way | ||
legacy_clipboard_nodes = channel1.main_tree.get_children() | ||
for legacy_node in legacy_clipboard_nodes: | ||
legacy_node.copy_to(target=user1.clipboard_tree) | ||
|
||
print("\n\n\nSETUP DONE: Log in as admin to view data (email: {}, password: {})\n\n\n".format(email, password)) | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
contentcuration/contentcuration/migrations/0147_embeddings_embeddingscontentnode.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,36 @@ | ||
# Generated by Django 3.2.19 on 2023-11-08 09:55 | ||
import uuid | ||
|
||
import django.db.models.deletion | ||
import pgvector.django | ||
from django.conf import settings | ||
from django.db import migrations | ||
from django.db import models | ||
|
||
import contentcuration.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('contentcuration', '0146_drop_taskresult_fields'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='EmbeddingsContentNode', | ||
fields=[ | ||
('cid', models.CharField(max_length=64, primary_key=True, serialize=False)), | ||
('contentnode', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='node_cid', to='contentcuration.contentnode')), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Embeddings', | ||
fields=[ | ||
('embedding_id', contentcuration.models.UUIDField(default=uuid.uuid4, max_length=32, primary_key=True, serialize=False)), | ||
('model', models.CharField(db_index=True, max_length=64)), | ||
('embedding', pgvector.django.VectorField()), | ||
('embedded_node', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='embeddings', to='contentcuration.embeddingscontentnode')), | ||
], | ||
), | ||
] |
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
34 changes: 34 additions & 0 deletions
34
contentcuration/contentcuration/tests/custom_pytest_db_backend/base.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,34 @@ | ||
from django.db.backends.postgresql.base import DatabaseWrapper as PostgresDatabaseWrapper | ||
from django.db.backends.postgresql.creation import DatabaseCreation as PostgresDatabaseCreation | ||
|
||
|
||
class CustomDBCreationForPytest(PostgresDatabaseCreation): | ||
""" | ||
Overriding creation module to enable postgres pgvector extension before | ||
pytest runs migration. Because embeddings table rely on pgvector | ||
extension to work. | ||
""" | ||
|
||
def _create_test_db(self, verbosity, autoclobber, keepdb=False): | ||
from contentcuration.management.commands.setup import enable_pgvector_extension | ||
|
||
# Create test database and get its name. | ||
test_db_name = super()._create_test_db(verbosity, autoclobber, keepdb) | ||
|
||
# Close current nodb_cursor connection and point connection to the | ||
# newly created test database. | ||
self.connection.close() | ||
self.connection.settings_dict["NAME"] = test_db_name | ||
|
||
# Enable pgvector extension. | ||
enable_pgvector_extension(self.connection) | ||
|
||
return self.connection.settings_dict["NAME"] | ||
|
||
|
||
class DatabaseWrapper(PostgresDatabaseWrapper): | ||
""" | ||
A database wrapper to customise creation module. Rest everything is same as | ||
Postgres. | ||
""" | ||
creation_class = CustomDBCreationForPytest |
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 |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"build": "webpack --env prod --config webpack.config.js", | ||
"postgres": "pg_ctl -D /usr/local/var/[email protected] start || true", | ||
"redis": "redis-server /usr/local/etc/redis.conf || true", | ||
"devsetup": "cd contentcuration && python manage.py setup --settings=contentcuration.dev_settings", | ||
"devsetup": "python contentcuration/manage.py setup --settings=contentcuration.dev_settings", | ||
"devsetup:clean": "python contentcuration/manage.py setup --clean-data-state --settings=contentcuration.dev_settings", | ||
"services": "npm-run-all -c --parallel --silent celery minio redis postgres", | ||
"test": "jest --config jest_config/jest.conf.js", | ||
"build:dev": "webpack serve --env dev --config webpack.config.js --progress", | ||
|
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 |
---|---|---|
|
@@ -35,3 +35,4 @@ pillow==10.2.0 | |
python-dateutil>=2.8.1 | ||
jsonschema>=3.2.0 | ||
django-celery-results | ||
pgvector |
Oops, something went wrong.