Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

[FIX] Changes to improve json retrieval (#1306)

Deepak K550d6be3bd7e
diff --git a/prompt-service/pyproject.toml b/prompt-service/pyproject.toml
index d5a41769d..8efc60c5f 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~=0.71.0",
+ "unstract-sdk~=0.71.1",
"gcsfs==2024.10.0",
"s3fs==2024.10.0",
"redis>=5.0.3,<5.3",
diff --git a/prompt-service/src/unstract/prompt_service/constants.py b/prompt-service/src/unstract/prompt_service/constants.py
index 6107dfd02..ef8c7108d 100644
--- a/prompt-service/src/unstract/prompt_service/constants.py
+++ b/prompt-service/src/unstract/prompt_service/constants.py
@@ -93,6 +93,8 @@ class PromptServiceConstants:
)
COMBINED_PROMPT = "combined_prompt"
TOOL = "tool"
+ JSON_POSTAMBLE = "JSON_POSTAMBLE"
+ DEFAULT_JSON_POSTAMBLE = "Wrap the final JSON result inbetween ### like below example:\n###\n<FINAL_JSON_RESULT>\n###"
class RunLevel(Enum):
diff --git a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
index 3c82b7643..85f400780 100644
--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
@@ -10,6 +10,7 @@ from unstract.prompt_service.constants import ExecutionSource, FileStorageKeys,
from unstract.prompt_service.constants import PromptServiceConstants as PSKeys
from unstract.prompt_service.exceptions import RateLimitError
from unstract.prompt_service.helpers.plugin import PluginManager
+from unstract.prompt_service.utils.env_loader import get_env_or_die
from unstract.prompt_service.utils.log import publish_log
from unstract.sdk.constants import LogLevel
from unstract.sdk.exceptions import RateLimitError as SdkRateLimitError
@@ -59,6 +60,7 @@ class AnswerPromptService:
platform_postamble = tool_settings.get(PSKeys.PLATFORM_POSTAMBLE, "")
summarize_as_source = tool_settings.get(PSKeys.SUMMARIZE_AS_SOURCE)
enable_highlight = tool_settings.get(PSKeys.ENABLE_HIGHLIGHT, False)
+ prompt_type = output.get(PSKeys.TYPE, PSKeys.TEXT)
if not enable_highlight or summarize_as_source:
platform_postamble = ""
plugin = PluginManager().get_plugin("json-extraction")
@@ -71,6 +73,7 @@ class AnswerPromptService:
grammar_list=tool_settings.get(PSKeys.GRAMMAR, []),
context=context,
platform_postamble=platform_postamble,
+ prompt_type=prompt_type,
)
output[PSKeys.COMBINED_PROMPT] = prompt
return AnswerPromptService.run_completion(
@@ -78,7 +81,7 @@ class AnswerPromptService:
prompt=prompt,
metadata=metadata,
prompt_key=output[PSKeys.NAME],
- prompt_type=output.get(PSKeys.TYPE, PSKeys.TEXT),
+ prompt_type=prompt_type,
enable_highlight=enable_highlight,
file_path=file_path,
execution_source=execution_source,
@@ -92,6 +95,7 @@ class AnswerPromptService:
grammar_list: list[dict[str, Any]],
context: str,
platform_postamble: str,
+ prompt_type: str = PSKeys.TEXT,
) -> str:
prompt = f"{preamble}\n\nQuestion or Instruction: {prompt}"
if grammar_list is not None and len(grammar_list) > 0:
@@ -106,6 +110,11 @@ class AnswerPromptService:
if len(synonyms) > 0 and word != "":
prompt += f'\nNote: You can consider that the word {word} is same as \
{", ".join(synonyms)} in both the quesiton and the context.' # noqa
+ if prompt_type == PSKeys.JSON:
+ json_postamble = get_env_or_die(
+ PSKeys.JSON_POSTAMBLE, PSKeys.DEFAULT_JSON_POSTAMBLE
+ )
+ postamble += f"\n{json_postamble}"
if platform_postamble:
platform_postamble += "\n\n"
prompt += (
diff --git a/prompt-service/uv.lock b/prompt-service/uv.lock
index 22c87f64f..25f5b18d6 100644
--- a/prompt-service/uv.lock
+++ b/prompt-service/uv.lock
@@ -1216,15 +1216,16 @@ wheels = [
[[package]]
name = "kombu"
-version = "5.3.7"
+version = "5.5.3"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "amqp" },
+ { name = "tzdata" },
{ name = "vine" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/99/3a/2fb09708fef243e35c286414f2bf78543dc311ae7d3de5d343bd8437e38d/kombu-5.3.7.tar.gz", hash = "sha256:011c4cd9a355c14a1de8d35d257314a1d2456d52b7140388561acac3cf1a97bf", size = 439344 }
+sdist = { url = "https://files.pythonhosted.org/packages/60/0a/128b65651ed8120460fc5af754241ad595eac74993115ec0de4f2d7bc459/kombu-5.5.3.tar.gz", hash = "sha256:021a0e11fcfcd9b0260ef1fb64088c0e92beb976eb59c1dfca7ddd4ad4562ea2", size = 461784 }
wheels = [
- { url = "https://files.pythonhosted.org/packages/b4/9a/1951f2261271d6994f0df5a55b3e9cdad42ed1fc3020a0dc7f6de80a4566/kombu-5.3.7-py3-none-any.whl", hash = "sha256:5634c511926309c7f9789f1433e9ed402616b56836ef9878f01bd59267b4c7a9", size = 200190 },
+ { url = "https://files.pythonhosted.org/packages/5d/35/1407fb0b2f5b07b50cbaf97fce09ad87d3bfefbf64f7171a8651cd8d2f68/kombu-5.5.3-py3-none-any.whl", hash = "sha256:5b0dbceb4edee50aa464f59469d34b97864be09111338cfb224a10b6a163909b", size = 209921 },
]
[[package]]
@@ -2983,7 +2984,7 @@ dependencies = [
[package.metadata]
requires-dist = [
{ name = "flask", marker = "extra == 'flask'", specifier = "~=3.1.0" },
- { name = "kombu", specifier = "==5.3.7" },
+ { name = "kombu", specifier = "~=5.5.3" },
{ name = "redis", specifier = "~=5.2.1" },
{ name = "requests", specifier = "==2.31.0" },
]
@@ -3053,7 +3054,7 @@ requires-dist = [
{ name = "s3fs", specifier = "==2024.10.0" },
{ name = "unstract-core", editable = "../unstract/core" },
{ name = "unstract-flags", editable = "../unstract/flags" },
- { name = "unstract-sdk", specifier = "~=0.71.0" },
+ { name = "unstract-sdk", specifier = "~=0.71.1" },
]
[package.metadata.requires-dev]
@@ -3073,7 +3074,7 @@ test = [
[[package]]
name = "unstract-sdk"
-version = "0.71.0"
+version = "0.71.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "filetype" },
@@ -3112,9 +3113,9 @@ dependencies = [
{ name = "tiktoken" },
{ name = "transformers" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/d0/12/efecbf0581040b1bfc44fe939a68ba07e45c65c16b3e91666227e94b52be/unstract_sdk-0.71.0.tar.gz", hash = "sha256:c9269cbd1656e40a03445d3758bc51d7921273af729e0d3813f5f9e28967e31d", size = 2360756 }
+sdist = { url = "https://files.pythonhosted.org/packages/ac/f9/49ccd72d7b6cc398d13892fa8c96dfe26e26ed305832dc4385ce6bf19c98/unstract_sdk-0.71.1.tar.gz", hash = "sha256:246b11528df349919f9b66588b2167a74d5c522d00a42c98773d75f590e9edf0", size = 2360839 }
wheels = [
- { url = "https://files.pythonhosted.org/packages/3a/88/4ba63cd8611d7bdaf36619e9fe1b19effb5bc10a13ec50b79bf714aa410a/unstract_sdk-0.71.0-py3-none-any.whl", hash = "sha256:0d3bdaf79468f03991f457d040b07ab5d159be3a601b4bfae7f772083d70b705", size = 265087 },
+ { url = "https://files.pythonhosted.org/packages/c4/ad/ae8bc00b5f83747c72d3719c1330637b7cf05e3b2e79ddb258fe4cf8aa47/unstract_sdk-0.71.1-py3-none-any.whl", hash = "sha256:d1c076c1ed7b3d2a6d38f40f4a9cf366edc9a4966d4bd1d6a042e550d3113be0", size = 265172 },
]
[[package]]