singhankur01 commited on
Commit
499d555
·
verified ·
1 Parent(s): 31d55d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -58,8 +58,8 @@ PRELOAD_URLS = [
58
  "https://hackrx.blob.core.windows.net/assets/indian_constitution.pdf?sv=2023-01-03&st=2025-07-28T06%3A42%3A00Z&se=2026-11-29T06%3A42%3A00Z&sr=b&sp=r&sig=5Gs%2FOXqP3zY00lgciu4BZjDV5QjTDIx7fgnfdz6Pu24%3D",
59
  "https://hackrx.blob.core.windows.net/assets/UNI%20GROUP%20HEALTH%20INSURANCE%20POLICY%20-%20UIIHLGP26043V022526%201.pdf?sv=2023-01-03&spr=https&st=2025-07-31T17%3A06%3A03Z&se=2026-08-01T17%3A06%3A00Z&sr=b&sp=r&sig=wLlooaThgRx91i2z4WaeggT0qnuUUEzIUKj42GsvMfg%3D",
60
  "https://hackrx.blob.core.windows.net/assets/Happy%20Family%20Floater%20-%202024%20OICHLIP25046V062425%201.pdf?sv=2023-01-03&spr=https&st=2025-07-31T17%3A24%3A30Z&se=2026-08-01T17%3A24%3A00Z&sr=b&sp=r&sig=VNMTTQUjdXGYb2F4Di4P0zNvmM2rTBoEHr%2BnkUXIqpQ%3D",
61
- "https://dn790007.ca.archive.org/0/items/newtonspmathema00newtrich/newtonspmathema00newtrich.pdf",
62
- "https://hackrx.blob.core.windows.net/assets/principia_newton.pdf?sv=2023-01-03&st=2025-07-28T07%3A20%3A32Z&se=2026-07-29T07%3A20%3A00Z&sr=b&sp=r&sig=V5I1QYyigoxeUMbnUKsdEaST99F5%2FDfo7wpKg9XXF5w%3D"
63
  ]
64
 
65
 
@@ -69,7 +69,7 @@ async def lifespan(app: FastAPI):
69
  print("🚀 Initializing models and prompt template...")
70
 
71
  try:
72
- GOOGLE_API_KEY = os.getenv("gemini_api_key3")
73
  print("🔑 gemini_api_key:", "FOUND" if GOOGLE_API_KEY else "NOT FOUND")
74
 
75
  if not GOOGLE_API_KEY:
@@ -106,12 +106,16 @@ async def lifespan(app: FastAPI):
106
  ml_models["prompt_template"] = ChatPromptTemplate.from_template("""
107
  **Role**: You are an expert assistant in insurance, legal compliance, human resources, and contract management.
108
  **Instructions**:
 
109
  - Answer using ONLY the provided context.
110
  - Identify each distinct question in the query and give exactly one concise, complete sentence for each , Also do not use line breakers in between the sentence.
 
 
111
  - Summarize the relevant part of the context without losing meaning.
112
  - Do not copy large clauses verbatim unless absolutely necessary.
113
  - If the answer is not in the context, respond: "Please ask query related to the Document only."
114
  - Do not use unnecessary filler or legal jargon.
 
115
  **Tone & Style**:
116
  - Professional yet approachable.
117
  - Clear and direct.
@@ -226,12 +230,12 @@ async def run_hackrx(req: RunRequest):
226
  # end_time2 = time.time() - start_time2
227
  # print(f"vector done: {end_time2}")
228
  # dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})
229
- dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 12 ,"lambda_mult": 0.5} )
230
 
231
 
232
  # Create retrievers using the pre-loaded models from our ml_models dictionary
233
  keyword_retriever = BM25Retriever.from_documents(chunks)
234
- keyword_retriever.k = 8
235
  # dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()
236
  ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.35, 0.65],search_kwargs={"k": 10})
237
  ### to make it faster we are now using our built reranker thats why commenting the code below
 
58
  "https://hackrx.blob.core.windows.net/assets/indian_constitution.pdf?sv=2023-01-03&st=2025-07-28T06%3A42%3A00Z&se=2026-11-29T06%3A42%3A00Z&sr=b&sp=r&sig=5Gs%2FOXqP3zY00lgciu4BZjDV5QjTDIx7fgnfdz6Pu24%3D",
59
  "https://hackrx.blob.core.windows.net/assets/UNI%20GROUP%20HEALTH%20INSURANCE%20POLICY%20-%20UIIHLGP26043V022526%201.pdf?sv=2023-01-03&spr=https&st=2025-07-31T17%3A06%3A03Z&se=2026-08-01T17%3A06%3A00Z&sr=b&sp=r&sig=wLlooaThgRx91i2z4WaeggT0qnuUUEzIUKj42GsvMfg%3D",
60
  "https://hackrx.blob.core.windows.net/assets/Happy%20Family%20Floater%20-%202024%20OICHLIP25046V062425%201.pdf?sv=2023-01-03&spr=https&st=2025-07-31T17%3A24%3A30Z&se=2026-08-01T17%3A24%3A00Z&sr=b&sp=r&sig=VNMTTQUjdXGYb2F4Di4P0zNvmM2rTBoEHr%2BnkUXIqpQ%3D",
61
+ "https://hackrx.blob.core.windows.net/assets/principia_newton.pdf?sv=2023-01-03&st=2025-07-28T07%3A20%3A32Z&se=2026-07-29T07%3A20%3A00Z&sr=b&sp=r&sig=V5I1QYyigoxeUMbnUKsdEaST99F5%2FDfo7wpKg9XXF5w%3D",
62
+ "https://hackrx.blob.core.windows.net/assets/hackrx_6/policies/HDFHLIP23024V072223.pdf?sv=2023-01-03&st=2025-07-30T06%3A46%3A49Z&se=2025-09-01T06%3A46%3A00Z&sr=c&sp=rl&sig=9szykRKdGYj0BVm1skP%2BX8N9%2FRENEn2k7MQPUp33jyQ%3D"
63
  ]
64
 
65
 
 
69
  print("🚀 Initializing models and prompt template...")
70
 
71
  try:
72
+ GOOGLE_API_KEY = os.getenv("gemini_api_key")
73
  print("🔑 gemini_api_key:", "FOUND" if GOOGLE_API_KEY else "NOT FOUND")
74
 
75
  if not GOOGLE_API_KEY:
 
106
  ml_models["prompt_template"] = ChatPromptTemplate.from_template("""
107
  **Role**: You are an expert assistant in insurance, legal compliance, human resources, and contract management.
108
  **Instructions**:
109
+ - If the query contains multiple questions, break them into separate sub-questions first.
110
  - Answer using ONLY the provided context.
111
  - Identify each distinct question in the query and give exactly one concise, complete sentence for each , Also do not use line breakers in between the sentence.
112
+ - Answer each sub-question separately, clearly numbered, using only relevant context for that part.
113
+ - Provide only the direct answer for each query found .
114
  - Summarize the relevant part of the context without losing meaning.
115
  - Do not copy large clauses verbatim unless absolutely necessary.
116
  - If the answer is not in the context, respond: "Please ask query related to the Document only."
117
  - Do not use unnecessary filler or legal jargon.
118
+ - Do not merge answers for different sub-questions into a single sentence.
119
  **Tone & Style**:
120
  - Professional yet approachable.
121
  - Clear and direct.
 
230
  # end_time2 = time.time() - start_time2
231
  # print(f"vector done: {end_time2}")
232
  # dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})
233
+ dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 10 ,"lambda_mult": 0.80} )
234
 
235
 
236
  # Create retrievers using the pre-loaded models from our ml_models dictionary
237
  keyword_retriever = BM25Retriever.from_documents(chunks)
238
+ keyword_retriever.k = 7
239
  # dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()
240
  ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.35, 0.65],search_kwargs={"k": 10})
241
  ### to make it faster we are now using our built reranker thats why commenting the code below