Skip to content
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

[Fix] Unified LLM Whispeper adapters #1058

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 4.2.1 on 2025-01-08 21:08

import json

from cryptography.fernet import Fernet
from django.conf import settings
from django.db import migrations


def migrate_adapter_data(apps, schema_editor):
adapter_instance = apps.get_model("adapter_processor_v2", "AdapterInstance")

old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e"
new_adapter_id = "llmwhisperer|a5e6b8af-3e1f-4a80-b006-d017e8e67f93"

for adapter in adapter_instance.objects.all():

# Deserialize adapter_metadata_b if it exists
encryption_secret: str = settings.ENCRYPTION_KEY
f: Fernet = Fernet(encryption_secret.encode("utf-8"))
if adapter.adapter_metadata_b:

metadata = json.loads(
f.decrypt(bytes(adapter.adapter_metadata_b).decode("utf-8"))
)
else:
metadata = {}

# Add version in adapter_metadata_b
if "version" not in metadata:
if adapter.adapter_id == old_adapter_id:
metadata["version"] = "v1"
else:
metadata["version"] = "v2"

# Serialize and save updated metadata

adapter.adapter_metadata_b = f.encrypt(json.dumps(metadata).encode("utf-8"))

if adapter.adapter_id == old_adapter_id:
adapter.adapter_id = new_adapter_id

adapter.save()


def rollback_adapter_data(apps, schema_editor):
adapter_instance = apps.get_model("adapter_processor_v2", "AdapterInstance")

old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e"

encryption_secret: str = settings.ENCRYPTION_KEY
f: Fernet = Fernet(encryption_secret.encode("utf-8"))

for adapter in adapter_instance.objects.all():
if adapter.adapter_metadata_b:
# Deserialize adapter_metadata_b
metadata = json.loads(
f.decrypt(bytes(adapter.adapter_metadata_b).decode("utf-8"))
)

# Check if 'version' is 'v1' and revert adapter_id to old value
if metadata.get("version") == "v1":
adapter.adapter_id = old_adapter_id

# Remove 'version' key to restore original state
if "version" in metadata:
del metadata["version"]

# Serialize and save updated metadata
adapter.adapter_metadata_b = f.encrypt(json.dumps(metadata).encode("utf-8"))

adapter.save()


class Migration(migrations.Migration):

dependencies = [
("adapter_processor_v2", "0001_initial"),
]

operations = [
migrations.RunPython(migrate_adapter_data, reverse_code=rollback_adapter_data),
]
Loading