LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
Refactoring for latest SDK v0.29.0+ (#366)
Gayathric694de72e7a2
diff --git a/backend/pdm.lock b/backend/pdm.lockindex e639a95f2..57c972c42 100644--- a/backend/pdm.lock+++ b/backend/pdm.lock@@ -5,7 +5,7 @@groups = ["default", "deploy", "test"]strategy = ["cross_platform", "inherit_metadata"]lock_version = "4.4.1"-content_hash = "sha256:c94b9b5eaa8320bf0a7f2ff8aeee0b49b1beff589686ff704823a5e196b0ec3b"+content_hash = "sha256:65995bec9035d980c5dbfa609f531964e573eb0bb13fa53c207a4ae006c253b0"[[package]]name = "adlfs"@@ -1827,7 +1827,7 @@ files = [[[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["default"]@@ -1839,29 +1839,29 @@ dependencies = ["sniffio",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"extras = ["http2"]requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["default"]dependencies = ["h2<5,>=3",- "httpx==0.27.0",+ "httpx==0.25.2",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "huggingface-hub"-version = "0.23.2"+version = "0.23.3"requires_python = ">=3.8.0"summary = "Client library to download and publish models, datasets and other repos on the huggingface.co hub"groups = ["default"]@@ -1875,8 +1875,8 @@ dependencies = ["typing-extensions>=3.7.4.3",]files = [- {file = "huggingface_hub-0.23.2-py3-none-any.whl", hash = "sha256:48727a16e704d409c4bb5913613308499664f22a99743435dc3a13b23c485827"},- {file = "huggingface_hub-0.23.2.tar.gz", hash = "sha256:f6829b62d5fdecb452a76fdbec620cba4c1573655a8d710c1df71735fd9edbd2"},+ {file = "huggingface_hub-0.23.3-py3-none-any.whl", hash = "sha256:22222c41223f1b7c209ae5511d2d82907325a0e3cdbce5f66949d43c598ff3bc"},+ {file = "huggingface_hub-0.23.3.tar.gz", hash = "sha256:1a1118a0b3dea3bab6c325d71be16f5ffe441d32f3ac7c348d6875911b694b5b"},][[package]]@@ -2075,7 +2075,7 @@ files = [[[package]]name = "llama-index-core"-version = "0.10.43"+version = "0.10.43.post1"requires_python = "<4.0,>=3.8.1"summary = "Interface between LLMs and your data"groups = ["default"]@@ -2105,8 +2105,8 @@ dependencies = ["wrapt",]files = [- {file = "llama_index_core-0.10.43-py3-none-any.whl", hash = "sha256:355a5f7808dfa09dc27b6bd564926fa697e0279344b829732a9f91b8000157ac"},- {file = "llama_index_core-0.10.43.tar.gz", hash = "sha256:c7dce5158e112d3cbc8d331c303e830b2eabcae55c9636e8050dc85320522c1b"},+ {file = "llama_index_core-0.10.43.post1-py3-none-any.whl", hash = "sha256:673d0e7965b712150a1bc0bfff5512606dc1d8581fcfe617e18fd3ccd4e3d8b1"},+ {file = "llama_index_core-0.10.43.post1.tar.gz", hash = "sha256:fad9babac97172d0c6a3cd006fe48f0dfae88c481cf7d5ca5703dbaf8985ac54"},][[package]]@@ -2523,7 +2523,7 @@ files = [[[package]]name = "marshmallow"-version = "3.21.2"+version = "3.21.3"requires_python = ">=3.8"summary = "A lightweight library for converting complex datatypes to and from native Python datatypes."groups = ["default"]@@ -2531,8 +2531,8 @@ dependencies = ["packaging>=17.0",]files = [- {file = "marshmallow-3.21.2-py3-none-any.whl", hash = "sha256:70b54a6282f4704d12c0a41599682c5c5450e843b9ec406308653b47c59648a1"},- {file = "marshmallow-3.21.2.tar.gz", hash = "sha256:82408deadd8b33d56338d2182d455db632c6313aa2af61916672146bb32edc56"},+ {file = "marshmallow-3.21.3-py3-none-any.whl", hash = "sha256:86ce7fb914aa865001a4b2092c4c2872d13bc347f3d42673272cabfdbad386f1"},+ {file = "marshmallow-3.21.3.tar.gz", hash = "sha256:4f57c5e050a54d66361e826f94fba213eb10b67b2fdb02c3e0343ce207ba1662"},][[package]]@@ -2549,18 +2549,18 @@ files = [[[package]]name = "mistralai"-version = "0.3.0"+version = "0.4.0"requires_python = "<4.0,>=3.9"summary = ""groups = ["default"]dependencies = [- "httpx<1,>=0.25.2",- "orjson<4.0.0,>=3.9.10",+ "httpx<0.26,>=0.25",+ "orjson<3.11,>=3.9.10","pydantic<3.0.0,>=2.5.2",]files = [- {file = "mistralai-0.3.0-py3-none-any.whl", hash = "sha256:7aca38baf9008d6c690b750ccd78b27cf5b2b052c26649ccae5cc573bb6fb246"},- {file = "mistralai-0.3.0.tar.gz", hash = "sha256:61da3ab8d9f83251e9e5d8c8fd77c076511b5d83525b4d5550444c2eb00b0a7b"},+ {file = "mistralai-0.4.0-py3-none-any.whl", hash = "sha256:54d6a7ff03cd11214477ff6b9bc052e655ec7a07028a7dd0898cd852da71a220"},+ {file = "mistralai-0.4.0.tar.gz", hash = "sha256:1f21f02dec1fd6fefe12ca100d5937fd62542c11008c193d16df6f4a2d8554da"},][[package]]@@ -4689,7 +4689,7 @@ dependencies = [[[package]]name = "unstract-sdk"-version = "0.30.0"+version = "0.31.0"requires_python = "<3.11.1,>=3.9"summary = "A framework for writing Unstract Tools/Apps"groups = ["default"]@@ -4704,8 +4704,8 @@ dependencies = ["unstract-adapters~=0.18.0",]files = [- {file = "unstract_sdk-0.30.0-py3-none-any.whl", hash = "sha256:f6d31ba9dd3b29ec6e21ff1760dea328c979290b0096fbc94e7c960c90ed837e"},- {file = "unstract_sdk-0.30.0.tar.gz", hash = "sha256:68f5b9c0747effff1d375bb32b3691f6e75ee7c5e66ccdefdf9f96b83c17ee64"},+ {file = "unstract_sdk-0.31.0-py3-none-any.whl", hash = "sha256:1a468e1f7272f45397d9d0592937049462096ee4c0f7f4486110f06bb3800b9e"},+ {file = "unstract_sdk-0.31.0.tar.gz", hash = "sha256:9230e2540bf5d5130bfd171d17ae7cb7a016dd74047449b521d1cf3a4c8cc8ab"},][[package]]diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.pyindex 559c97896..43410c10c 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py@@ -693,7 +693,7 @@ class PromptStudioHelper:usage_kwargs = {"run_id": run_id}util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_id)tool_index = Index(tool=util)- doc_id: str = tool_index.index_file(+ doc_id: str = tool_index.index(tool_id=tool_id,embedding_instance_id=embedding_model,vector_db_instance_id=vector_db,@@ -703,7 +703,7 @@ class PromptStudioHelper:chunk_overlap=profile_manager.chunk_overlap,reindex=reindex,output_file_path=extract_file_path,- usage_kwargs=usage_kwargs,+ usage_kwargs=usage_kwargs.copy(),)PromptStudioIndexHelper.handle_index_manager(diff --git a/backend/prompt_studio/prompt_studio_core/views.py b/backend/prompt_studio/prompt_studio_core/views.pyindex 8917513c7..af38e2584 100644--- a/backend/prompt_studio/prompt_studio_core/views.py+++ b/backend/prompt_studio/prompt_studio_core/views.py@@ -43,7 +43,7 @@ from rest_framework.request import Requestfrom rest_framework.response import Responsefrom rest_framework.versioning import URLPathVersioningfrom tool_instance.models import ToolInstance-from utils.common_utils import CommonUtils+from unstract.sdk.utils.common_utils import CommonUtilsfrom utils.user_session import UserSessionUtilsfrom unstract.connectors.filesystems.local_storage.local_storage import LocalStorageFS@@ -205,7 +205,7 @@ class PromptStudioCoreView(viewsets.ModelViewSet):document: DocumentManager = DocumentManager.objects.get(pk=document_id)file_name: str = document.document_name# Generate a run_id- run_id = CommonUtils.get_uuid()+ run_id = CommonUtils.generate_uuid()unique_id = PromptStudioHelper.index_document(tool_id=str(tool.tool_id),file_name=file_name,@@ -215,6 +215,8 @@ class PromptStudioCoreView(viewsets.ModelViewSet):run_id=run_id,)+ usage_kwargs: dict[Any, Any] = dict()+ usage_kwargs[ToolStudioPromptKeys.RUN_ID] = run_idfor processor_plugin in self.processor_plugins:cls = processor_plugin[ProcessorConfig.METADATA][ProcessorConfig.METADATA_SERVICE_CLASS@@ -225,6 +227,7 @@ class PromptStudioCoreView(viewsets.ModelViewSet):org_id=UserSessionUtils.get_organization_id(request),user_id=tool.created_by.user_id,document_id=document_id,+ usage_kwargs=usage_kwargs.copy(),)if unique_id:@@ -256,7 +259,7 @@ class PromptStudioCoreView(viewsets.ModelViewSet):run_id: str = request.data.get(ToolStudioPromptKeys.RUN_ID)if not run_id:# Generate a run_id- run_id = CommonUtils.get_uuid()+ run_id = CommonUtils.generate_uuid()response: dict[str, Any] = PromptStudioHelper.prompt_responder(id=id,@@ -287,7 +290,7 @@ class PromptStudioCoreView(viewsets.ModelViewSet):run_id: str = request.data.get(ToolStudioPromptKeys.RUN_ID)if not run_id:# Generate a run_id- run_id = CommonUtils.get_uuid()+ run_id = CommonUtils.generate_uuid()response: dict[str, Any] = PromptStudioHelper.prompt_responder(tool_id=tool_id,org_id=UserSessionUtils.get_organization_id(request),diff --git a/backend/pyproject.toml b/backend/pyproject.tomlindex f780de20a..ef6957776 100644--- a/backend/pyproject.toml+++ b/backend/pyproject.toml@@ -31,7 +31,7 @@ dependencies = ["python-socketio==5.9.0", # For log_events"social-auth-app-django==5.3.0", # For OAuth"social-auth-core==4.4.2", # For OAuth- "unstract-sdk~=0.30.0",+ "unstract-sdk~=0.31.0","unstract-adapters~=0.18.0",# ! IMPORTANT!# Indirect local dependencies usually need to be added in their own projectsdiff --git a/backend/sample.env b/backend/sample.envindex 0845ee831..765331cbe 100644--- a/backend/sample.env+++ b/backend/sample.env@@ -89,9 +89,9 @@ PROMPT_PORT=3003PROMPT_STUDIO_FILE_PATH=/app/prompt-studio-data# Structure Tool-STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.22"+STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.23"STRUCTURE_TOOL_IMAGE_NAME="unstract/tool-structure"-STRUCTURE_TOOL_IMAGE_TAG="0.0.22"+STRUCTURE_TOOL_IMAGE_TAG="0.0.23"# Feature FlagsEVALUATION_SERVER_IP=localhostdiff --git a/backend/utils/common_utils.py b/backend/utils/common_utils.pyindex 98589fa3a..8db14369e 100644--- a/backend/utils/common_utils.py+++ b/backend/utils/common_utils.py@@ -1,4 +1,3 @@-import uuidfrom enum import Enum@@ -15,11 +14,6 @@ class CommonUtils:"""return string.lower() == "true"- @staticmethod- def get_uuid() -> str:- """Class method to get uuid."""- return str(uuid.uuid4())-class ModelEnum(Enum):@classmethoddiff --git a/pdm.lock b/pdm.lockindex e80dd26e0..fdbc5bf0e 100644--- a/pdm.lock+++ b/pdm.lock@@ -5,7 +5,7 @@groups = ["default", "hook-check-django-migrations", "lint"]strategy = ["cross_platform", "inherit_metadata"]lock_version = "4.4.1"-content_hash = "sha256:4863d4dc20b7a75e91cc45eecdfee67bc7e11f1ec0ff188036c84c6cb051b88d"+content_hash = "sha256:758066767d66116dd4e0635b83b7b5e60e21c8a2a289fb428e7f21bf7ab89421"[[package]]name = "adlfs"@@ -1826,7 +1826,7 @@ files = [[[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["hook-check-django-migrations"]@@ -1838,29 +1838,29 @@ dependencies = ["sniffio",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"extras = ["http2"]requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["hook-check-django-migrations"]dependencies = ["h2<5,>=3",- "httpx==0.27.0",+ "httpx==0.25.2",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "huggingface-hub"-version = "0.23.2"+version = "0.23.3"requires_python = ">=3.8.0"summary = "Client library to download and publish models, datasets and other repos on the huggingface.co hub"groups = ["hook-check-django-migrations"]@@ -1874,8 +1874,8 @@ dependencies = ["typing-extensions>=3.7.4.3",]files = [- {file = "huggingface_hub-0.23.2-py3-none-any.whl", hash = "sha256:48727a16e704d409c4bb5913613308499664f22a99743435dc3a13b23c485827"},- {file = "huggingface_hub-0.23.2.tar.gz", hash = "sha256:f6829b62d5fdecb452a76fdbec620cba4c1573655a8d710c1df71735fd9edbd2"},+ {file = "huggingface_hub-0.23.3-py3-none-any.whl", hash = "sha256:22222c41223f1b7c209ae5511d2d82907325a0e3cdbce5f66949d43c598ff3bc"},+ {file = "huggingface_hub-0.23.3.tar.gz", hash = "sha256:1a1118a0b3dea3bab6c325d71be16f5ffe441d32f3ac7c348d6875911b694b5b"},][[package]]@@ -2074,7 +2074,7 @@ files = [[[package]]name = "llama-index-core"-version = "0.10.43"+version = "0.10.43.post1"requires_python = "<4.0,>=3.8.1"summary = "Interface between LLMs and your data"groups = ["hook-check-django-migrations"]@@ -2104,8 +2104,8 @@ dependencies = ["wrapt",]files = [- {file = "llama_index_core-0.10.43-py3-none-any.whl", hash = "sha256:355a5f7808dfa09dc27b6bd564926fa697e0279344b829732a9f91b8000157ac"},- {file = "llama_index_core-0.10.43.tar.gz", hash = "sha256:c7dce5158e112d3cbc8d331c303e830b2eabcae55c9636e8050dc85320522c1b"},+ {file = "llama_index_core-0.10.43.post1-py3-none-any.whl", hash = "sha256:673d0e7965b712150a1bc0bfff5512606dc1d8581fcfe617e18fd3ccd4e3d8b1"},+ {file = "llama_index_core-0.10.43.post1.tar.gz", hash = "sha256:fad9babac97172d0c6a3cd006fe48f0dfae88c481cf7d5ca5703dbaf8985ac54"},][[package]]@@ -2522,7 +2522,7 @@ files = [[[package]]name = "marshmallow"-version = "3.21.2"+version = "3.21.3"requires_python = ">=3.8"summary = "A lightweight library for converting complex datatypes to and from native Python datatypes."groups = ["hook-check-django-migrations"]@@ -2530,8 +2530,8 @@ dependencies = ["packaging>=17.0",]files = [- {file = "marshmallow-3.21.2-py3-none-any.whl", hash = "sha256:70b54a6282f4704d12c0a41599682c5c5450e843b9ec406308653b47c59648a1"},- {file = "marshmallow-3.21.2.tar.gz", hash = "sha256:82408deadd8b33d56338d2182d455db632c6313aa2af61916672146bb32edc56"},+ {file = "marshmallow-3.21.3-py3-none-any.whl", hash = "sha256:86ce7fb914aa865001a4b2092c4c2872d13bc347f3d42673272cabfdbad386f1"},+ {file = "marshmallow-3.21.3.tar.gz", hash = "sha256:4f57c5e050a54d66361e826f94fba213eb10b67b2fdb02c3e0343ce207ba1662"},][[package]]@@ -2559,18 +2559,18 @@ files = [[[package]]name = "mistralai"-version = "0.3.0"+version = "0.4.0"requires_python = "<4.0,>=3.9"summary = ""groups = ["hook-check-django-migrations"]dependencies = [- "httpx<1,>=0.25.2",- "orjson<4.0.0,>=3.9.10",+ "httpx<0.26,>=0.25",+ "orjson<3.11,>=3.9.10","pydantic<3.0.0,>=2.5.2",]files = [- {file = "mistralai-0.3.0-py3-none-any.whl", hash = "sha256:7aca38baf9008d6c690b750ccd78b27cf5b2b052c26649ccae5cc573bb6fb246"},- {file = "mistralai-0.3.0.tar.gz", hash = "sha256:61da3ab8d9f83251e9e5d8c8fd77c076511b5d83525b4d5550444c2eb00b0a7b"},+ {file = "mistralai-0.4.0-py3-none-any.whl", hash = "sha256:54d6a7ff03cd11214477ff6b9bc052e655ec7a07028a7dd0898cd852da71a220"},+ {file = "mistralai-0.4.0.tar.gz", hash = "sha256:1f21f02dec1fd6fefe12ca100d5937fd62542c11008c193d16df6f4a2d8554da"},][[package]]@@ -4799,7 +4799,7 @@ dependencies = [[[package]]name = "unstract-sdk"-version = "0.30.0"+version = "0.31.0"requires_python = "<3.11.1,>=3.9"summary = "A framework for writing Unstract Tools/Apps"groups = ["hook-check-django-migrations"]@@ -4814,8 +4814,8 @@ dependencies = ["unstract-adapters~=0.18.0",]files = [- {file = "unstract_sdk-0.30.0-py3-none-any.whl", hash = "sha256:f6d31ba9dd3b29ec6e21ff1760dea328c979290b0096fbc94e7c960c90ed837e"},- {file = "unstract_sdk-0.30.0.tar.gz", hash = "sha256:68f5b9c0747effff1d375bb32b3691f6e75ee7c5e66ccdefdf9f96b83c17ee64"},+ {file = "unstract_sdk-0.31.0-py3-none-any.whl", hash = "sha256:1a468e1f7272f45397d9d0592937049462096ee4c0f7f4486110f06bb3800b9e"},+ {file = "unstract_sdk-0.31.0.tar.gz", hash = "sha256:9230e2540bf5d5130bfd171d17ae7cb7a016dd74047449b521d1cf3a4c8cc8ab"},][[package]]diff --git a/prompt-service/pdm.lock b/prompt-service/pdm.lockindex 10ce384e6..0eb6581c2 100644--- a/prompt-service/pdm.lock+++ b/prompt-service/pdm.lock@@ -5,7 +5,7 @@groups = ["default", "deploy"]strategy = ["cross_platform", "inherit_metadata"]lock_version = "4.4.1"-content_hash = "sha256:756cc7348d34401d3339efd7b8480f6b78a3734617549e14445fc09f2ec9dc2e"+content_hash = "sha256:d25d7fe24972f9351f95ef5843d2b4c4ebf3725add3d47ab487bb41b91a86c3a"[[package]]name = "aiohttp"@@ -1187,7 +1187,7 @@ files = [[[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["default"]@@ -1199,29 +1199,29 @@ dependencies = ["sniffio",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "httpx"-version = "0.27.0"+version = "0.25.2"extras = ["http2"]requires_python = ">=3.8"summary = "The next generation HTTP client."groups = ["default"]dependencies = ["h2<5,>=3",- "httpx==0.27.0",+ "httpx==0.25.2",]files = [- {file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},- {file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},+ {file = "httpx-0.25.2-py3-none-any.whl", hash = "sha256:a05d3d052d9b2dfce0e3896636467f8a5342fb2b902c819428e1ac65413ca118"},+ {file = "httpx-0.25.2.tar.gz", hash = "sha256:8b8fcaa0c8ea7b05edd69a094e63a2094c4efcb48129fb757361bc423c0ad9e8"},][[package]]name = "huggingface-hub"-version = "0.23.2"+version = "0.23.3"requires_python = ">=3.8.0"summary = "Client library to download and publish models, datasets and other repos on the huggingface.co hub"groups = ["default"]@@ -1235,8 +1235,8 @@ dependencies = ["typing-extensions>=3.7.4.3",]files = [- {file = "huggingface_hub-0.23.2-py3-none-any.whl", hash = "sha256:48727a16e704d409c4bb5913613308499664f22a99743435dc3a13b23c485827"},- {file = "huggingface_hub-0.23.2.tar.gz", hash = "sha256:f6829b62d5fdecb452a76fdbec620cba4c1573655a8d710c1df71735fd9edbd2"},+ {file = "huggingface_hub-0.23.3-py3-none-any.whl", hash = "sha256:22222c41223f1b7c209ae5511d2d82907325a0e3cdbce5f66949d43c598ff3bc"},+ {file = "huggingface_hub-0.23.3.tar.gz", hash = "sha256:1a1118a0b3dea3bab6c325d71be16f5ffe441d32f3ac7c348d6875911b694b5b"},][[package]]@@ -1413,7 +1413,7 @@ files = [[[package]]name = "llama-index-core"-version = "0.10.43"+version = "0.10.43.post1"requires_python = "<4.0,>=3.8.1"summary = "Interface between LLMs and your data"groups = ["default"]@@ -1443,8 +1443,8 @@ dependencies = ["wrapt",]files = [- {file = "llama_index_core-0.10.43-py3-none-any.whl", hash = "sha256:355a5f7808dfa09dc27b6bd564926fa697e0279344b829732a9f91b8000157ac"},- {file = "llama_index_core-0.10.43.tar.gz", hash = "sha256:c7dce5158e112d3cbc8d331c303e830b2eabcae55c9636e8050dc85320522c1b"},+ {file = "llama_index_core-0.10.43.post1-py3-none-any.whl", hash = "sha256:673d0e7965b712150a1bc0bfff5512606dc1d8581fcfe617e18fd3ccd4e3d8b1"},+ {file = "llama_index_core-0.10.43.post1.tar.gz", hash = "sha256:fad9babac97172d0c6a3cd006fe48f0dfae88c481cf7d5ca5703dbaf8985ac54"},][[package]]@@ -1901,7 +1901,7 @@ files = [[[package]]name = "marshmallow"-version = "3.21.2"+version = "3.21.3"requires_python = ">=3.8"summary = "A lightweight library for converting complex datatypes to and from native Python datatypes."groups = ["default"]@@ -1909,8 +1909,8 @@ dependencies = ["packaging>=17.0",]files = [- {file = "marshmallow-3.21.2-py3-none-any.whl", hash = "sha256:70b54a6282f4704d12c0a41599682c5c5450e843b9ec406308653b47c59648a1"},- {file = "marshmallow-3.21.2.tar.gz", hash = "sha256:82408deadd8b33d56338d2182d455db632c6313aa2af61916672146bb32edc56"},+ {file = "marshmallow-3.21.3-py3-none-any.whl", hash = "sha256:86ce7fb914aa865001a4b2092c4c2872d13bc347f3d42673272cabfdbad386f1"},+ {file = "marshmallow-3.21.3.tar.gz", hash = "sha256:4f57c5e050a54d66361e826f94fba213eb10b67b2fdb02c3e0343ce207ba1662"},][[package]]@@ -1927,18 +1927,18 @@ files = [[[package]]name = "mistralai"-version = "0.3.0"+version = "0.4.0"requires_python = "<4.0,>=3.9"summary = ""groups = ["default"]dependencies = [- "httpx<1,>=0.25.2",- "orjson<4.0.0,>=3.9.10",+ "httpx<0.26,>=0.25",+ "orjson<3.11,>=3.9.10","pydantic<3.0.0,>=2.5.2",]files = [- {file = "mistralai-0.3.0-py3-none-any.whl", hash = "sha256:7aca38baf9008d6c690b750ccd78b27cf5b2b052c26649ccae5cc573bb6fb246"},- {file = "mistralai-0.3.0.tar.gz", hash = "sha256:61da3ab8d9f83251e9e5d8c8fd77c076511b5d83525b4d5550444c2eb00b0a7b"},+ {file = "mistralai-0.4.0-py3-none-any.whl", hash = "sha256:54d6a7ff03cd11214477ff6b9bc052e655ec7a07028a7dd0898cd852da71a220"},+ {file = "mistralai-0.4.0.tar.gz", hash = "sha256:1f21f02dec1fd6fefe12ca100d5937fd62542c11008c193d16df6f4a2d8554da"},][[package]]@@ -3473,7 +3473,7 @@ dependencies = [[[package]]name = "unstract-sdk"-version = "0.30.0"+version = "0.31.0"requires_python = "<3.11.1,>=3.9"summary = "A framework for writing Unstract Tools/Apps"groups = ["default"]@@ -3488,8 +3488,8 @@ dependencies = ["unstract-adapters~=0.18.0",]files = [- {file = "unstract_sdk-0.30.0-py3-none-any.whl", hash = "sha256:f6d31ba9dd3b29ec6e21ff1760dea328c979290b0096fbc94e7c960c90ed837e"},- {file = "unstract_sdk-0.30.0.tar.gz", hash = "sha256:68f5b9c0747effff1d375bb32b3691f6e75ee7c5e66ccdefdf9f96b83c17ee64"},+ {file = "unstract_sdk-0.31.0-py3-none-any.whl", hash = "sha256:1a468e1f7272f45397d9d0592937049462096ee4c0f7f4486110f06bb3800b9e"},+ {file = "unstract_sdk-0.31.0.tar.gz", hash = "sha256:9230e2540bf5d5130bfd171d17ae7cb7a016dd74047449b521d1cf3a4c8cc8ab"},][[package]]diff --git a/prompt-service/pyproject.toml b/prompt-service/pyproject.tomlindex 237a19deb..378cbc6c8 100644--- a/prompt-service/pyproject.toml+++ b/prompt-service/pyproject.toml@@ -15,7 +15,7 @@ dependencies = ["flask~=3.0","llama-index==0.10.28","python-dotenv==1.0.0",- "unstract-sdk~=0.30.0",+ "unstract-sdk~=0.31.0","redis>=5.0.3","unstract-core @ file:///${PROJECT_ROOT}/../unstract/core",]diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.pyindex 7a2b805c0..fb010b136 100644--- a/prompt-service/src/unstract/prompt_service/main.py+++ b/prompt-service/src/unstract/prompt_service/main.py@@ -33,6 +33,12 @@ from unstract.core.pubsub_helper import LogPublisherPOS_TEXT_PATH = "/tmp/pos.txt"USE_UNSTRACT_PROMPT = TrueMAX_RETRIES = 3+NO_CONTEXT_ERROR = (+ "Couldn't fetch context from vector DB. "+ "This happens usually due to a delay by the Vector DB "+ "provider to confirm writes to db. "+ "Please try again after some time"+)PG_BE_HOST = EnvLoader.get_env_or_die("PG_BE_HOST")PG_BE_PORT = EnvLoader.get_env_or_die("PG_BE_PORT")@@ -193,8 +199,7 @@ def prompt_processor() -> Any:promptx = output[PSKeys.PROMPT]chunk_size = output[PSKeys.CHUNK_SIZE]util = PromptServiceBaseTool(log_level=LogLevel.INFO, platform_key=platform_key)- tool_index = Index(tool=util)- adapter_instance_id = output[PSKeys.LLM]+ index = Index(tool=util)if is_active is False:app.logger.info(f"[{tool_id}] Skipping inactive prompt: {prompt_name}")@@ -231,7 +236,7 @@ def prompt_processor() -> Any:structured_output, variable_names, output, promptx)- doc_id = tool_index.generate_file_id(+ doc_id = index.generate_file_id(tool_id=tool_id,file_hash=file_hash,vector_db=output[PSKeys.VECTOR_DB],@@ -258,13 +263,13 @@ def prompt_processor() -> Any:llm = LLM(tool=util,adapter_instance_id=adapter_instance_id,- usage_kwargs=usage_kwargs,+ usage_kwargs=usage_kwargs.copy(),)embedding = Embedding(tool=util,adapter_instance_id=output[PSKeys.EMBEDDING],- usage_kwargs=usage_kwargs,+ usage_kwargs=usage_kwargs.copy(),)vector_db = VectorDB(@@ -287,96 +292,97 @@ def prompt_processor() -> Any:"Unable to obtain LLM / embedding / vectorDB",)return APIError(message=msg)+ try:+ vector_index = vector_db.get_vector_store_index()- vector_index = vector_db.get_vector_store_index()-- context = ""- if output[PSKeys.CHUNK_SIZE] == 0:- # We can do this only for chunkless indexes- context: Optional[str] = tool_index.get_text_from_index(- embedding_instance_id=output[PSKeys.EMBEDDING],- vector_db_instance_id=output[PSKeys.VECTOR_DB],- doc_id=doc_id,- usage_kwargs=usage_kwargs,- )- if not context:- # UN-1288 For Pinecone, we are seeing an inconsistent case where- # query with doc_id fails even though indexing just happened.- # This causes the following retrieve to return no text.- # To rule out any lag on the Pinecone vector DB write,- # the following sleep is added.- # Note: This will not fix the issue. Since this issue is inconsistent- # and not reproducible easily, this is just a safety net.- time.sleep(2)- context: Optional[str] = tool_index.get_text_from_index(+ context = ""+ if output[PSKeys.CHUNK_SIZE] == 0:+ # We can do this only for chunkless indexes+ context: Optional[str] = index.query_index(embedding_instance_id=output[PSKeys.EMBEDDING],vector_db_instance_id=output[PSKeys.VECTOR_DB],doc_id=doc_id,usage_kwargs=usage_kwargs,)- if context is None:- # TODO: Obtain user set name for vector DB- msg = "Couldn't fetch context from vector DB"- app.logger.error(- f"{msg} {output[PSKeys.VECTOR_DB]} for doc_id {doc_id}"- )- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.ERROR,- RunLevel.RUN,- msg,+ if not context:+ # UN-1288 For Pinecone, we are seeing an inconsistent case where+ # query with doc_id fails even though indexing just happened.+ # This causes the following retrieve to return no text.+ # To rule out any lag on the Pinecone vector DB write,+ # the following sleep is added.+ # Note: This will not fix the issue. Since this issue is+ # inconsistent, and not reproducible easily,+ # this is just a safety net.+ time.sleep(2)+ context: Optional[str] = index.query_index(+ embedding_instance_id=output[PSKeys.EMBEDDING],+ vector_db_instance_id=output[PSKeys.VECTOR_DB],+ doc_id=doc_id,+ usage_kwargs=usage_kwargs,)- raise APIError(message=msg)- # TODO: Use vectorDB name when available- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.DEBUG,- RunLevel.RUN,- "Fetched context from vector DB",- )+ if context is None:+ # TODO: Obtain user set name for vector DB+ msg = NO_CONTEXT_ERROR+ app.logger.error(+ f"{msg} {output[PSKeys.VECTOR_DB]} for doc_id {doc_id}"+ )+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.ERROR,+ RunLevel.RUN,+ msg,+ )+ raise APIError(message=msg)+ # TODO: Use vectorDB name when available+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.DEBUG,+ RunLevel.RUN,+ "Fetched context from vector DB",+ )- if chunk_size == 0:- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.RUN,- "Retrieving answer from LLM",- )- answer = construct_and_run_prompt(- tool_settings=tool_settings,- output=output,- llm=llm,- context=context,- prompt="promptx",- )- else:- answer = "NA"- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.RUN,- "Retrieving context from adapter",- )+ if chunk_size == 0:+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.INFO,+ RunLevel.RUN,+ "Retrieving answer from LLM",+ )+ answer = construct_and_run_prompt(+ tool_settings=tool_settings,+ output=output,+ llm=llm,+ context=context,+ prompt="promptx",+ )+ else:+ answer = "NA"+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.INFO,+ RunLevel.RUN,+ "Retrieving context from adapter",+ )retrieval_strategy = output.get(PSKeys.RETRIEVAL_STRATEGY)@@ -406,207 +412,161 @@ def prompt_processor() -> Any:"Retrieved context from adapter",)- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.RUN,- f"Processing prompt type: {output[PSKeys.TYPE]}",- )+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.INFO,+ RunLevel.RUN,+ f"Processing prompt type: {output[PSKeys.TYPE]}",+ )- if output[PSKeys.TYPE] == PSKeys.NUMBER:- if answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- # TODO: Extract these prompts as constants after pkging- prompt = f"Extract the number from the following \- text:\n{answer}\n\nOutput just the number. \- If the number is expressed in millions \- or thousands, expand the number to its numeric value \- The number should be directly assignable\- to a numeric variable.\- It should not have any commas, \- percentages or other grouping \- characters. No explanation is required.\- If you cannot extract the number, output 0."- answer = run_completion(- llm=llm,- prompt=prompt,- )- try:- structured_output[output[PSKeys.NAME]] = float(answer)- except Exception as e:- app.logger.info(- f"Error parsing response (to numeric, float): {e}",- LogLevel.ERROR,- )+ if output[PSKeys.TYPE] == PSKeys.NUMBER:+ if answer.lower() == "na":structured_output[output[PSKeys.NAME]] = None- elif output[PSKeys.TYPE] == PSKeys.EMAIL:- if answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- prompt = f'Extract the email from the following text:\n{answer}\n\nOutput just the email. \- The email should be directly assignable to a string variable. \- No explanation is required. If you cannot extract the email, output "NA".' # noqa- answer = run_completion(- llm=llm,- prompt=prompt,- )- structured_output[output[PSKeys.NAME]] = answer- elif output[PSKeys.TYPE] == PSKeys.DATE:- if answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- prompt = f'Extract the date from the following text:\n{answer}\n\nOutput just the date.\- The date should be in ISO date time format. No explanation is required. \- The date should be directly assignable to a date variable. \- If you cannot convert the string into a date, output "NA".' # noqa- answer = run_completion(- llm=llm,- prompt=prompt,- )- structured_output[output[PSKeys.NAME]] = answer-- elif output[PSKeys.TYPE] == PSKeys.BOOLEAN:- if answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- prompt = f'Extract yes/no from the following text:\n{answer}\n\n\- Output in single word.\- If the context is trying to convey that the answer is true, \- then return "yes", else return "no".'- answer = run_completion(- llm=llm,- prompt=prompt,- )- if answer.lower() == "yes":- structured_output[output[PSKeys.NAME]] = Trueelse:- structured_output[output[PSKeys.NAME]] = False- elif output[PSKeys.TYPE] == PSKeys.JSON:- if answer.lower() == "[]" or answer.lower() == "na":- structured_output[output[PSKeys.NAME]] = None- else:- prompt = f"Convert the following text into valid JSON string: \- \n{answer}\n\n The JSON string should be able to be parsed \- into a Python dictionary. \- Output just the JSON string. No explanation is required. \- If you cannot extract the JSON string, output {{}}"- answer = run_completion(- llm=llm,- prompt=prompt,- )- try:- structured_output[output[PSKeys.NAME]] = json.loads(answer)- except JSONDecodeError as e:- app.logger.info(f"JSON format error : {answer}", LogLevel.ERROR)- app.logger.info(- f"Error parsing response (to json): {e}", LogLevel.ERROR+ # TODO: Extract these prompts as constants after pkging+ prompt = f"Extract the number from the following \+ text:\n{answer}\n\nOutput just the number. \+ If the number is expressed in millions \+ or thousands, expand the number to its numeric value \+ The number should be directly assignable\+ to a numeric variable.\+ It should not have any commas, \+ percentages or other grouping \+ characters. No explanation is required.\+ If you cannot extract the number, output 0."+ answer = run_completion(+ llm=llm,+ prompt=prompt,)- structured_output[output[PSKeys.NAME]] = {}- else:- structured_output[output[PSKeys.NAME]] = answer-- # If there is a trailing '\n' remove it- if isinstance(structured_output[output[PSKeys.NAME]], str):- structured_output[output[PSKeys.NAME]] = structured_output[- output[PSKeys.NAME]- ].rstrip("\n")-- # Challenge condition- if tool_settings.get("enable_challenge"):- challenge_plugin: dict[str, Any] = plugins.get("challenge", {})- try:- if challenge_plugin:- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.CHALLENGE,- "Challenging response",+ try:+ structured_output[output[PSKeys.NAME]] = float(answer)+ except Exception as e:+ app.logger.info(+ f"Error parsing response (to numeric, float): {e}",+ LogLevel.ERROR,+ )+ structured_output[output[PSKeys.NAME]] = None+ elif output[PSKeys.TYPE] == PSKeys.EMAIL:+ if answer.lower() == "na":+ structured_output[output[PSKeys.NAME]] = None+ else:+ prompt = f'Extract the email from the following text:\n{answer}\n\nOutput just the email. \+ The email should be directly assignable to a string variable. \+ No explanation is required. If you cannot extract the email, output "NA".' # noqa+ answer = run_completion(+ llm=llm,+ prompt=prompt,)- challenge_llm = LLM(- tool=util,- adapter_instance_id=tool_settings[PSKeys.CHALLENGE_LLM],- usage_kwargs=usage_kwargs,+ structured_output[output[PSKeys.NAME]] = answer+ elif output[PSKeys.TYPE] == PSKeys.DATE:+ if answer.lower() == "na":+ structured_output[output[PSKeys.NAME]] = None+ else:+ prompt = f'Extract the date from the following text:\n{answer}\n\nOutput just the date.\+ The date should be in ISO date time format. No explanation is required. \+ The date should be directly assignable to a date variable. \+ If you cannot convert the string into a date, output "NA".' # noqa+ answer = run_completion(+ llm=llm,+ prompt=prompt,)- challenge = challenge_plugin["entrypoint_cls"](- challenge_llm=challenge_llm,- run_id=run_id,- context=context,- tool_settings=tool_settings,- output=output,- structured_output=structured_output,- logger=app.logger,- platform_key=platform_key,+ structured_output[output[PSKeys.NAME]] = answer++ elif output[PSKeys.TYPE] == PSKeys.BOOLEAN:+ if answer.lower() == "na":+ structured_output[output[PSKeys.NAME]] = None+ else:+ prompt = f'Extract yes/no from the following text:\n{answer}\n\n\+ Output in single word.\+ If the context is trying to convey that the answer is true, \+ then return "yes", else return "no".'+ answer = run_completion(+ llm=llm,+ prompt=prompt,)- # Will inline replace the structured output passed.- challenge.run()+ if answer.lower() == "yes":+ structured_output[output[PSKeys.NAME]] = True+ else:+ structured_output[output[PSKeys.NAME]] = False+ elif output[PSKeys.TYPE] == PSKeys.JSON:+ if answer.lower() == "[]" or answer.lower() == "na":+ structured_output[output[PSKeys.NAME]] = Noneelse:- app.logger.info(- "No challenge plugin found to evaluate prompt: %s",- output["name"],+ prompt = f"Convert the following text into valid JSON string: \+ \n{answer}\n\n The JSON string should be able to be parsed \+ into a Python dictionary. \+ Output just the JSON string. No explanation is required. \+ If you cannot extract the JSON string, output {{}}"+ answer = run_completion(+ llm=llm,+ prompt=prompt,)- except challenge_plugin["exception_cls"] as e:- app.logger.error(- "Failed to challenge prompt %s: %s", output["name"], str(e)- )- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.ERROR,- RunLevel.CHALLENGE,- "Error while challenging response",- )+ try:+ structured_output[output[PSKeys.NAME]] = json.loads(answer)+ except JSONDecodeError as e:+ app.logger.info(f"JSON format error : {answer}", LogLevel.ERROR)+ app.logger.info(+ f"Error parsing response (to json): {e}",+ LogLevel.ERROR,+ )+ structured_output[output[PSKeys.NAME]] = {}+ else:+ structured_output[output[PSKeys.NAME]] = answer- #- # Evaluate the prompt.- #- if (- PSKeys.EVAL_SETTINGS in output- and output[PSKeys.EVAL_SETTINGS][PSKeys.EVAL_SETTINGS_EVALUATE]- ):- eval_plugin: dict[str, Any] = plugins.get("evaluation", {})- if eval_plugin:- _publish_log(- log_events_id,- {- "tool_id": tool_id,- "prompt_key": prompt_name,- "doc_name": doc_name,- },- LogLevel.INFO,- RunLevel.EVAL,- "Evaluating response",- )+ # If there is a trailing '\n' remove it+ if isinstance(structured_output[output[PSKeys.NAME]], str):+ structured_output[output[PSKeys.NAME]] = structured_output[+ output[PSKeys.NAME]+ ].rstrip("\n")++ # Challenge condition+ if tool_settings.get("enable_challenge"):+ challenge_plugin: dict[str, Any] = plugins.get("challenge", {})try:- evaluator = eval_plugin["entrypoint_cls"](- "",- context,- "",- "",- output,- structured_output,- app.logger,- platform_key,- )- # Will inline replace the structured output passed.- evaluator.run()- except eval_plugin["exception_cls"] as e:+ if challenge_plugin:+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.INFO,+ RunLevel.CHALLENGE,+ "Challenging response",+ )+ challenge_llm = LLM(+ tool=util,+ adapter_instance_id=tool_settings[PSKeys.CHALLENGE_LLM],+ usage_kwargs=usage_kwargs,+ )+ challenge = challenge_plugin["entrypoint_cls"](+ challenge_llm=challenge_llm,+ run_id=run_id,+ context=context,+ tool_settings=tool_settings,+ output=output,+ structured_output=structured_output,+ logger=app.logger,+ platform_key=platform_key,+ )+ # Will inline replace the structured output passed.+ challenge.run()+ else:+ app.logger.info(+ "No challenge plugin found to evaluate prompt: %s",+ output["name"],+ )+ except challenge_plugin["exception_cls"] as e:app.logger.error(- f'Failed to evaluate prompt {output["name"]}: {str(e)}'+ "Failed to challenge prompt %s: %s",+ output["name"],+ str(e),)_publish_log(log_events_id,@@ -616,10 +576,19 @@ def prompt_processor() -> Any:"doc_name": doc_name,},LogLevel.ERROR,- RunLevel.EVAL,- "Error while evaluation",+ RunLevel.CHALLENGE,+ "Error while challenging response",)- else:++ #+ # Evaluate the prompt.+ #+ if (+ PSKeys.EVAL_SETTINGS in output+ and output[PSKeys.EVAL_SETTINGS][PSKeys.EVAL_SETTINGS_EVALUATE]+ ):+ eval_plugin: dict[str, Any] = plugins.get("evaluation", {})+ if eval_plugin:_publish_log(log_events_id,{@@ -627,15 +596,56 @@ def prompt_processor() -> Any:"prompt_key": prompt_name,"doc_name": doc_name,},- LogLevel.DEBUG,+ LogLevel.INFO,RunLevel.EVAL,- "Evaluation completed",+ "Evaluating response",)- else:- app.logger.info(- f'No eval plugin found to evaluate prompt: {output["name"]}' # noqa: E501- )-+ try:+ evaluator = eval_plugin["entrypoint_cls"](+ "",+ context,+ "",+ "",+ output,+ structured_output,+ app.logger,+ platform_key,+ )+ # Will inline replace the structured output passed.+ evaluator.run()+ except eval_plugin["exception_cls"] as e:+ app.logger.error(+ f'Failed to evaluate prompt {output["name"]}: {str(e)}'+ )+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.ERROR,+ RunLevel.EVAL,+ "Error while evaluation",+ )+ else:+ _publish_log(+ log_events_id,+ {+ "tool_id": tool_id,+ "prompt_key": prompt_name,+ "doc_name": doc_name,+ },+ LogLevel.DEBUG,+ RunLevel.EVAL,+ "Evaluation completed",+ )+ else:+ app.logger.info(+ f'No eval plugin found to evaluate prompt: {output["name"]}' # noqa: E501+ )+ finally:+ vector_db.close()_publish_log(log_events_id,{"tool_id": tool_id, "doc_name": doc_name},@@ -764,7 +774,7 @@ def run_completion(prompt: str,) -> tuple[str, dict[str, Any]]:try:- completion = llm.run_completion(prompt)+ completion = llm.complete(prompt)answer: str = completion[PSKeys.RESPONSE].textreturn answerdiff --git a/pyproject.toml b/pyproject.tomlindex 0b220743a..ad39b5a05 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -40,7 +40,7 @@ hook-check-django-migrations = ["psycopg2-binary==2.9.9","python-dotenv==1.0.0","python-magic==0.4.27",- "unstract-sdk~=0.30.0",+ "unstract-sdk~=0.31.0","unstract-adapters~=0.18.0","-e unstract-connectors @ file:///${PROJECT_ROOT}/unstract/connectors","-e unstract-core @ file:///${PROJECT_ROOT}/unstract/core",diff --git a/tools/classifier/requirements.txt b/tools/classifier/requirements.txtindex fa65c7aa3..ba85517ba 100644--- a/tools/classifier/requirements.txt+++ b/tools/classifier/requirements.txt@@ -1,4 +1,4 @@# Add your dependencies here# Required for all unstract tools-unstract-sdk~=0.26.0+unstract-sdk~=0.31.0diff --git a/tools/classifier/src/config/properties.json b/tools/classifier/src/config/properties.jsonindex 45c13ff63..cb665a259 100644--- a/tools/classifier/src/config/properties.json+++ b/tools/classifier/src/config/properties.json@@ -2,7 +2,7 @@"schemaVersion": "0.0.1","displayName": "File Classifier","functionName": "classify",- "toolVersion": "0.0.18",+ "toolVersion": "0.0.19","description": "Classifies a file into a bin based on its contents","input": {"description": "File to be classified"diff --git a/tools/classifier/src/helper.py b/tools/classifier/src/helper.pyindex a57e4c2a5..f3906fcc2 100644--- a/tools/classifier/src/helper.py+++ b/tools/classifier/src/helper.py@@ -1,8 +1,8 @@from typing import Any, Optional-from llama_index.core.llms import LLMfrom unstract.sdk.cache import ToolCachefrom unstract.sdk.constants import ToolEnv+from unstract.sdk.llm import LLMfrom unstract.sdk.tool.base import BaseToolfrom unstract.sdk.utils import ToolUtilsfrom unstract.sdk.x2txt import X2Text@@ -42,14 +42,13 @@ class ClassifierHelper:self.tool.stream_log("Creating text extraction adapter " f"using adapter_id: {adapter_id}")- tool_extraction = X2Text(tool=self.tool)- adapter = tool_extraction.get_x2text(adapter_id)+ x2text = X2Text(tool=self.tool, adapter_instance_id=adapter_id)self.tool.stream_log("Text extraction adapter has been created successfully.")self.tool.stream_log("Adapter created")try:- extracted_text: str = adapter.process(input_file_path=file)+ extracted_text: str = x2text.process(input_file_path=file)return extracted_textexcept Exception as e:self.tool.stream_log(f"Adapter error: {e}")@@ -130,9 +129,9 @@ class ClassifierHelper:str: Classification"""try:- response = llm.complete(prompt, max_tokens=50, stop=["\n"])- classification: str = response.text.strip()- self.tool.stream_log(f"LLM response: {response}")+ completion = llm.complete(prompt, max_tokens=50, stop=["\n"])[LLM.RESPONSE]+ classification: str = completion.text.strip()+ self.tool.stream_log(f"LLM response: {completion}")return classificationexcept Exception as e:self.tool.stream_error_and_exit(f"Error calling LLM {e}")diff --git a/tools/classifier/src/main.py b/tools/classifier/src/main.pyindex 39300629b..c1dfb9453 100644--- a/tools/classifier/src/main.py+++ b/tools/classifier/src/main.py@@ -4,17 +4,11 @@ from pathlib import Pathfrom typing import Any, Optionalfrom helper import ClassifierHelper # type: ignore-from unstract.sdk.constants import (- LogLevel,- LogState,- MetadataKey,- ToolEnv,- ToolSettingsKey,-)-from unstract.sdk.llm import ToolLLM+from unstract.sdk.constants import LogLevel, LogState, MetadataKey, ToolSettingsKey+from unstract.sdk.exceptions import SdkError+from unstract.sdk.llm import LLMfrom unstract.sdk.tool.base import BaseToolfrom unstract.sdk.tool.entrypoint import ToolEntrypoint-from unstract.sdk.utils.callback_manager import CallbackManager as UNCallbackManagerclass UnstractClassifier(BaseTool):@@ -78,22 +72,22 @@ class UnstractClassifier(BaseTool):bins.append("unknown")bins_with_quotes = [f"'{b}'" for b in bins]- tool_llm = ToolLLM(tool=self)- llm = tool_llm.get_llm(adapter_instance_id=llm_adapter_instance_id)- if not llm:+ usage_kwargs: dict[Any, Any] = dict()+ usage_kwargs["workflow_id"] = self.workflow_id+ usage_kwargs["execution_id"] = self.execution_id++ try:+ llm = LLM(+ tool=self,+ adapter_instance_id=llm_adapter_instance_id,+ usage_kwargs=usage_kwargs,+ )+ except SdkError:self.stream_error_and_exit("Unable to get llm instance")- # Setting the callback_manager to be used- UNCallbackManager.set_callback_manager(- platform_api_key=self.get_env_or_die(ToolEnv.PLATFORM_API_KEY),- llm=llm,- workflow_id=self.workflow_id,- execution_id=self.execution_id,- adapter_instance_id=llm_adapter_instance_id,- )- max_tokens = tool_llm.get_max_tokens(reserved_for_output=50 + 1000)++ max_tokens = llm.get_max_tokens(reserved_for_output=50 + 1000)max_bytes = int(max_tokens * 1.3)self.stream_log(f"LLM Max tokens: {max_tokens} ==> Max bytes: {max_bytes}")- self.stream_log(f"LLM Max tokens: {max_tokens} ==> Max bytes: {max_bytes}")limited_text = ""for byte in text.encode():if len(limited_text.encode()) < max_bytes:diff --git a/tools/structure/requirements.txt b/tools/structure/requirements.txtindex 1cf39ef9d..ba85517ba 100644--- a/tools/structure/requirements.txt+++ b/tools/structure/requirements.txt@@ -1,4 +1,4 @@# Add your dependencies here# Required for all unstract tools-unstract-sdk~=0.29.0+unstract-sdk~=0.31.0diff --git a/tools/structure/src/config/properties.json b/tools/structure/src/config/properties.jsonindex b4b9efc4c..38c2a430a 100644--- a/tools/structure/src/config/properties.json+++ b/tools/structure/src/config/properties.json@@ -2,7 +2,7 @@"schemaVersion": "0.0.1","displayName": "Structure Tool","functionName": "structure_tool",- "toolVersion": "0.0.20",+ "toolVersion": "0.0.23","description": "This is a template tool which can answer set of input prompts designed in the Prompt Studio","input": {"description": "File that needs to be indexed and parsed for answers"diff --git a/tools/structure/src/main.py b/tools/structure/src/main.pyindex 51dee300d..5fe1aadb3 100644--- a/tools/structure/src/main.py+++ b/tools/structure/src/main.py@@ -11,6 +11,7 @@ from unstract.sdk.prompt import PromptToolfrom unstract.sdk.tool.base import BaseToolfrom unstract.sdk.tool.entrypoint import ToolEntrypointfrom unstract.sdk.utils import ToolUtils+from unstract.sdk.utils.common_utils import CommonUtilsclass StructureTool(BaseTool):@@ -79,8 +80,11 @@ class StructureTool(BaseTool):# TODO: Need to split extraction and indexing# to avoid unwanted indexingself.stream_log("Indexing document")+ usage_kwargs: dict[Any, Any] = dict()+ run_id = CommonUtils.generate_uuid()+ usage_kwargs["run_id"] = run_idif tool_settings[SettingsKeys.ENABLE_SINGLE_PASS_EXTRACTION]:- index.index_file(+ index.index(tool_id=tool_metadata[SettingsKeys.TOOL_ID],embedding_instance_id=tool_settings[SettingsKeys.EMBEDDING],vector_db_instance_id=tool_settings[SettingsKeys.VECTOR_DB],@@ -91,6 +95,7 @@ class StructureTool(BaseTool):chunk_overlap=tool_settings[SettingsKeys.CHUNK_OVERLAP],output_file_path=tool_data_dir / SettingsKeys.EXTRACT,reindex=True,+ usage_kwargs=usage_kwargs,)if summarize_as_source:summarize_file_hash = self._summarize_and_index(@@ -100,6 +105,7 @@ class StructureTool(BaseTool):responder=responder,outputs=outputs,index=index,+ usage_kwargs=usage_kwargs,)payload[SettingsKeys.FILE_HASH] = summarize_file_hashself.stream_log("Fetching response for single pass extraction")@@ -110,7 +116,7 @@ class StructureTool(BaseTool):# indexed to get the output in required pathreindex = Truefor output in outputs:- index.index_file(+ index.index(tool_id=tool_metadata[SettingsKeys.TOOL_ID],embedding_instance_id=output[SettingsKeys.EMBEDDING],vector_db_instance_id=output[SettingsKeys.VECTOR_DB],@@ -121,6 +127,7 @@ class StructureTool(BaseTool):chunk_overlap=output[SettingsKeys.CHUNK_OVERLAP],output_file_path=tool_data_dir / SettingsKeys.EXTRACT,reindex=reindex,+ usage_kwargs=usage_kwargs,)if summarize_as_source:summarize_file_hash = self._summarize_and_index(@@ -130,6 +137,7 @@ class StructureTool(BaseTool):responder=responder,outputs=outputs,index=index,+ usage_kwargs=usage_kwargs,)payload[SettingsKeys.FILE_HASH] = summarize_file_hash# For summary indexing should be done@@ -180,6 +188,7 @@ class StructureTool(BaseTool):responder: PromptTool,outputs: dict[str, Any],index: Index,+ usage_kwargs: dict[Any, Any] = {},) -> str:"""Summarizes the context of the file and indexes the summarizedcontent.@@ -225,7 +234,7 @@ class StructureTool(BaseTool):summarize_file_hash: str = ToolUtils.get_hash_from_file(file_path=summarize_file_path)- index.index_file(+ index.index(tool_id=tool_id,embedding_instance_id=tool_settings[SettingsKeys.EMBEDDING],vector_db_instance_id=tool_settings[SettingsKeys.VECTOR_DB],@@ -234,6 +243,7 @@ class StructureTool(BaseTool):file_hash=summarize_file_hash,chunk_size=0,chunk_overlap=0,+ usage_kwargs=usage_kwargs,)return summarize_file_hashdiff --git a/tools/text_extractor/requirements.txt b/tools/text_extractor/requirements.txtindex fa65c7aa3..ba85517ba 100644--- a/tools/text_extractor/requirements.txt+++ b/tools/text_extractor/requirements.txt@@ -1,4 +1,4 @@# Add your dependencies here# Required for all unstract tools-unstract-sdk~=0.26.0+unstract-sdk~=0.31.0diff --git a/tools/text_extractor/src/config/properties.json b/tools/text_extractor/src/config/properties.jsonindex 6c9aa83e0..21fa9c569 100644--- a/tools/text_extractor/src/config/properties.json+++ b/tools/text_extractor/src/config/properties.json@@ -2,7 +2,7 @@"schemaVersion": "0.0.1","displayName": "Text Extractor","functionName": "text_extractor",- "toolVersion": "0.0.16",+ "toolVersion": "0.0.18","description": "The Text Extractor is a powerful tool designed to convert documents to its text form or Extract texts from documents","input": {"description": "Document"diff --git a/tools/text_extractor/src/main.py b/tools/text_extractor/src/main.pyindex 41ec9cfc8..bea79015c 100644--- a/tools/text_extractor/src/main.py+++ b/tools/text_extractor/src/main.py@@ -54,9 +54,8 @@ class TextExtractor(BaseTool):input_log = f"Processing file: \n\n`{source_name}`"self.stream_update(input_log, state=LogState.INPUT_UPDATE)- tool_extraction = X2Text(tool=self)- text_extraction_adapter = tool_extraction.get_x2text(- adapter_instance_id=text_extraction_adapter_id+ text_extraction_adapter = X2Text(+ tool=self, adapter_instance_id=text_extraction_adapter_id)self.stream_log("Text extraction adapter has been created successfully.")extracted_text = text_extraction_adapter.process(input_file_path=input_file)diff --git a/unstract/tool-registry/src/unstract/tool_registry/public_tools.json b/unstract/tool-registry/src/unstract/tool_registry/public_tools.jsonindex abcb66983..101ffd38c 100644--- a/unstract/tool-registry/src/unstract/tool_registry/public_tools.json+++ b/unstract/tool-registry/src/unstract/tool_registry/public_tools.json@@ -5,7 +5,7 @@"schemaVersion": "0.0.1","displayName": "File Classifier","functionName": "classify",- "toolVersion": "0.0.17",+ "toolVersion": "0.0.19","description": "Classifies a file into a bin based on its contents","input": {"description": "File to be classified"@@ -139,9 +139,9 @@}},"icon": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg\n enable-background=\"new 0 0 20 20\"\n height=\"48\"\n viewBox=\"0 0 20 20\"\n width=\"48\"\n fill=\"#000000\"\n version=\"1.1\"\n id=\"svg8109\"\n sodipodi:docname=\"folder_copy_black_48dp.svg\"\n xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n xmlns=\"http://www.w3.org/2000/svg\"\n xmlns:svg=\"http://www.w3.org/2000/svg\">\n <defs\n id=\"defs8113\" />\n <sodipodi:namedview\n id=\"namedview8111\"\n pagecolor=\"#ffffff\"\n bordercolor=\"#000000\"\n borderopacity=\"0.25\"\n inkscape:showpageshadow=\"2\"\n inkscape:pageopacity=\"0.0\"\n inkscape:pagecheckerboard=\"0\"\n inkscape:deskcolor=\"#d1d1d1\"\n showgrid=\"false\" />\n <g\n id=\"g8099\">\n <rect\n fill=\"none\"\n height=\"20\"\n width=\"20\"\n x=\"0\"\n id=\"rect8097\"\n y=\"0\" />\n </g>\n <g\n id=\"g8107\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <g\n id=\"g8105\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <path\n d=\"M 2.5,5 H 1 V 15.5 C 1,16.33 1.67,17 2.5,17 H 15.68 V 15.5 H 2.5 Z\"\n id=\"path8101\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n <path\n d=\"M 16.5,4 H 11 L 9,2 H 5.5 C 4.67,2 4,2.67 4,3.5 v 9 C 4,13.33 4.67,14 5.5,14 h 11 c 0.83,0 1.5,-0.67 1.5,-1.5 v -7 C 18,4.67 17.33,4 16.5,4 Z m 0,8.5 h -11 v -9 h 2.88 l 2,2 h 6.12 z\"\n id=\"path8103\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n </g>\n </g>\n</svg>\n",- "image_url": "docker:unstract/tool-classifier:0.0.17",+ "image_url": "docker:unstract/tool-classifier:0.0.19","image_name": "unstract/tool-classifier",- "image_tag": "0.0.17"+ "image_tag": "0.0.19"},"text_extractor": {"tool_uid": "text_extractor",@@ -149,7 +149,7 @@"schemaVersion": "0.0.1","displayName": "Text Extractor","functionName": "text_extractor",- "toolVersion": "0.0.16",+ "toolVersion": "0.0.18","description": "The Text Extractor is a powerful tool designed to convert documents to its text form or Extract texts from documents","input": {"description": "Document"@@ -224,8 +224,8 @@}},"icon": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg\n enable-background=\"new 0 0 20 20\"\n height=\"48\"\n viewBox=\"0 0 20 20\"\n width=\"48\"\n fill=\"#000000\"\n version=\"1.1\"\n id=\"svg8109\"\n sodipodi:docname=\"folder_copy_black_48dp.svg\"\n xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n xmlns=\"http://www.w3.org/2000/svg\"\n xmlns:svg=\"http://www.w3.org/2000/svg\">\n <defs\n id=\"defs8113\" />\n <sodipodi:namedview\n id=\"namedview8111\"\n pagecolor=\"#ffffff\"\n bordercolor=\"#000000\"\n borderopacity=\"0.25\"\n inkscape:showpageshadow=\"2\"\n inkscape:pageopacity=\"0.0\"\n inkscape:pagecheckerboard=\"0\"\n inkscape:deskcolor=\"#d1d1d1\"\n showgrid=\"false\" />\n <g\n id=\"g8099\">\n <rect\n fill=\"none\"\n height=\"20\"\n width=\"20\"\n x=\"0\"\n id=\"rect8097\"\n y=\"0\" />\n </g>\n <g\n id=\"g8107\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <g\n id=\"g8105\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <path\n d=\"M 2.5,5 H 1 V 15.5 C 1,16.33 1.67,17 2.5,17 H 15.68 V 15.5 H 2.5 Z\"\n id=\"path8101\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n <path\n d=\"M 16.5,4 H 11 L 9,2 H 5.5 C 4.67,2 4,2.67 4,3.5 v 9 C 4,13.33 4.67,14 5.5,14 h 11 c 0.83,0 1.5,-0.67 1.5,-1.5 v -7 C 18,4.67 17.33,4 16.5,4 Z m 0,8.5 h -11 v -9 h 2.88 l 2,2 h 6.12 z\"\n id=\"path8103\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n </g>\n </g>\n</svg>\n",- "image_url": "docker:unstract/tool-text-extractor:0.0.16",+ "image_url": "docker:unstract/tool-text-extractor:0.0.18","image_name": "unstract/tool-text-extractor",- "image_tag": "0.0.16"+ "image_tag": "0.0.18"}}