Skip to content

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

[FIX] Enhancing Subquestion Retrieval for Prompt Studio (#634)

harini-venkataraman24ca821bb889
diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.py
index 14d24ecbe..c9880c444 100644
--- a/prompt-service/src/unstract/prompt_service/main.py
+++ b/prompt-service/src/unstract/prompt_service/main.py
@@ -692,29 +692,56 @@ def run_retrieval( # type:ignore
retrieval_type: str,
metadata: dict[str, Any],
) -> tuple[str, str]:
+ context: str = ""
prompt = output[PSKeys.PROMPTX]
if retrieval_type == PSKeys.SUBQUESTION:
- subq_prompt = (
- f"Generate a sub-question from the following verbose prompt that will"
- f" help extract relevant documents from a vector store:\n\n{prompt}"
+ subq_prompt: str = (
+ f"I am sending you a verbose prompt \n \n Prompt : {prompt} \n \n"
+ "Generate set of specific subquestions "
+ "from the prompt which can be used to retrive "
+ "relevant context from vector db. "
+ "Ensure that each subquestion is distinct and relevant"
+ "to the different facets of the original query. Do not"
+ "add subquestions for details not mentioned in the original "
+ "prompt. The goal is to maximize retrieval accuracy"
+ " using these subquestions. Please note that, there are cases where the "
+ "response might have a list of answers. The subquestions must not miss out "
+ "any values in these cases. "
+ "Output should be a list of comma seperated "
+ "subquestion prompts. Do not change this format. \n \n "
+ " Subquestions : "
)
- prompt = run_completion(
+ subquestions = run_completion(
llm=llm,
prompt=subq_prompt,
)
- context = _retrieve_context(output, doc_id, vector_index, prompt)
-
- 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)
+ subquestion_list = subquestions.split(",")
+ for each_subq in subquestion_list:
+ retrieved_context = _retrieve_context(
+ output, doc_id, vector_index, each_subq
+ )
+ # Not adding the potential for pinecode serverless
+ # inconsistency issue owing to risk of infinte loop
+ # and inablity to diffrentiate genuine cases of
+ # empty context.
+
+ context = "".join([context, retrieved_context])
+
+ if retrieval_type == PSKeys.SIMPLE:
+
context = _retrieve_context(output, doc_id, vector_index, prompt)
+ 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 = _retrieve_context(output, doc_id, vector_index, prompt)
+
answer = construct_and_run_prompt( # type:ignore
tool_settings=tool_settings,
output=output,