Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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("
|
| 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
|
| 107 |
|
| 108 |
**Instructions**:
|
| 109 |
-
|
|
|
|
| 110 |
- If the user query contains multiple questions, split them into clear, well-formed sub-questions.
|
| 111 |
-
-
|
| 112 |
-
- For
|
| 113 |
-
-
|
| 114 |
-
-
|
|
|
|
| 115 |
- Do NOT use line breakers ("/n" or "\") in between the answers.
|
| 116 |
-
- Avoid boilerplate phrases like “the document states” or “
|
| 117 |
-
- If
|
| 118 |
|
| 119 |
-
Step 2
|
| 120 |
-
- Review the initial answers
|
| 121 |
-
- Revise
|
| 122 |
-
-
|
| 123 |
|
| 124 |
-
Step 3
|
| 125 |
-
-
|
| 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":
|
| 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 =
|
| 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.
|
| 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"]
|