LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-2427 [FEAT]: Dynamically override LLM Profile in API Deployment invocation (#1388)
vishnuszipstack0f26d7572ca3
diff --git a/backend/api_v2/api_deployment_views.py b/backend/api_v2/api_deployment_views.pyindex 4f42abeff..61f4e4ee9 100644--- a/backend/api_v2/api_deployment_views.py+++ b/backend/api_v2/api_deployment_views.py@@ -45,9 +45,16 @@ class DeploymentExecution(views.APIView):@DeploymentHelper.validate_api_keydef post(- self, request: Request, org_name: str, api_name: str, api: APIDeployment+ self,+ request: Request,+ org_name: str,+ api_name: str,+ api: APIDeployment,+ api_key: str,) -> Response:- serializer = ExecutionRequestSerializer(data=request.data)+ serializer = ExecutionRequestSerializer(+ data=request.data, context={"api": api, "api_key": api_key}+ )serializer.is_valid(raise_exception=True)file_objs = serializer.validated_data.get(ApiExecution.FILES_FORM_DATA)timeout = serializer.validated_data.get(ApiExecution.TIMEOUT_FORM_DATA)@@ -55,6 +62,8 @@ class DeploymentExecution(views.APIView):include_metrics = serializer.validated_data.get(ApiExecution.INCLUDE_METRICS)use_file_history = serializer.validated_data.get(ApiExecution.USE_FILE_HISTORY)tag_names = serializer.validated_data.get(ApiExecution.TAGS)+ llm_profile_id = serializer.validated_data.get(ApiExecution.LLM_PROFILE_ID)+response = DeploymentHelper.execute_workflow(organization_name=org_name,api=api,@@ -64,6 +73,7 @@ class DeploymentExecution(views.APIView):include_metrics=include_metrics,use_file_history=use_file_history,tag_names=tag_names,+ llm_profile_id=llm_profile_id,)if "error" in response and response["error"]:return Response(diff --git a/backend/api_v2/constants.py b/backend/api_v2/constants.pyindex ec7dfd0ea..6e2017816 100644--- a/backend/api_v2/constants.py+++ b/backend/api_v2/constants.py@@ -8,3 +8,4 @@ class ApiExecution:USE_FILE_HISTORY: str = "use_file_history" # Undocumented parameterEXECUTION_ID: str = "execution_id"TAGS: str = "tags"+ LLM_PROFILE_ID: str = "llm_profile_id"diff --git a/backend/api_v2/deployment_helper.py b/backend/api_v2/deployment_helper.pyindex df89d5bd7..3851ca153 100644--- a/backend/api_v2/deployment_helper.py+++ b/backend/api_v2/deployment_helper.py@@ -53,6 +53,7 @@ class DeploymentHelper(BaseAPIKeyValidator):api_deployment = DeploymentHelper.get_deployment_by_api_name(api_name=api_name)DeploymentHelper.validate_api(api_deployment=api_deployment, api_key=api_key)kwargs["api"] = api_deployment+ kwargs["api_key"] = api_keyreturn func(self, request, *args, **kwargs)@staticmethod@@ -143,6 +144,7 @@ class DeploymentHelper(BaseAPIKeyValidator):include_metrics: bool = False,use_file_history: bool = False,tag_names: list[str] = [],+ llm_profile_id: str | None = None,) -> ReturnDict:"""Execute workflow by api.@@ -153,6 +155,7 @@ class DeploymentHelper(BaseAPIKeyValidator):use_file_history (bool): Use FileHistory table to return results on alreadyprocessed files. Defaults to Falsetag_names (list(str)): list of tag names+ llm_profile_id (str, optional): LLM profile ID for overriding tool settingsReturns:ReturnDict: execution status/ result@@ -185,6 +188,7 @@ class DeploymentHelper(BaseAPIKeyValidator):execution_id=execution_id,queue=CeleryQueue.CELERY_API_DEPLOYMENTS,use_file_history=use_file_history,+ llm_profile_id=llm_profile_id,)result.status_api = DeploymentHelper.construct_status_endpoint(api_endpoint=api.api_endpoint, execution_id=execution_iddiff --git a/backend/api_v2/postman_collection/dto.py b/backend/api_v2/postman_collection/dto.pyindex 4d67b3738..263bb15b5 100644--- a/backend/api_v2/postman_collection/dto.py+++ b/backend/api_v2/postman_collection/dto.py@@ -119,6 +119,12 @@ class APIDeploymentDto(APIBase):),FormDataItem(key=ApiExecution.INCLUDE_METADATA, type="text", value="False"),FormDataItem(key=ApiExecution.INCLUDE_METRICS, type="text", value="False"),+ FormDataItem(+ key=ApiExecution.LLM_PROFILE_ID,+ type="text",+ value="",+ description="Optional: UUID of the LLM profile to override default settings",+ ),]def get_api_key(self) -> str:diff --git a/backend/api_v2/serializers.py b/backend/api_v2/serializers.pyindex 9c8034da8..e01c29258 100644--- a/backend/api_v2/serializers.py+++ b/backend/api_v2/serializers.py@@ -4,6 +4,7 @@ from typing import Anyfrom django.core.validators import RegexValidatorfrom pipeline_v2.models import Pipeline+from prompt_studio.prompt_profile_manager_v2.models import ProfileManagerfrom rest_framework.serializers import (BooleanField,CharField,@@ -114,6 +115,8 @@ class ExecutionRequestSerializer(TagParamsSerializer):helpful for demos.tags (str): Comma-separated List of tags to associate with the execution.e.g:'tag1,tag2-name,tag3_name'+ llm_profile_id (str): UUID of the LLM profile to override the default profile.+ If not provided, the default profile will be used."""MAX_FILES_ALLOWED = 32@@ -124,6 +127,7 @@ class ExecutionRequestSerializer(TagParamsSerializer):include_metadata = BooleanField(default=False)include_metrics = BooleanField(default=False)use_file_history = BooleanField(default=False)+ llm_profile_id = CharField(required=False, allow_null=True, allow_blank=True)files = ListField(child=FileField(),required=True,@@ -142,6 +146,36 @@ class ExecutionRequestSerializer(TagParamsSerializer):raise ValidationError(f"Maximum '{self.MAX_FILES_ALLOWED}' files allowed.")return value+ def validate_llm_profile_id(self, value):+ """Validate that the llm_profile_id belongs to the API key owner."""+ if not value:+ return value++ # Get context from serializer+ api = self.context.get("api")+ api_key = self.context.get("api_key")++ if not api or not api_key:+ raise ValidationError("Unable to validate LLM profile ownership")++ # Check if profile exists+ try:+ profile = ProfileManager.objects.get(profile_id=value)+ except ProfileManager.DoesNotExist:+ raise ValidationError("Profile not found")++ # Get the specific API key being used+ try:+ active_api_key = api.api_keys.get(api_key=api_key, is_active=True)+ except api.api_keys.model.DoesNotExist:+ raise ValidationError("API key not found or not active for this deployment")++ # Check if the profile owner matches the API key owner+ if profile.created_by != active_api_key.created_by:+ raise ValidationError("You can only use profiles that you own")++ return value+class ExecutionQuerySerializer(Serializer):execution_id = CharField(required=True)diff --git a/backend/pyproject.toml b/backend/pyproject.tomlindex 00a8fda76..6a609cd30 100644--- a/backend/pyproject.toml+++ b/backend/pyproject.toml@@ -37,7 +37,7 @@ dependencies = ["social-auth-core==4.4.2", # For OAuth# TODO: Temporarily removing the extra dependencies of aws and gcs from unstract-sdk# to resolve lock file. Will have to be re-looked into- "unstract-sdk[azure]~=0.73.1",+ "unstract-sdk[azure]~=0.74.0","gcsfs==2024.10.0","s3fs==2024.10.0","azure-identity==1.16.0",diff --git a/backend/uv.lock b/backend/uv.lockindex 1b41d55ec..ecb115257 100644--- a/backend/uv.lock+++ b/backend/uv.lock@@ -3967,7 +3967,7 @@ requires-dist = [{ name = "unstract-core", editable = "../unstract/core" },{ name = "unstract-filesystem", editable = "../unstract/filesystem" },{ name = "unstract-flags", editable = "../unstract/flags" },- { name = "unstract-sdk", extras = ["azure"], specifier = "~=0.73.1" },+ { name = "unstract-sdk", extras = ["azure"], specifier = "~=0.74.0" },{ name = "unstract-tool-registry", editable = "../unstract/tool-registry" },{ name = "unstract-tool-sandbox", editable = "../unstract/tool-sandbox" },{ name = "unstract-workflow-execution", editable = "../unstract/workflow-execution" },@@ -4073,7 +4073,7 @@ dependencies = [][package.metadata]-requires-dist = [{ name = "unstract-sdk", specifier = "~=0.73.1" }]+requires-dist = [{ name = "unstract-sdk", specifier = "~=0.74.0" }][[package]]name = "unstract-flags"@@ -4092,7 +4092,7 @@ requires-dist = [[[package]]name = "unstract-sdk"-version = "0.73.1"+version = "0.74.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "filetype" },@@ -4131,9 +4131,9 @@ dependencies = [{ name = "tiktoken" },{ name = "transformers" },]-sdist = { url = "https://files.pythonhosted.org/packages/88/bb/60a8cc6d352ca204e04bf33a85d9b9961415833e0f314b2cbbcd42c5a631/unstract_sdk-0.73.1.tar.gz", hash = "sha256:294d0206bc43c09bc8c884a77ab78794301de0dab3ff6a1fe68f9c5235f72251", size = 2376089 }+sdist = { url = "https://files.pythonhosted.org/packages/74/b7/8d4db4b8a334ef1969cf56074e595922a4814e8252d525cf9223772ed01b/unstract_sdk-0.74.0.tar.gz", hash = "sha256:adb2e320dfedf2c52d4fe9fa29cfb6c64deb7869712cd6c344b0e15062606def", size = 2376223 }wheels = [- { url = "https://files.pythonhosted.org/packages/d8/38/4696fe06d95fd62153dd7a270332e3bfc76a4ffc7e57c0b9135459666317/unstract_sdk-0.73.1-py3-none-any.whl", hash = "sha256:527c4904a8a4500db5ebba0ee3db216069d8093d064187c1e09eecde42402ef9", size = 266247 },+ { url = "https://files.pythonhosted.org/packages/a4/2f/862cef00c35ef9dc2d546e1b82e498a5b4750bfc6d55c1217123fa7fc56a/unstract_sdk-0.74.0-py3-none-any.whl", hash = "sha256:209a85c002e8ee049a3fdd1ffffaac6848b8670222c9b272593ee4002f519aef", size = 266399 },][package.optional-dependencies]@@ -4160,7 +4160,7 @@ requires-dist = [{ name = "jsonschema", specifier = "~=4.18.2" },{ name = "pyyaml", specifier = "~=6.0.1" },{ name = "unstract-flags", editable = "../unstract/flags" },- { name = "unstract-sdk", specifier = "~=0.73.1" },+ { name = "unstract-sdk", specifier = "~=0.74.0" },{ name = "unstract-tool-sandbox", editable = "../unstract/tool-sandbox" },]diff --git a/backend/workflow_manager/endpoint_v2/source.py b/backend/workflow_manager/endpoint_v2/source.pyindex d29ad039b..36542a79c 100644--- a/backend/workflow_manager/endpoint_v2/source.py+++ b/backend/workflow_manager/endpoint_v2/source.py@@ -810,6 +810,7 @@ class SourceConnector(BaseConnector):workflow_file_execution: WorkflowFileExecution,tags: list[str],file_hash: FileHash,+ llm_profile_id: str | None = None,) -> str:"""Add input file to execution directory.@@ -817,6 +818,7 @@ class SourceConnector(BaseConnector):workflow_file_execution: WorkflowFileExecution modeltags (list[str]): Tag names associated with the workflow execution.file_hash: FileHash model+ llm_profile_id (str, optional): LLM profile ID for overriding tool settings.Raises:InvalidSource: _description_@@ -849,6 +851,7 @@ class SourceConnector(BaseConnector):file_execution_id=workflow_file_execution.id,source_hash=file_content_hash,tags=tags,+ llm_profile_id=llm_profile_id,)return file_content_hashdiff --git a/backend/workflow_manager/workflow_v2/dto.py b/backend/workflow_manager/workflow_v2/dto.pyindex f6e0a0f1a..1ac2268ac 100644--- a/backend/workflow_manager/workflow_v2/dto.py+++ b/backend/workflow_manager/workflow_v2/dto.py@@ -155,6 +155,7 @@ class FileData:execution_mode: struse_file_history: boolq_file_no_list: list[int]+ llm_profile_id: str | None = None@classmethoddef from_dict(cls, data: dict[str, Any]) -> FileData:diff --git a/backend/workflow_manager/workflow_v2/file_execution_tasks.py b/backend/workflow_manager/workflow_v2/file_execution_tasks.pyindex 3d8d450bc..85616bc52 100644--- a/backend/workflow_manager/workflow_v2/file_execution_tasks.py+++ b/backend/workflow_manager/workflow_v2/file_execution_tasks.py@@ -433,6 +433,7 @@ class FileExecutionTasks:workflow_file_execution,file_hash,workflow_execution,+ file_data,)# Update file execution tracker with updated file hash@@ -671,6 +672,7 @@ class FileExecutionTasks:workflow_file_exec: WorkflowFileExecution,file_hash: FileHash,workflow_execution: WorkflowExecution,+ file_data: FileData,) -> str:"""Handle file preparation and volume storage."""workflow_file_exec.update_status(ExecutionStatus.EXECUTING)@@ -687,6 +689,7 @@ class FileExecutionTasks:workflow_file_execution=workflow_file_exec,tags=workflow_execution.tag_names,file_hash=file_hash,+ llm_profile_id=file_data.llm_profile_id,)file_hash.file_hash = content_hashworkflow_file_exec.update(file_hash=content_hash)diff --git a/backend/workflow_manager/workflow_v2/workflow_helper.py b/backend/workflow_manager/workflow_v2/workflow_helper.pyindex 118b986d1..a2ac15a20 100644--- a/backend/workflow_manager/workflow_v2/workflow_helper.py+++ b/backend/workflow_manager/workflow_v2/workflow_helper.py@@ -146,6 +146,7 @@ class WorkflowHelper:scheduled: bool,execution_mode: tuple[str, str],use_file_history: bool,+ llm_profile_id: str | None,) -> str | None:total_files = len(input_files)workflow_log.publish_initial_workflow_logs(total_files=total_files)@@ -196,6 +197,7 @@ class WorkflowHelper:execution_mode=mode,use_file_history=use_file_history,q_file_no_list=list(q_file_no_list) if q_file_no_list else [],+ llm_profile_id=llm_profile_id,)batch_data = FileBatchData(files=batch, file_data=file_data)@@ -255,6 +257,7 @@ class WorkflowHelper:single_step: bool = False,execution_mode: tuple[str, str] | None = None,use_file_history: bool = True,+ llm_profile_id: str | None = None,) -> ExecutionResponse:tool_instances: list[ToolInstance] = (ToolInstanceHelper.get_tool_instances_by_workflow(@@ -304,6 +307,7 @@ class WorkflowHelper:scheduled=scheduled,use_file_history=use_file_history,execution_mode=execution_mode,+ llm_profile_id=llm_profile_id,)api_results = []return ExecutionResponse(@@ -424,6 +428,7 @@ class WorkflowHelper:pipeline_id: str | None = None,queue: str | None = None,use_file_history: bool = True,+ llm_profile_id: str | None = None,) -> ExecutionResponse:"""Adding a workflow to the queue for execution.@@ -435,6 +440,7 @@ class WorkflowHelper:queue (Optional[str]): Name of the celery queue to push intouse_file_history (bool): Use FileHistory table to return results on alreadyprocessed files. Defaults to True+ llm_profile_id (str, optional): LLM profile ID for overriding tool settingsReturns:ExecutionResponse: Existing status of execution@@ -459,6 +465,7 @@ class WorkflowHelper:"pipeline_id": pipeline_id,"log_events_id": log_events_id,"use_file_history": use_file_history,+ "llm_profile_id": llm_profile_id,},queue=queue,)@@ -625,6 +632,8 @@ class WorkflowHelper:workflow = Workflow.objects.get(id=workflow_id)# TODO: Make use of WorkflowExecution.get_or_create()try:+ # Filter out llm_profile_id from kwargs as create_workflow_execution doesn't accept it+ filtered_kwargs = {k: v for k, v in kwargs.items() if k != "llm_profile_id"}workflow_execution = WorkflowExecutionServiceHelper.create_workflow_execution(workflow_id=workflow_id,single_step=False,@@ -632,7 +641,7 @@ class WorkflowHelper:mode=WorkflowExecution.Mode.QUEUE,execution_id=execution_id,total_files=len(hash_values),- **kwargs, # type: ignore+ **filtered_kwargs, # type: ignore)except IntegrityError:# Use existing instance on retry attempt@@ -650,6 +659,7 @@ class WorkflowHelper:execution_mode=execution_mode,hash_values_of_files=hash_values,use_file_history=use_file_history,+ llm_profile_id=kwargs.get("llm_profile_id"),)except Exception as error:error_message = traceback.format_exc()diff --git a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.css b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.cssindex fcd61731d..10423ea86 100644--- a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.css+++ b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.css@@ -3,3 +3,30 @@.manage-llm-pro-icon {font-size: 10px;}++.action-buttons-container {+ display: flex;+ align-items: center;+ gap: 8px;+}++.profile-id-container {+ display: flex;+ align-items: center;+ gap: 8px;+ margin-top: 4px;+}++.profile-id-text {+ font-size: 12px;+}++.profile-copy-button {+ padding: 0;+ min-width: auto;+ height: auto;+}++.profile-copy-icon {+ font-size: 12px;+}diff --git a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsxindex ebdd3dfcd..bb831cda2 100644--- a/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx+++ b/frontend/src/components/custom-tools/manage-llm-profiles/ManageLlmProfiles.jsx@@ -1,4 +1,4 @@-import { DeleteOutlined, EditOutlined } from "@ant-design/icons";+import { DeleteOutlined, EditOutlined, CopyOutlined } from "@ant-design/icons";import { Button, Radio, Table, Tooltip, Typography } from "antd";import { useEffect, useState } from "react";@@ -41,16 +41,10 @@ const columns = [key: "text_extractor",},{- title: "",- dataIndex: "delete",- key: "delete",- width: 30,- },- {- title: "",- dataIndex: "edit",- key: "edit",- width: 30,+ title: "Actions",+ dataIndex: "actions",+ key: "actions",+ width: 120,},{title: "Select Default",@@ -122,43 +116,62 @@ function ManageLlmProfiles() {const modifiedRows = llmProfiles.map((item) => {return {key: item?.profile_id,- name: item?.profile_name || "",+ name: (+ <div>+ <div>{item?.profile_name || ""}</div>+ <div className="profile-id-container">+ <Typography.Text type="secondary" className="profile-id-text">+ {item?.profile_id}+ </Typography.Text>+ <Tooltip title="Copy Profile ID">+ <Button+ size="small"+ type="text"+ className="profile-copy-button"+ onClick={() => copyProfileId(item?.profile_id)}+ >+ <CopyOutlined className="profile-copy-icon" />+ </Button>+ </Tooltip>+ </div>+ </div>+ ),llm: item?.llm || "",embedding_model: item?.embedding_model || "",vector_db: item?.vector_store || "",text_extractor: item?.x2text || "",- delete: (- <ConfirmModal- handleConfirm={() => handleDelete(item?.profile_id)}- content="The LLM profile will be permanently deleted."- >- <Tooltip- title={- defaultLlmProfile === item?.profile_id &&- "Default profile cannot be deleted"- }+ actions: (+ <div className="action-buttons-container">+ <Button+ size="small"+ className="display-flex-align-center"+ disabled={isPublicSource}+ onClick={() => handleEdit(item?.profile_id)}+ >+ <EditOutlined classID="manage-llm-pro-icon" />+ </Button>+ <ConfirmModal+ handleConfirm={() => handleDelete(item?.profile_id)}+ content="The LLM profile will be permanently deleted.">- <Button- size="small"- className="display-flex-align-center"- disabled={- isPublicSource || defaultLlmProfile === item?.profile_id+ <Tooltip+ title={+ defaultLlmProfile === item?.profile_id &&+ "Default profile cannot be deleted"}>- <DeleteOutlined classID="manage-llm-pro-icon" />- </Button>- </Tooltip>- </ConfirmModal>- ),- edit: (- <Button- size="small"- className="display-flex-align-center"- disabled={isPublicSource}- onClick={() => handleEdit(item?.profile_id)}- >- <EditOutlined classID="manage-llm-pro-icon" />- </Button>+ <Button+ size="small"+ className="display-flex-align-center"+ disabled={+ isPublicSource || defaultLlmProfile === item?.profile_id+ }+ >+ <DeleteOutlined classID="manage-llm-pro-icon" />+ </Button>+ </Tooltip>+ </ConfirmModal>+ </div>),select: (<Radio@@ -184,15 +197,23 @@ function ManageLlmProfiles() {}};- const handleEdit = (id) => {- setEditLlmProfileId(id);+ const handleEdit = (profileId) => {+ setEditLlmProfileId(profileId);setIsAddLlm(true);};- const handleDelete = (id) => {+ const copyProfileId = (profileId) => {+ navigator.clipboard.writeText(profileId);+ setAlertDetails({+ type: "success",+ content: "Profile ID copied to clipboard",+ });+ };++ const handleDelete = (profileId) => {const requestOptions = {method: "DELETE",- url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/profile-manager/${id}/`,+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/profile-manager/${profileId}/`,headers: {"X-CSRFToken": sessionDetails?.csrfToken,},@@ -201,14 +222,14 @@ function ManageLlmProfiles() {axiosPrivate(requestOptions).then(() => {const modifiedLlmProfiles = [...llmProfiles].filter(- (item) => item?.profile_id !== id+ (item) => item?.profile_id !== profileId);const body = {llmProfiles: modifiedLlmProfiles,};// Reset the default LLM profile if it got deleted.- if (id === defaultLlmProfile) {+ if (profileId === defaultLlmProfile) {body["defaultLlmProfile"] = "";}diff --git a/platform-service/pyproject.toml b/platform-service/pyproject.tomlindex a3ccfb3b2..1ab607797 100644--- a/platform-service/pyproject.toml+++ b/platform-service/pyproject.toml@@ -15,7 +15,7 @@ dependencies = ["redis~=5.2.1","cryptography>=41.0.7","requests>=2.31.0",- "unstract-sdk[gcs, azure, aws]~=0.73.1", # Add version+ "unstract-sdk[gcs, azure, aws]~=0.74.0", # Add version"unstract-flags","unstract-core[flask]",]diff --git a/platform-service/src/unstract/platform_service/constants.py b/platform-service/src/unstract/platform_service/constants.pyindex 89f016644..f5ed0aa7e 100644--- a/platform-service/src/unstract/platform_service/constants.py+++ b/platform-service/src/unstract/platform_service/constants.py@@ -9,6 +9,7 @@ class DBTable:PAGE_USAGE = "page_usage"SUBSCRIPTION_USAGE = "subscription_usage"SUBSCRIPTION = "subscription"+ LLM_PROFILE_MANAGER = "profile_manager"class LogLevel:diff --git a/platform-service/src/unstract/platform_service/controller/platform.py b/platform-service/src/unstract/platform_service/controller/platform.pyindex fb6514edb..049989997 100644--- a/platform-service/src/unstract/platform_service/controller/platform.py+++ b/platform-service/src/unstract/platform_service/controller/platform.py@@ -528,3 +528,31 @@ def custom_tool_instance() -> Any:f"{prompt_registry_id}: {e}")raise APIError(message=msg) from e+++@platform_bp.route(+ "/llm_profile_instance",+ methods=["GET"],+ endpoint="llm_profile_instance",+)+@authentication_middleware+def llm_profile_instance() -> Any:+ """Fetching llm profile instance."""+ bearer_token = get_token_from_auth_header(request)+ _, organization_id = get_organization_from_bearer_token(bearer_token)+ if not organization_id:+ return Env.INVALID_ORGANIZATOIN, 403+ llm_profile_id = request.args.get("llm_profile_id")+ if not llm_profile_id:+ raise APIError(message="llm_profile_id is required", code=400)+ try:+ data_dict = PromptStudioRequestHelper.get_llm_profile_instance_from_db(+ organization_id=organization_id,+ llm_profile_id=llm_profile_id,+ )+ return jsonify(data_dict)+ except Exception as e:+ if isinstance(e, APIError):+ raise e+ msg = f"Error while getting data for LLM profile {llm_profile_id}: {e}"+ raise APIError(message=msg) from ediff --git a/platform-service/src/unstract/platform_service/helper/prompt_studio.py b/platform-service/src/unstract/platform_service/helper/prompt_studio.pyindex bf31d80b0..a7e96a866 100644--- a/platform-service/src/unstract/platform_service/helper/prompt_studio.py+++ b/platform-service/src/unstract/platform_service/helper/prompt_studio.py@@ -40,3 +40,34 @@ class PromptStudioRequestHelper:data_dict: dict[str, Any] = dict(zip(columns, result_row, strict=False))cursor.close()return data_dict++ @staticmethod+ def get_llm_profile_instance_from_db(+ organization_id: str,+ llm_profile_id: str,+ ) -> dict[str, Any]:+ """Get llm profile instance from Backend Database.++ Args:+ organization_id (str): organization schema id+ llm_profile_id (str): llm_profile_id++ Returns:+ _type_: _description_+ """+ query = (+ "SELECT * FROM "+ f'"{DB_SCHEMA}".{DBTable.LLM_PROFILE_MANAGER} x '+ f"WHERE profile_id='{llm_profile_id}'"+ )+ cursor = db.execute_sql(query)+ result_row = cursor.fetchone()+ if not result_row:+ raise APIError(+ message=f"LLM profile with UUID '{llm_profile_id}' is not found.",+ code=404,+ )+ columns = [desc[0] for desc in cursor.description]+ data_dict: dict[str, Any] = dict(zip(columns, result_row, strict=False))+ cursor.close()+ return data_dictdiff --git a/platform-service/uv.lock b/platform-service/uv.lockindex d9f124ce7..34d8dfec8 100644--- a/platform-service/uv.lock+++ b/platform-service/uv.lock@@ -3120,7 +3120,7 @@ requires-dist = [{ name = "requests", specifier = ">=2.31.0" },{ name = "unstract-core", extras = ["flask"], editable = "../unstract/core" },{ name = "unstract-flags", editable = "../unstract/flags" },- { name = "unstract-sdk", extras = ["gcs", "azure", "aws"], specifier = "~=0.73.1" },+ { name = "unstract-sdk", extras = ["gcs", "azure", "aws"], specifier = "~=0.74.0" },][package.metadata.requires-dev]@@ -3137,7 +3137,7 @@ test = [{ name = "pytest", specifier = ">=8.0.1" }][[package]]name = "unstract-sdk"-version = "0.73.1"+version = "0.74.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "filetype" },@@ -3176,9 +3176,9 @@ dependencies = [{ name = "tiktoken" },{ name = "transformers" },]-sdist = { url = "https://files.pythonhosted.org/packages/88/bb/60a8cc6d352ca204e04bf33a85d9b9961415833e0f314b2cbbcd42c5a631/unstract_sdk-0.73.1.tar.gz", hash = "sha256:294d0206bc43c09bc8c884a77ab78794301de0dab3ff6a1fe68f9c5235f72251", size = 2376089 }+sdist = { url = "https://files.pythonhosted.org/packages/74/b7/8d4db4b8a334ef1969cf56074e595922a4814e8252d525cf9223772ed01b/unstract_sdk-0.74.0.tar.gz", hash = "sha256:adb2e320dfedf2c52d4fe9fa29cfb6c64deb7869712cd6c344b0e15062606def", size = 2376223 }wheels = [- { url = "https://files.pythonhosted.org/packages/d8/38/4696fe06d95fd62153dd7a270332e3bfc76a4ffc7e57c0b9135459666317/unstract_sdk-0.73.1-py3-none-any.whl", hash = "sha256:527c4904a8a4500db5ebba0ee3db216069d8093d064187c1e09eecde42402ef9", size = 266247 },+ { url = "https://files.pythonhosted.org/packages/a4/2f/862cef00c35ef9dc2d546e1b82e498a5b4750bfc6d55c1217123fa7fc56a/unstract_sdk-0.74.0-py3-none-any.whl", hash = "sha256:209a85c002e8ee049a3fdd1ffffaac6848b8670222c9b272593ee4002f519aef", size = 266399 },][package.optional-dependencies]diff --git a/prompt-service/pyproject.toml b/prompt-service/pyproject.tomlindex 8695385dd..f8330a3b6 100644--- a/prompt-service/pyproject.toml+++ b/prompt-service/pyproject.toml@@ -16,7 +16,7 @@ dependencies = ["json-repair~=0.42.0",# TODO: Temporarily removing the extra dependencies of aws and gcs from unstract-sdk# to resolve lock file. Will have to be re-looked into- "unstract-sdk[azure]~=0.73.2",+ "unstract-sdk[azure]~=0.74.0","gcsfs==2024.10.0","s3fs==2024.10.0","redis>=5.0.3,<5.3",diff --git a/prompt-service/uv.lock b/prompt-service/uv.lockindex bee10d40c..52d2f3e91 100644--- a/prompt-service/uv.lock+++ b/prompt-service/uv.lock@@ -3143,7 +3143,7 @@ requires-dist = [{ name = "s3fs", specifier = "==2024.10.0" },{ name = "unstract-core", editable = "../unstract/core" },{ name = "unstract-flags", editable = "../unstract/flags" },- { name = "unstract-sdk", extras = ["azure"], specifier = "~=0.73.2" },+ { name = "unstract-sdk", extras = ["azure"], specifier = "~=0.74.0" },][package.metadata.requires-dev]@@ -3166,7 +3166,7 @@ test = [[[package]]name = "unstract-sdk"-version = "0.73.2"+version = "0.74.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "filetype" },@@ -3205,9 +3205,9 @@ dependencies = [{ name = "tiktoken" },{ name = "transformers" },]-sdist = { url = "https://files.pythonhosted.org/packages/ad/07/8bdc82327d6d30ed5c120aa607b044fad88dd50a0e0ac340a0214834d254/unstract_sdk-0.73.2.tar.gz", hash = "sha256:8285e44997b2145ec5920e0cd651312fd0cebf1742bb314f516f59f87f7846f4", size = 2376138 }+sdist = { url = "https://files.pythonhosted.org/packages/74/b7/8d4db4b8a334ef1969cf56074e595922a4814e8252d525cf9223772ed01b/unstract_sdk-0.74.0.tar.gz", hash = "sha256:adb2e320dfedf2c52d4fe9fa29cfb6c64deb7869712cd6c344b0e15062606def", size = 2376223 }wheels = [- { url = "https://files.pythonhosted.org/packages/e5/45/c90c9d922e62bd3935e3fbb045b3083ae56148aa7bc56ba5ee8c3549ade1/unstract_sdk-0.73.2-py3-none-any.whl", hash = "sha256:7faf204411e5c8b7037fbaae4697e772b22cb4b045e0ec3d961dd1469952e330", size = 266302 },+ { url = "https://files.pythonhosted.org/packages/a4/2f/862cef00c35ef9dc2d546e1b82e498a5b4750bfc6d55c1217123fa7fc56a/unstract_sdk-0.74.0-py3-none-any.whl", hash = "sha256:209a85c002e8ee049a3fdd1ffffaac6848b8670222c9b272593ee4002f519aef", size = 266399 },][package.optional-dependencies]diff --git a/pyproject.toml b/pyproject.tomlindex dc59e36fc..a73e4cd05 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -37,7 +37,7 @@ hook-check-django-migrations = ["psycopg2-binary==2.9.9","python-dotenv==1.0.0","python-magic==0.4.27",- "unstract-sdk~=0.73.1",+ "unstract-sdk~=0.74.0","unstract-connectors","unstract-core","unstract-flags",diff --git a/tools/classifier/requirements.txt b/tools/classifier/requirements.txtindex dafab997f..8de102ba1 100644--- a/tools/classifier/requirements.txt+++ b/tools/classifier/requirements.txt@@ -4,4 +4,4 @@# aws alone is needed here# because tools use transient temporary storage.-unstract-sdk[aws]~=0.73.1+unstract-sdk[aws]~=0.74.0diff --git a/tools/structure/requirements.txt b/tools/structure/requirements.txtindex 9bd4deebf..c16a340c6 100644--- a/tools/structure/requirements.txt+++ b/tools/structure/requirements.txt@@ -3,4 +3,4 @@# Required for all unstract tools# aws alone is needed here# because tools use transient temporary storage.-unstract-sdk[aws]~=0.73.1+unstract-sdk[aws]~=0.74.0diff --git a/tools/structure/src/constants.py b/tools/structure/src/constants.pyindex 822c061ab..74b334c39 100644--- a/tools/structure/src/constants.py+++ b/tools/structure/src/constants.py@@ -79,6 +79,7 @@ class SettingsKeys:INDEXING = "indexing"EXECUTION_ID = "execution_id"IS_DIRECTORY_MODE = "is_directory_mode"+ LLM_PROFILE_ID = "llm_profile_id"class IndexingConstants:diff --git a/tools/structure/src/helpers.py b/tools/structure/src/helpers.pyindex ae05be02d..20501301c 100644--- a/tools/structure/src/helpers.py+++ b/tools/structure/src/helpers.py@@ -1,5 +1,6 @@import datetimeimport logging+from collections.abc import Callablefrom typing import Anyfrom constants import IndexingConstants as IKeys@@ -96,6 +97,37 @@ class StructureToolHelper:)return responder.index(payload=payload)+ @staticmethod+ def handle_profile_overrides(+ tool: BaseTool,+ llm_profile_to_override: dict,+ llm_profile_id: str,+ tool_metadata: dict,+ apply_profile_overrides_func: Callable[[dict, dict], list[str]],+ ) -> None:+ """Handle profile overrides and logging.++ Args:+ tool: The tool instance for logging+ llm_profile_to_override: The profile data to apply, or None if no profile+ llm_profile_id: The profile ID for logging purposes+ tool_metadata: The tool metadata dictionary to modify+ apply_profile_overrides_func: Function to apply profile overrides+ """+ if llm_profile_to_override:+ tool.stream_log(+ f"Applying profile overrides from profile: {llm_profile_to_override.get('profile_name', llm_profile_id)}"+ )+ changes = apply_profile_overrides_func(tool_metadata, llm_profile_to_override)+ if changes:+ tool.stream_log("Profile overrides applied successfully. Changes made:")+ for change in changes:+ tool.stream_log(f" - {change}")+ else:+ tool.stream_log(+ "Profile overrides applied - no changes needed (values already matched)"+ )+@staticmethoddef elapsed_time(start_time) -> float:"""Returns the elapsed time since the process was started."""diff --git a/tools/structure/src/main.py b/tools/structure/src/main.pyindex 85c256aee..b99f6a87a 100644--- a/tools/structure/src/main.py+++ b/tools/structure/src/main.py@@ -25,6 +25,88 @@ PAID_FEATURE_MSG = (class StructureTool(BaseTool):+ def _apply_profile_overrides(+ self, tool_metadata: dict, profile_data: dict+ ) -> list[str]:+ """Apply profile overrides to tool metadata.++ Args:+ tool_metadata: The tool metadata dictionary to modify+ profile_data: The profile data containing override values++ Returns:+ List of change descriptions+ """+ changes = []++ # Mapping between profile keys and tool metadata keys+ profile_to_tool_mapping = {+ "chunk_overlap": "chunk-overlap",+ "chunk_size": "chunk-size",+ "embedding_model_id": "embedding",+ "llm_id": "llm",+ "similarity_top_k": "similarity-top-k",+ "vector_store_id": "vector-db",+ "x2text_id": "x2text_adapter",+ "retrieval_strategy": "retrieval-strategy",+ }++ # Override tool_settings section+ if "tool_settings" in tool_metadata:+ tool_changes = self._override_section(+ tool_metadata["tool_settings"],+ profile_data,+ profile_to_tool_mapping,+ section_name="tool_settings",+ )+ changes.extend(tool_changes)++ # Override each output in outputs section+ if "outputs" in tool_metadata:+ for i, output in enumerate(tool_metadata["outputs"]):+ output_name = output.get("name", f"output_{i}")+ output_changes = self._override_section(+ output,+ profile_data,+ profile_to_tool_mapping,+ section_name=f"output[{output_name}]",+ )+ changes.extend(output_changes)++ return changes++ def _override_section(+ self,+ section: dict,+ profile_data: dict,+ mapping: dict,+ section_name: str = "section",+ ) -> list[str]:+ """Override values in a section using profile data.++ Args:+ section: The section dictionary to modify+ profile_data: The profile data containing override values+ mapping: Mapping between profile keys and section keys+ section_name: Name of the section for logging purposes++ Returns:+ List of change descriptions+ """+ changes = []+ for profile_key, section_key in mapping.items():+ if profile_key in profile_data and section_key in section:+ old_value = section[section_key]+ new_value = profile_data[profile_key]+ if old_value != new_value: # Only track actual changes+ section[section_key] = new_value+ change_desc = (+ f"{section_name}.{section_key}: {old_value} -> {new_value}"+ )+ changes.append(change_desc)+ self.stream_log(f"Overrode {change_desc}")+ return changes+def validate(self, input_file: str, settings: dict[str, Any]) -> None:enable_challenge: bool = settings.get(SettingsKeys.ENABLE_CHALLENGE, False)challenge_llm: str = settings.get(SettingsKeys.CHALLENGE_LLM_ADAPTER_ID, "")@@ -66,7 +148,22 @@ class StructureTool(BaseTool):exported_tool = platform_helper.get_prompt_studio_tool(prompt_registry_id=prompt_registry_id)+ llm_profile_id = self.get_exec_metadata.get(SettingsKeys.LLM_PROFILE_ID)+ llm_profile_to_override = None+ if llm_profile_id:+ llm_profile = platform_helper.get_llm_profile(llm_profile_id)+ llm_profile_to_override = llm_profile # Use the entire profile data+tool_metadata = exported_tool[SettingsKeys.TOOL_METADATA]++ # Apply profile overrides if available+ STHelper.handle_profile_overrides(+ self,+ llm_profile_to_override,+ llm_profile_id,+ tool_metadata,+ self._apply_profile_overrides,+ )ps_project_name = tool_metadata.get("name", prompt_registry_id)# Count only the active (enabled) promptstotal_prompt_count = len(tool_metadata[SettingsKeys.OUTPUTS])diff --git a/tools/text_extractor/requirements.txt b/tools/text_extractor/requirements.txtindex dafab997f..8de102ba1 100644--- a/tools/text_extractor/requirements.txt+++ b/tools/text_extractor/requirements.txt@@ -4,4 +4,4 @@# aws alone is needed here# because tools use transient temporary storage.-unstract-sdk[aws]~=0.73.1+unstract-sdk[aws]~=0.74.0diff --git a/unstract/filesystem/pyproject.toml b/unstract/filesystem/pyproject.tomlindex db9db0999..fc7d3b7f6 100644--- a/unstract/filesystem/pyproject.toml+++ b/unstract/filesystem/pyproject.toml@@ -6,7 +6,7 @@ authors = [{ name = "Zipstack Inc.", email = "[email protected]" }]requires-python = ">=3.12,<3.13"readme = "README.md"-dependencies = ["unstract-sdk~=0.73.1"]+dependencies = ["unstract-sdk~=0.74.0"]# [tool.uv.sources]# unstract-sdk = { git = "https://github.com/Zipstack/unstract-sdk", branch = "python-upgrade" }diff --git a/unstract/filesystem/uv.lock b/unstract/filesystem/uv.lockindex 44776e58e..404b8ac07 100644--- a/unstract/filesystem/uv.lock+++ b/unstract/filesystem/uv.lock@@ -2592,11 +2592,11 @@ dependencies = [][package.metadata]-requires-dist = [{ name = "unstract-sdk", specifier = "~=0.73.1" }]+requires-dist = [{ name = "unstract-sdk", specifier = "~=0.74.0" }][[package]]name = "unstract-sdk"-version = "0.73.1"+version = "0.74.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "filetype" },@@ -2635,9 +2635,9 @@ dependencies = [{ name = "tiktoken" },{ name = "transformers" },]-sdist = { url = "https://files.pythonhosted.org/packages/88/bb/60a8cc6d352ca204e04bf33a85d9b9961415833e0f314b2cbbcd42c5a631/unstract_sdk-0.73.1.tar.gz", hash = "sha256:294d0206bc43c09bc8c884a77ab78794301de0dab3ff6a1fe68f9c5235f72251", size = 2376089 }+sdist = { url = "https://files.pythonhosted.org/packages/74/b7/8d4db4b8a334ef1969cf56074e595922a4814e8252d525cf9223772ed01b/unstract_sdk-0.74.0.tar.gz", hash = "sha256:adb2e320dfedf2c52d4fe9fa29cfb6c64deb7869712cd6c344b0e15062606def", size = 2376223 }wheels = [- { url = "https://files.pythonhosted.org/packages/d8/38/4696fe06d95fd62153dd7a270332e3bfc76a4ffc7e57c0b9135459666317/unstract_sdk-0.73.1-py3-none-any.whl", hash = "sha256:527c4904a8a4500db5ebba0ee3db216069d8093d064187c1e09eecde42402ef9", size = 266247 },+ { url = "https://files.pythonhosted.org/packages/a4/2f/862cef00c35ef9dc2d546e1b82e498a5b4750bfc6d55c1217123fa7fc56a/unstract_sdk-0.74.0-py3-none-any.whl", hash = "sha256:209a85c002e8ee049a3fdd1ffffaac6848b8670222c9b272593ee4002f519aef", size = 266399 },][[package]]diff --git a/unstract/tool-registry/pyproject.toml b/unstract/tool-registry/pyproject.tomlindex f2cf5f330..24cdfc739 100644--- a/unstract/tool-registry/pyproject.toml+++ b/unstract/tool-registry/pyproject.toml@@ -11,7 +11,7 @@ dependencies = ["docker~=6.1.3","jsonschema~=4.18.2","PyYAML~=6.0.1",- "unstract-sdk~=0.73.1",+ "unstract-sdk~=0.74.0","unstract-tool-sandbox","unstract-flags",]diff --git a/unstract/workflow-execution/src/unstract/workflow_execution/constants.py b/unstract/workflow-execution/src/unstract/workflow_execution/constants.pyindex 04c0d1fcf..06168e9df 100644--- a/unstract/workflow-execution/src/unstract/workflow_execution/constants.py+++ b/unstract/workflow-execution/src/unstract/workflow_execution/constants.py@@ -45,6 +45,7 @@ class MetaDataKey:ORGANIZATION_ID = "organization_id"TOOL_METADATA = "tool_metadata"TAGS = "tags"+ LLM_PROFILE_ID = "llm_profile_id"class ToolMetadataKey:diff --git a/unstract/workflow-execution/src/unstract/workflow_execution/execution_file_handler.py b/unstract/workflow-execution/src/unstract/workflow_execution/execution_file_handler.pyindex 9a8ec919e..9505c2da4 100644--- a/unstract/workflow-execution/src/unstract/workflow_execution/execution_file_handler.py+++ b/unstract/workflow-execution/src/unstract/workflow_execution/execution_file_handler.py@@ -101,6 +101,7 @@ class ExecutionFileHandler:file_execution_id: str,source_hash: str,tags: list[str],+ llm_profile_id: str | None = None,) -> None:"""Creating metadata for workflow. This method is responsible forcreating metadata for the workflow. It takes the input file path and@@ -112,6 +113,7 @@ class ExecutionFileHandler:file_execution_id (str): Unique execution id for the file.source_hash (str): The hash value of the source/input file.tags (list[str]): Tag names associated with the workflow execution.+ llm_profile_id (str, optional): LLM profile ID for overriding tool settings.Returns:None@@ -134,6 +136,11 @@ class ExecutionFileHandler:MetaDataKey.FILE_EXECUTION_ID: str(file_execution_id),MetaDataKey.TAGS: tags,}++ # Add llm_profile_id to metadata if provided+ if llm_profile_id:+ content[MetaDataKey.LLM_PROFILE_ID] = llm_profile_id+file_system = FileSystem(FileStorageType.WORKFLOW_EXECUTION)file_storage = file_system.get_file_storage()file_storage.json_dump(path=metadata_path, data=content)diff --git a/uv.lock b/uv.lockindex 04ef0e0c7..57fffc39c 100644--- a/uv.lock+++ b/uv.lock@@ -3847,7 +3847,7 @@ hook-check-django-migrations = [{ name = "unstract-core", editable = "unstract/core" },{ name = "unstract-filesystem", editable = "unstract/filesystem" },{ name = "unstract-flags", editable = "unstract/flags" },- { name = "unstract-sdk", specifier = "~=0.73.1" },+ { name = "unstract-sdk", specifier = "~=0.74.0" },{ name = "unstract-tool-registry", editable = "unstract/tool-registry" },{ name = "unstract-tool-sandbox", editable = "unstract/tool-sandbox" },{ name = "unstract-workflow-execution", editable = "unstract/workflow-execution" },@@ -3930,7 +3930,7 @@ dependencies = [][package.metadata]-requires-dist = [{ name = "unstract-sdk", specifier = "~=0.73.1" }]+requires-dist = [{ name = "unstract-sdk", specifier = "~=0.74.0" }][[package]]name = "unstract-flags"@@ -3949,7 +3949,7 @@ requires-dist = [[[package]]name = "unstract-sdk"-version = "0.73.1"+version = "0.74.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "filetype" },@@ -3988,9 +3988,9 @@ dependencies = [{ name = "tiktoken" },{ name = "transformers" },]-sdist = { url = "https://files.pythonhosted.org/packages/88/bb/60a8cc6d352ca204e04bf33a85d9b9961415833e0f314b2cbbcd42c5a631/unstract_sdk-0.73.1.tar.gz", hash = "sha256:294d0206bc43c09bc8c884a77ab78794301de0dab3ff6a1fe68f9c5235f72251", size = 2376089 }+sdist = { url = "https://files.pythonhosted.org/packages/74/b7/8d4db4b8a334ef1969cf56074e595922a4814e8252d525cf9223772ed01b/unstract_sdk-0.74.0.tar.gz", hash = "sha256:adb2e320dfedf2c52d4fe9fa29cfb6c64deb7869712cd6c344b0e15062606def", size = 2376223 }wheels = [- { url = "https://files.pythonhosted.org/packages/d8/38/4696fe06d95fd62153dd7a270332e3bfc76a4ffc7e57c0b9135459666317/unstract_sdk-0.73.1-py3-none-any.whl", hash = "sha256:527c4904a8a4500db5ebba0ee3db216069d8093d064187c1e09eecde42402ef9", size = 266247 },+ { url = "https://files.pythonhosted.org/packages/a4/2f/862cef00c35ef9dc2d546e1b82e498a5b4750bfc6d55c1217123fa7fc56a/unstract_sdk-0.74.0-py3-none-any.whl", hash = "sha256:209a85c002e8ee049a3fdd1ffffaac6848b8670222c9b272593ee4002f519aef", size = 266399 },][[package]]@@ -4012,7 +4012,7 @@ requires-dist = [{ name = "jsonschema", specifier = "~=4.18.2" },{ name = "pyyaml", specifier = "~=6.0.1" },{ name = "unstract-flags", editable = "unstract/flags" },- { name = "unstract-sdk", specifier = "~=0.73.1" },+ { name = "unstract-sdk", specifier = "~=0.74.0" },{ name = "unstract-tool-sandbox", editable = "unstract/tool-sandbox" },]