Query Decomposition( Advanced RAG )

Introduction
IN Parallel Query retrieval and reciprocal rank fusion, we were just re-writing the query in different forms, we were not changing the abstraction parameter of query. Query decomposition deals with converting a more abstract query to less abstract. We will use a method called Chain of Thoughts to achieve this result.
System Prompt
You should write the following in system prompt for better results.
“Generate a step by step plan on how to answer user query. Give output in form of 5 queries and make each query less abstract then the past one.“
This method is also know as Chain of thoughts.
Walk Through of Algorithm
User inputs a query and we ask LLM to process it by giving the above system prompt. LLM will process it and give queries from most abstract to least abstract format.
We will ask LLM to generate only 3 or 5 queries for logistical reasons . Pass each query to get relevant embeddings out of it and store the outputs.
Do same steps for all the queries. You will be be receiving 3 to 5 outputs.
Pass on all outputs with user query to LLM to generate a good final answer.
Relevant Question: Should the weightage of output last query be significantly more than that of output of first query. Since the last query is least abstract and contains all the information , so according to me , weightage of last output should be more than all other outputs .Do share your views in comments😊!!
Example: If 5 outputs are generated , then ratio of significance should be in order of: 0,10 ,20,30,40 respectively.
Pseudo Code:
FUNCTION step_back_prompting(original_question):
# 1. Generate a step-back question
step_back_question = generate_step_back_question(original_question)
# 2. Use the step-back question to retrieve or generate relevant information
relevant_information = retrieve_or_generate_information(step_back_question)
# 3. Use the relevant information and the step-back question to solve the original problem
solution = solve_original_problem(step_back_question, relevant_information, original_question)
RETURN solution
END FUNCTION
FUNCTION generate_step_back_question(question):
# This function would need to be implemented based on the specific task and context
# For example, if the original question is about a specific car model, the step-back
# question could be about general car characteristics or the car industry
# This is the core of the step-back prompting strategy. It requires careful
# design and may involve prompting the model itself to generate the step-back question.
RETURN step_back_question_string # The generated step-back question
END FUNCTION
FUNCTION retrieve_or_generate_information(question):
# This function could involve searching a knowledge base, using a retrieval model,
# or directly generating information based on the step-back question using a model
# It is crucial to get accurate and relevant information to ensure the solution
# to the original question is accurate
RETURN relevant_information_data # The retrieved or generated information
END FUNCTION
FUNCTION solve_original_problem(step_back_question, relevant_information, original_question):
# This function would use the retrieved or generated information and the original
# question to solve the problem. It might involve reasoning, problem-solving steps,
# or even calling a separate function for the specific task.
RETURN solution_to_original_question
END FUNCTION
Complete code will be posted on my github repo shortly!! Stay tuned!
Subscribe to my newsletter
Read articles from Shrey C paunwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
