Skip to content

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

Fixed high token usage for json type (#397)

Deepak K42aa2152c97b
diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.py
index decce974d..c2a1fc6ab 100644
--- a/prompt-service/src/unstract/prompt_service/main.py
+++ b/prompt-service/src/unstract/prompt_service/main.py
@@ -497,24 +497,29 @@ def prompt_processor() -> Any:
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,
- )
- structured_output[output[PSKeys.NAME]] = {}
+ except JSONDecodeError:
+ 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 {{}}"
+ try:
+ answer = run_completion(
+ llm=llm,
+ prompt=prompt,
+ )
+ 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
@@ -546,6 +551,7 @@ def prompt_processor() -> Any:
usage_kwargs=usage_kwargs,
)
challenge = challenge_plugin["entrypoint_cls"](
+ llm=llm,
challenge_llm=challenge_llm,
run_id=run_id,
context=context,