singhankur01 commited on
Commit
f22caa3
·
verified ·
1 Parent(s): 7f612e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -97,35 +97,36 @@ async def lifespan(app: FastAPI):
97
  ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=9)
98
  ml_models["llm"] = ChatGoogleGenerativeAI(
99
  # model="gemini-1.5-pro",
100
- model="gemini-2.0-flash",
101
  api_key=GOOGLE_API_KEY,
102
  # temperature=0.15,
103
  # max_output_tokens=300
104
  )
105
  ml_models["prompt_template"] = ChatPromptTemplate.from_template("""
106
- **Role**: You are an expert assistant in insurance, legal compliance, human resources, contract management, and general question answering.
107
 
108
  **Instructions**:
109
 
110
  Step 1 – **Initial Draft**:
111
- - If the user query contains multiple questions, split them into clear, well-formed sub-questions.
112
- - If the query or subqueris is a universal or general knowledge question (e.g., scientific facts, laws of motion), answer using your own knowledge if you think proper information is not there in the context given.
113
- - For all other domain-specific queries (e.g., insurance, legal, HR), rely strictly on the provided context to generate responses.
114
- - Answer each sub-question in concise and grammatically correct sentence, improving phrasing and spelling.
115
- - Preserve the original order of sub-questions in your responses.
116
- - Do not repeat the query text, do not number the answers, and separate the answer of the subquery with a new paragraph.
117
- - Do NOT use line breakers ("/n" or "\") in between the answers.
 
 
118
  - Avoid boilerplate phrases like “the document states” or “according to the context.”
119
- - If a sub-question cannot be answered from the context (and is not general knowledge), respond with: *I do not know the answer of "subquery", Please ask query related to the Document only.*
120
 
121
  Step 2 – **Critique & Revise**:
122
  - Review the initial answers for any missing or underused context.
123
- - Revise responses to improve accuracy, completeness, grammar and clarity based on the full context.
124
  - Maintain a professional and domain-appropriate tone.
125
 
126
  Step 3 – **Final Output**:
127
  - Present the revised and cohesive set of responses.
128
-
129
  ---
130
  **Context**:
131
  {context}
@@ -237,15 +238,15 @@ async def run_hackrx(req: RunRequest):
237
  # end_time2 = time.time() - start_time2
238
  # print(f"vector done: {end_time2}")
239
  # dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})
240
- dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 12 ,"lambda_mult": 0.7} )
241
  # dense_retriever = vectorstore.as_retriever(search_type="similarity" ,search_kwargs={"k": 11} )
242
 
243
 
244
  # Create retrievers using the pre-loaded models from our ml_models dictionary
245
  keyword_retriever = BM25Retriever.from_documents(chunks)
246
- keyword_retriever.k = 8
247
  # dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()
248
- ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.35, 0.7],search_kwargs={"k": 12})
249
  ### to make it faster we are now using our built reranker thats why commenting the code below
250
  # compression_retriever = ContextualCompressionRetriever(
251
  # base_retriever=ensemble_retriever, base_compressor=ml_models["reranker_compressor"]
 
97
  ml_models["reranker_compressor"] = CrossEncoderReranker(model=cross_encoder_model, top_n=9)
98
  ml_models["llm"] = ChatGoogleGenerativeAI(
99
  # model="gemini-1.5-pro",
100
+ model="gemini-2.5-flash",
101
  api_key=GOOGLE_API_KEY,
102
  # temperature=0.15,
103
  # max_output_tokens=300
104
  )
105
  ml_models["prompt_template"] = ChatPromptTemplate.from_template("""
106
+ **Role**: You are an expert assistant in insurance, legal compliance, human resources, and contract management and general question answering.
107
 
108
  **Instructions**:
109
 
110
  Step 1 – **Initial Draft**:
111
+ - If the query contains multiple questions, split them into perfect sub-questions.
112
+ - Use ONLY the provided context to answer.
113
+ - Provide one concise, complete sentence per sub-question.
114
+ - List answers in the same order as the sub-questions, without repeating the query text.
115
+ - Do not add numbering or bullet points; separate answers with a single space.
116
+ - Make the answer well structured and with proper starting like a human is answering it.
117
+ - Make grammatically correct sentence, improving phrasing and spelling.
118
+ - Avoid phrases like “the provided document states” or “according to the context.”
119
+ - Summarize relevant parts of the context without losing meaning.
120
  - Avoid boilerplate phrases like “the document states” or “according to the context.”
121
+ - If the answer is not in the context for some subqueries, respond exactly with: " I do not know the answer of "subquery",Please ask query related to the Document only." for that subquery.
122
 
123
  Step 2 – **Critique & Revise**:
124
  - Review the initial answers for any missing or underused context.
125
+ - Revise responses to improve accuracy, completeness, grammar and clarity based on the context.
126
  - Maintain a professional and domain-appropriate tone.
127
 
128
  Step 3 – **Final Output**:
129
  - Present the revised and cohesive set of responses.
 
130
  ---
131
  **Context**:
132
  {context}
 
238
  # end_time2 = time.time() - start_time2
239
  # print(f"vector done: {end_time2}")
240
  # dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})
241
+ dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 16 ,"lambda_mult": 0.65} )
242
  # dense_retriever = vectorstore.as_retriever(search_type="similarity" ,search_kwargs={"k": 11} )
243
 
244
 
245
  # Create retrievers using the pre-loaded models from our ml_models dictionary
246
  keyword_retriever = BM25Retriever.from_documents(chunks)
247
+ keyword_retriever.k = 11
248
  # dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()
249
+ ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.4, 0.6],search_kwargs={"k": 16})
250
  ### to make it faster we are now using our built reranker thats why commenting the code below
251
  # compression_retriever = ContextualCompressionRetriever(
252
  # base_retriever=ensemble_retriever, base_compressor=ml_models["reranker_compressor"]