singhankur01 commited on
Commit
d3e01cd
·
verified ·
1 Parent(s): 6036aa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -68,7 +68,7 @@ async def lifespan(app: FastAPI):
68
  print("🚀 Initializing models and prompt template...")
69
 
70
  try:
71
- GOOGLE_API_KEY = os.getenv("gemini_api_key")
72
  print("🔑 gemini_api_key:", "FOUND" if GOOGLE_API_KEY else "NOT FOUND")
73
 
74
  if not GOOGLE_API_KEY:
@@ -99,30 +99,32 @@ async def lifespan(app: FastAPI):
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 Question Answering.
107
 
108
  **Instructions**:
109
- Step 1 - **Initial Draft**:
 
110
  - If the user query contains multiple questions, split them into clear, well-formed sub-questions.
111
- - Use ONLY the context provided below to generate answers.
112
- - For each sub-question, provide a concise, complete, single-sentence response.
113
- - Maintain the original order of the sub-questions in your responses.
114
- - Do NOT repeat the query text, do NOT number the answers, and separate them with a single space.
 
115
  - Do NOT use line breakers ("/n" or "\") in between the answers.
116
- - Avoid boilerplate phrases like “the document states” or “as per the context.”
117
- - If the answer to a sub-question is not in the context, say exactly: *I do not know the answer of "subquery",Please ask query related to the Document only.*
118
 
119
- Step 2 - **Critique & Revise**:
120
- - Review the initial answers to identify any sub-questions where relevant information from the context might have been missed.
121
- - Revise those answers to better incorporate relevant text and improve completeness and accuracy.
122
- - Ensure language is professional, clear, and aligned with domain tone.
123
 
124
- Step 3 - **Final Output**:
125
- - Output the revised, cohesive set of answers (one sentence per sub-question).
126
 
127
  ---
128
  **Context**:
@@ -235,15 +237,15 @@ async def run_hackrx(req: RunRequest):
235
  # end_time2 = time.time() - start_time2
236
  # print(f"vector done: {end_time2}")
237
  # dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 8})
238
- dense_retriever = vectorstore.as_retriever(search_type="mmr",search_kwargs={"k": 13 ,"lambda_mult": 0.7} )
239
  # dense_retriever = vectorstore.as_retriever(search_type="similarity" ,search_kwargs={"k": 11} )
240
 
241
 
242
  # Create retrievers using the pre-loaded models from our ml_models dictionary
243
  keyword_retriever = BM25Retriever.from_documents(chunks)
244
- keyword_retriever.k = 9
245
  # dense_retriever = Chroma.from_documents(documents=chunks, embedding=ml_models["embedder"]).as_retriever()
246
- ensemble_retriever = EnsembleRetriever(retrievers=[keyword_retriever, dense_retriever], weights=[0.35, 0.65],search_kwargs={"k": 12})
247
  ### to make it faster we are now using our built reranker thats why commenting the code below
248
  # compression_retriever = ContextualCompressionRetriever(
249
  # base_retriever=ensemble_retriever, base_compressor=ml_models["reranker_compressor"]
 
68
  print("🚀 Initializing models and prompt template...")
69
 
70
  try:
71
+ GOOGLE_API_KEY = os.getenv("gemini_api_key3")
72
  print("🔑 gemini_api_key:", "FOUND" if GOOGLE_API_KEY else "NOT FOUND")
73
 
74
  if not GOOGLE_API_KEY:
 
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**:
 
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": 17 ,"lambda_mult": 0.65} )
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 = 12
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": 18})
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"]