LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
Performance degradtion fix (#411)
Rahul Johny2e5f87299030
diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.pyindex 00d8d4309..ae6dbfde6 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py@@ -302,7 +302,7 @@ class PromptStudioHelper:"""tool: CustomTool = CustomTool.objects.get(pk=tool_id)if is_summary:- profile_manager = ProfileManager.objects.get(+ profile_manager: ProfileManager = ProfileManager.objects.get(prompt_studio_tool=tool, is_summarize_llm=True)default_profile = profile_managerdiff --git a/backend/prompt_studio/prompt_studio_core/urls.py b/backend/prompt_studio/prompt_studio_core/urls.pyindex 3a32f63c4..0abc1c436 100644--- a/backend/prompt_studio/prompt_studio_core/urls.py+++ b/backend/prompt_studio/prompt_studio_core/urls.py@@ -1,4 +1,6 @@+from django.db import transactionfrom django.urls import path+from django.utils.decorators import method_decoratorfrom rest_framework.urlpatterns import format_suffix_patternsfrom .views import PromptStudioCoreView@@ -77,7 +79,9 @@ urlpatterns = format_suffix_patterns(),path("prompt-studio/index-document/<uuid:pk>",- prompt_studio_prompt_index,+ method_decorator(transaction.non_atomic_requests)(+ prompt_studio_prompt_index+ ),name="prompt-studio-prompt-index",),path(diff --git a/backend/prompt_studio/prompt_studio_core/views.py b/backend/prompt_studio/prompt_studio_core/views.pyindex 235e266b4..9093efd42 100644--- a/backend/prompt_studio/prompt_studio_core/views.py+++ b/backend/prompt_studio/prompt_studio_core/views.py@@ -189,7 +189,7 @@ class PromptStudioCoreView(viewsets.ModelViewSet):data={"default_profile": profile_manager.profile_id},)- @action(detail=True, methods=["get"])+ @action(detail=True, methods=["post"])def index_document(self, request: HttpRequest, pk: Any = None) -> Response:"""API Entry point method to index input file.diff --git a/backend/prompt_studio/prompt_studio_index_manager/prompt_studio_index_helper.py b/backend/prompt_studio/prompt_studio_index_manager/prompt_studio_index_helper.pyindex 69fd5ba5b..49cae9141 100644--- a/backend/prompt_studio/prompt_studio_index_manager/prompt_studio_index_helper.py+++ b/backend/prompt_studio/prompt_studio_index_manager/prompt_studio_index_helper.py@@ -1,6 +1,7 @@import jsonimport logging+from django.db import transactionfrom prompt_studio.prompt_profile_manager.models import ProfileManagerfrom prompt_studio.prompt_studio_core.exceptions import IndexingAPIErrorfrom prompt_studio.prompt_studio_document_manager.models import DocumentManager@@ -18,46 +19,48 @@ class PromptStudioIndexHelper:profile_manager: ProfileManager,doc_id: str,) -> IndexManager:- document: DocumentManager = DocumentManager.objects.get(pk=document_id)+ try:- index_id = "raw_index_id"- if is_summary:- index_id = "summarize_index_id"+ with transaction.atomic():- args: dict[str, str] = dict()- args["document_manager"] = document- args["profile_manager"] = profile_manager+ document: DocumentManager = DocumentManager.objects.get(pk=document_id)- try:- # Create or get the existing record for this document and- # profile combo- index_manager, success = IndexManager.objects.get_or_create(**args)-- if success:- logger.info(- f"Index manager doc_id: {doc_id} for "- f"profile {profile_manager.profile_id} created"- )- else:- logger.info(- f"Index manager doc_id: {doc_id} for "- f"profile {profile_manager.profile_id} updated"- )-- index_ids = index_manager.index_ids_history- index_ids_list = json.loads(index_ids) if index_ids else []- if doc_id not in index_ids:- index_ids_list.append(doc_id)-- args[index_id] = doc_id- args["index_ids_history"] = json.dumps(index_ids_list)-- # Update the record with the index id- result: IndexManager = IndexManager.objects.filter(- index_manager_id=index_manager.index_manager_id- ).update(**args)+ index_id = "raw_index_id"+ if is_summary:+ index_id = "summarize_index_id"++ args: dict[str, str] = dict()+ args["document_manager"] = document+ args["profile_manager"] = profile_manager++ # Create or get the existing record for this document and+ # profile combo+ index_manager, success = IndexManager.objects.get_or_create(**args)+ if success:+ logger.info(+ f"Index manager doc_id: {doc_id} for "+ f"profile {profile_manager.profile_id} created"+ )+ else:+ logger.info(+ f"Index manager doc_id: {doc_id} for "+ f"profile {profile_manager.profile_id} updated"+ )++ index_ids = index_manager.index_ids_history+ index_ids_list = json.loads(index_ids) if index_ids else []+ if doc_id not in index_ids:+ index_ids_list.append(doc_id)++ args[index_id] = doc_id+ args["index_ids_history"] = json.dumps(index_ids_list)++ # Update the record with the index id+ result: IndexManager = IndexManager.objects.filter(+ index_manager_id=index_manager.index_manager_id+ ).update(**args)+ return resultexcept Exception as e:+ transaction.rollback()raise IndexingAPIError("Error updating indexing status") from e-- return resultdiff --git a/backend/tenant_account/migrations/0001_initial.py b/backend/tenant_account/migrations/0001_initial.pyindex cad92899c..9cf3e1e69 100644--- a/backend/tenant_account/migrations/0001_initial.py+++ b/backend/tenant_account/migrations/0001_initial.py@@ -14,8 +14,9 @@ class Migration(migrations.Migration):]operations = [+ # Updated the name here as the 002, 0002 step is just name changemigrations.CreateModel(- name="User",+ name="OrganizationMember",fields=[("user_ptr",@@ -28,6 +29,8 @@ class Migration(migrations.Migration):to=settings.AUTH_USER_MODEL,),),+ # Added column which is used in 0002 here+ ("role", models.CharField(default="admin")),],options={"verbose_name": "user",diff --git a/backend/tenant_account/migrations/0002_organizationmember_delete_user.py b/backend/tenant_account/migrations/0002_organizationmember_delete_user.pyindex 6a41c4aa8..68b4a63a2 100644--- a/backend/tenant_account/migrations/0002_organizationmember_delete_user.py+++ b/backend/tenant_account/migrations/0002_organizationmember_delete_user.py@@ -1,9 +1,6 @@# Generated by Django 4.2.1 on 2023-08-21 11:12-import django.contrib.auth.models-import django.db.models.deletion-from django.conf import settings-from django.db import migrations, models+from django.db import migrationsclass Migration(migrations.Migration):@@ -13,33 +10,36 @@ class Migration(migrations.Migration):]operations = [- migrations.CreateModel(- name="OrganizationMember",- fields=[- (- "user_ptr",- models.OneToOneField(- auto_created=True,- on_delete=django.db.models.deletion.CASCADE,- parent_link=True,- primary_key=True,- serialize=False,- to=settings.AUTH_USER_MODEL,- ),- ),- ("role", models.CharField(default="admin")),- ],- options={- "verbose_name": "user",- "verbose_name_plural": "users",- "abstract": False,- },- bases=("account.user",),- managers=[- ("objects", django.contrib.auth.models.UserManager()),- ],- ),- migrations.DeleteModel(- name="User",- ),+ # # Commenting out here as this is taken care in 0001+ # migrations.CreateModel(+ # name="OrganizationMember",+ # fields=[+ # (+ # "user_ptr",+ # models.OneToOneField(+ # auto_created=True,+ # on_delete=django.db.models.deletion.CASCADE,+ # parent_link=True,+ # primary_key=True,+ # serialize=False,+ # to=settings.AUTH_USER_MODEL,+ # ),+ # ),+ # ("role", models.CharField(default="admin")),+ # ],+ # options={+ # "verbose_name": "user",+ # "verbose_name_plural": "users",+ # "abstract": False,+ # },+ # bases=("account.user",),+ # managers=[+ # ("objects", django.contrib.auth.models.UserManager()),+ # ],+ # ),+ # # https://www.geeksforgeeks.org/what-is-access-exclusive-lock-mode-in-postgreysql/+ # # commenting drop table to ignore AccesExclusive Lock+ # migrations.DeleteModel(+ # name="User",+ # ),]