Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,6 +104,24 @@ class DocumentRetrievalAndGeneration:
|
|
| 104 |
|
| 105 |
return RetrieverTool(self)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
def run_agentic_rag(self, question: str) -> str:
|
| 108 |
retriever_output = self.retriever_tool.run(question)
|
| 109 |
|
|
@@ -119,11 +137,26 @@ Question: {question}
|
|
| 119 |
Answer:"""
|
| 120 |
|
| 121 |
input_ids = self.tokenizer.encode(enhanced_prompt, return_tensors="pt").to(self.model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
return self.generate_response_with_timeout(input_ids)
|
| 124 |
|
| 125 |
def query_and_generate_response(self, query):
|
| 126 |
-
#
|
| 127 |
similarityThreshold = 1
|
| 128 |
query_embedding = self.embeddings.encode(query, convert_to_tensor=True).cpu().numpy()
|
| 129 |
distances, indices = self.gpu_index.search(np.array([query_embedding]), k=3)
|
|
@@ -143,26 +176,10 @@ Answer:"""
|
|
| 143 |
print(self.all_splits[idx].page_content)
|
| 144 |
print("############################")
|
| 145 |
|
| 146 |
-
|
| 147 |
-
{"role": "system", "content": "You are a knowledgeable assistant with access to a comprehensive database."},
|
| 148 |
-
{"role": "user", "content": f"""
|
| 149 |
-
I need you to answer my question and provide related information in a specific format.
|
| 150 |
-
I have provided five relatable json files {content}, choose the most suitable chunks for answering the query.
|
| 151 |
-
RETURN ONLY SOLUTION without additional comments, sign-offs, retrived chunks, refrence to any Ticket or extra phrases. Be direct and to the point.
|
| 152 |
-
IF THERE IS NO ANSWER RELATABLE IN RETRIEVED CHUNKS, RETURN "NO SOLUTION AVAILABLE".
|
| 153 |
-
DO NOT GIVE REFRENCE TO ANY CHUNKS OR TICKETS,BE ON POINT.
|
| 154 |
-
|
| 155 |
-
Here's my question:
|
| 156 |
-
Query: {query}
|
| 157 |
-
Solution==>
|
| 158 |
-
"""}
|
| 159 |
-
]
|
| 160 |
-
input_ids = self.tokenizer.apply_chat_template(conversation, return_tensors="pt").to(self.model.device)
|
| 161 |
-
|
| 162 |
start_time = datetime.now()
|
| 163 |
-
standard_response = self.
|
| 164 |
elapsed_time = datetime.now() - start_time
|
| 165 |
-
|
| 166 |
print("Generated standard response:", standard_response)
|
| 167 |
print("Time elapsed:", elapsed_time)
|
| 168 |
print("Device in use:", self.model.device)
|
|
@@ -170,15 +187,16 @@ Answer:"""
|
|
| 170 |
standard_solution_text = standard_response.strip()
|
| 171 |
if "Solution:" in standard_solution_text:
|
| 172 |
standard_solution_text = standard_solution_text.split("Solution:", 1)[1].strip()
|
| 173 |
-
|
| 174 |
-
# Post-processing to remove "assistant" prefix
|
| 175 |
standard_solution_text = re.sub(r'^assistant\s*', '', standard_solution_text, flags=re.IGNORECASE)
|
| 176 |
standard_solution_text = standard_solution_text.strip()
|
| 177 |
|
| 178 |
# Agentic RAG
|
| 179 |
agentic_solution_text = self.run_agentic_rag(query)
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
return combined_solution, content
|
| 183 |
|
| 184 |
def qa_infer_gradio(self, query):
|
|
@@ -220,7 +238,7 @@ if __name__ == "__main__":
|
|
| 220 |
examples=EXAMPLES,
|
| 221 |
cache_examples=False,
|
| 222 |
outputs=[gr.Textbox(label="RESPONSE"), gr.Textbox(label="RELATED QUERIES")],
|
| 223 |
-
css=
|
| 224 |
title="TI E2E FORUM"
|
| 225 |
)
|
| 226 |
|
|
|
|
| 104 |
|
| 105 |
return RetrieverTool(self)
|
| 106 |
|
| 107 |
+
def run_standard_rag(self, query: str, content: str) -> str:
|
| 108 |
+
conversation = [
|
| 109 |
+
{"role": "system", "content": "You are a knowledgeable assistant with access to a comprehensive database."},
|
| 110 |
+
{"role": "user", "content": f"""
|
| 111 |
+
I need you to answer my question and provide related information in a specific format.
|
| 112 |
+
I have provided five relatable json files {content}, choose the most suitable chunks for answering the query.
|
| 113 |
+
RETURN ONLY SOLUTION without additional comments, sign-offs, retrived chunks, refrence to any Ticket or extra phrases. Be direct and to the point.
|
| 114 |
+
IF THERE IS NO ANSWER RELATABLE IN RETRIEVED CHUNKS, RETURN "NO SOLUTION AVAILABLE".
|
| 115 |
+
DO NOT GIVE REFRENCE TO ANY CHUNKS OR TICKETS,BE ON POINT.
|
| 116 |
+
|
| 117 |
+
Here's my question:
|
| 118 |
+
Query: {query}
|
| 119 |
+
Solution==>
|
| 120 |
+
"""}
|
| 121 |
+
]
|
| 122 |
+
input_ids = self.tokenizer.apply_chat_template(conversation, return_tensors="pt").to(self.model.device)
|
| 123 |
+
return self.generate_response_with_timeout(input_ids)
|
| 124 |
+
|
| 125 |
def run_agentic_rag(self, question: str) -> str:
|
| 126 |
retriever_output = self.retriever_tool.run(question)
|
| 127 |
|
|
|
|
| 137 |
Answer:"""
|
| 138 |
|
| 139 |
input_ids = self.tokenizer.encode(enhanced_prompt, return_tensors="pt").to(self.model.device)
|
| 140 |
+
return self.generate_response_with_timeout(input_ids)
|
| 141 |
+
|
| 142 |
+
def run_analytical_rag(self, question: str) -> str:
|
| 143 |
+
retriever_output = self.retriever_tool.run(question)
|
| 144 |
|
| 145 |
+
enhanced_prompt = f"""Using the following information retrieved from the knowledge base:
|
| 146 |
+
|
| 147 |
+
{retriever_output}
|
| 148 |
+
|
| 149 |
+
Provide a detailed, step-by-step analysis of the question below. Break down the problem, consider different aspects, and provide a thorough explanation. If relevant information is missing, state what additional data would be needed for a complete analysis.
|
| 150 |
+
|
| 151 |
+
Question: {question}
|
| 152 |
+
Analysis:
|
| 153 |
+
1. """
|
| 154 |
+
|
| 155 |
+
input_ids = self.tokenizer.encode(enhanced_prompt, return_tensors="pt").to(self.model.device)
|
| 156 |
return self.generate_response_with_timeout(input_ids)
|
| 157 |
|
| 158 |
def query_and_generate_response(self, query):
|
| 159 |
+
# Retrieval step
|
| 160 |
similarityThreshold = 1
|
| 161 |
query_embedding = self.embeddings.encode(query, convert_to_tensor=True).cpu().numpy()
|
| 162 |
distances, indices = self.gpu_index.search(np.array([query_embedding]), k=3)
|
|
|
|
| 176 |
print(self.all_splits[idx].page_content)
|
| 177 |
print("############################")
|
| 178 |
|
| 179 |
+
# Standard RAG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
start_time = datetime.now()
|
| 181 |
+
standard_response = self.run_standard_rag(query, content)
|
| 182 |
elapsed_time = datetime.now() - start_time
|
|
|
|
| 183 |
print("Generated standard response:", standard_response)
|
| 184 |
print("Time elapsed:", elapsed_time)
|
| 185 |
print("Device in use:", self.model.device)
|
|
|
|
| 187 |
standard_solution_text = standard_response.strip()
|
| 188 |
if "Solution:" in standard_solution_text:
|
| 189 |
standard_solution_text = standard_solution_text.split("Solution:", 1)[1].strip()
|
|
|
|
|
|
|
| 190 |
standard_solution_text = re.sub(r'^assistant\s*', '', standard_solution_text, flags=re.IGNORECASE)
|
| 191 |
standard_solution_text = standard_solution_text.strip()
|
| 192 |
|
| 193 |
# Agentic RAG
|
| 194 |
agentic_solution_text = self.run_agentic_rag(query)
|
| 195 |
|
| 196 |
+
# Analytical RAG
|
| 197 |
+
analytical_solution_text = self.run_analytical_rag(query)
|
| 198 |
+
|
| 199 |
+
combined_solution = f"Standard RAG Solution:\n{standard_solution_text}\n\nAgentic RAG Solution:\n{agentic_solution_text}\n\nAnalytical RAG Solution:\n{analytical_solution_text}"
|
| 200 |
return combined_solution, content
|
| 201 |
|
| 202 |
def qa_infer_gradio(self, query):
|
|
|
|
| 238 |
examples=EXAMPLES,
|
| 239 |
cache_examples=False,
|
| 240 |
outputs=[gr.Textbox(label="RESPONSE"), gr.Textbox(label="RELATED QUERIES")],
|
| 241 |
+
css=code,
|
| 242 |
title="TI E2E FORUM"
|
| 243 |
)
|
| 244 |
|