muhammadnoman76 commited on
Commit
0148862
·
1 Parent(s): c9d249e
Files changed (1) hide show
  1. app/services/chat_processor.py +195 -196
app/services/chat_processor.py CHANGED
@@ -9,7 +9,6 @@ from app.services.environmental_condition import EnvironmentalData
9
  from app.services.prompts import *
10
  from app.services.vector_database_search import VectorDatabaseSearch
11
  import re
12
-
13
  vectordb = VectorDatabaseSearch()
14
 
15
  class ChatProcessor:
@@ -23,6 +22,7 @@ class ChatProcessor:
23
  city = self.user_city if self.user_city else ''
24
  self.environment_data = EnvironmentalData(city)
25
  self.web_searcher = WebSearch(num_results=num_results, max_images=num_images)
 
26
 
27
  def extract_keywords_yake(self, text: str, language: str, max_ngram_size: int = 2, num_keywords: int = 4) -> list:
28
  lang_code = "en"
@@ -58,31 +58,210 @@ class ChatProcessor:
58
  name = profile['name']
59
  age = profile['age']
60
  self.chat_session.load_chat_history()
61
- self.chat_session.update_title(self.session_id, query)
62
  history = self.chat_session.format_history()
63
 
64
- history_based_prompt = HISTORY_BASED_PROMPT.format(history=history, query=query)
 
65
  enhanced_query = Model().send_message_openrouter(history_based_prompt)
66
 
67
  self.session_id = self.ensure_valid_session(title=enhanced_query)
68
  permission = self.chat_session.get_user_preferences()
69
- websearch_enabled = permission.get('websearch', False)
70
  env_recommendations = permission.get('environmental_recommendations', False)
71
  personalized_recommendations = permission.get('personalized_recommendations', False)
72
  keywords_permission = permission.get('keywords', False)
73
  reference_permission = permission.get('references', False)
74
  language = self.chat_session.get_language().lower()
75
 
76
- language_prompt = LANGUAGE_RESPONSE_PROMPT.format(language=language)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- if websearch_enabled:
79
- return self._process_with_websearch(enhanced_query, query, name, age, history, language,
80
- env_recommendations, personalized_recommendations,
81
- keywords_permission, reference_permission, language_prompt)
82
  else:
83
- return self._process_with_vectordb(enhanced_query, query, name, age, history, language,
84
- env_recommendations, personalized_recommendations,
85
- keywords_permission, reference_permission, language_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  except Exception as e:
88
  return {
@@ -91,189 +270,9 @@ class ChatProcessor:
91
  "response": "Sorry, there was an error processing your request.",
92
  "timestamp": datetime.now(timezone.utc).isoformat()
93
  }
94
-
95
- def _process_with_websearch(self, enhanced_query, original_query, name, age, history, language,
96
- env_recommendations, personalized_recommendations,
97
- keywords_permission, reference_permission, language_prompt):
98
- with ThreadPoolExecutor(max_workers=2) as executor:
99
- future_web = executor.submit(self.web_searcher.search, enhanced_query)
100
- future_images = executor.submit(self.web_searcher.search_images, enhanced_query)
101
- web_results = future_web.result()
102
- image_results = future_images.result()
103
-
104
- context_parts = []
105
- references = []
106
-
107
- for idx, result in enumerate(web_results, 1):
108
- if result['text']:
109
- context_parts.append(f"From Source {idx}: {result['text']}\n")
110
- references.append(result['link'])
111
-
112
- context = "\n".join(context_parts)
113
-
114
- if env_recommendations and personalized_recommendations:
115
- prompt = ENVIRONMENTAL_PERSONALIZED_PROMPT.format(
116
- user_name=name,
117
- user_age=age,
118
- history=history,
119
- user_details=self.chat_session.get_personalized_recommendation(),
120
- environmental_condition=self.environment_data.get_environmental_data(),
121
- previous_history=history,
122
- context=context,
123
- current_query=enhanced_query
124
- )
125
- elif personalized_recommendations:
126
- prompt = PERSONALIZED_PROMPT.format(
127
- user_name=name,
128
- user_age=age,
129
- user_details=self.chat_session.get_personalized_recommendation(),
130
- previous_history=history,
131
- context=context,
132
- current_query=enhanced_query
133
- )
134
- elif env_recommendations:
135
- prompt = ENVIRONMENTAL_PROMPT.format(
136
- user_name=name,
137
- user_age=age,
138
- environmental_condition=self.environment_data.get_environmental_data(),
139
- previous_history=history,
140
- context=context,
141
- current_query=enhanced_query
142
- )
143
- else:
144
- prompt = DEFAULT_PROMPT.format(
145
- previous_history=history,
146
- context=context,
147
- current_query=enhanced_query
148
- )
149
-
150
- prompt = prompt + language_prompt
151
- response = Model().llm(prompt, enhanced_query)
152
- keywords = self.extract_keywords_yake(response, language=language) if keywords_permission else ""
153
- references = references if reference_permission else ""
154
-
155
- chat_data = {
156
- "query": enhanced_query,
157
- "response": response,
158
- "references": references,
159
- "page_no": "",
160
- "keywords": keywords,
161
- "images": image_results,
162
- "context": context,
163
- "timestamp": datetime.now(timezone.utc).isoformat(),
164
- "session_id": self.chat_session.session_id
165
- }
166
-
167
- if not self.chat_session.save_chat(chat_data):
168
- raise ValueError("Failed to save chat message")
169
- return chat_data
170
-
171
- def _process_with_vectordb(self, enhanced_query, original_query, name, age, history, language,
172
- env_recommendations, personalized_recommendations,
173
- keywords_permission, reference_permission, language_prompt):
174
- attach_image = False
175
-
176
- with ThreadPoolExecutor(max_workers=1) as executor:
177
- future_images = executor.submit(self.web_searcher.search_images, enhanced_query)
178
- image_results = future_images.result()
179
-
180
- start_time = datetime.now(timezone.utc)
181
- results = vectordb.search(query=enhanced_query, top_k=3)
182
-
183
- context_parts = []
184
- references = []
185
- seen_pages = set()
186
-
187
- for result in results:
188
- confidence = result['confidence']
189
- if confidence > 60:
190
- context_parts.append(f"Content: {result['content']}")
191
- page = result['page']
192
- if page not in seen_pages:
193
- references.append(f"Source: {result['source']}, Page: {page}")
194
- seen_pages.add(page)
195
- attach_image = True
196
-
197
- context = "\n".join(context_parts)
198
- if not context or len(context) < 10:
199
- context = "There is no context found unfortunately"
200
-
201
- if env_recommendations and personalized_recommendations:
202
- prompt = ENVIRONMENTAL_PERSONALIZED_PROMPT.format(
203
- user_name=name,
204
- user_age=age,
205
- history=history,
206
- user_details=self.chat_session.get_personalized_recommendation(),
207
- environmental_condition=self.environment_data.get_environmental_data(),
208
- previous_history=history,
209
- context=context,
210
- current_query=enhanced_query
211
- )
212
- elif personalized_recommendations:
213
- prompt = PERSONALIZED_PROMPT.format(
214
- user_name=name,
215
- user_age=age,
216
- user_details=self.chat_session.get_personalized_recommendation(),
217
- previous_history=history,
218
- context=context,
219
- current_query=enhanced_query
220
- )
221
- elif env_recommendations:
222
- prompt = ENVIRONMENTAL_PROMPT.format(
223
- user_name=name,
224
- user_age=age,
225
- environmental_condition=self.environment_data.get_environmental_data(),
226
- previous_history=history,
227
- context=context,
228
- current_query=enhanced_query
229
- )
230
- else:
231
- prompt = DEFAULT_PROMPT.format(
232
- previous_history=history,
233
- context=context,
234
- current_query=enhanced_query
235
- )
236
-
237
- prompt = prompt + language_prompt
238
- response = Model().llm(prompt, original_query)
239
- end_time = datetime.now(timezone.utc)
240
-
241
- keywords = self.extract_keywords_yake(response, language=language) if keywords_permission else ""
242
- references = references if reference_permission else ""
243
-
244
- if not attach_image:
245
- image_results = ""
246
- keywords = ""
247
-
248
- chat_data = {
249
- "query": enhanced_query,
250
- "response": response,
251
- "references": references,
252
- "page_no": "",
253
- "keywords": keywords,
254
- "images": image_results,
255
- "context": context,
256
- "timestamp": datetime.now(timezone.utc).isoformat(),
257
- "session_id": self.chat_session.session_id
258
- }
259
-
260
- match = re.search(r'(## Personal Recommendations|## Environmental Considerations)', response)
261
- truncated_response = response[:match.start()].strip() if match else response
262
-
263
- if not self.chat_session.save_details(
264
- session_id=self.session_id,
265
- context=context,
266
- query=enhanced_query,
267
- response=truncated_response,
268
- rag_start_time=start_time,
269
- rag_end_time=end_time
270
- ):
271
- raise ValueError("Failed to save the RAG details")
272
-
273
- if not self.chat_session.save_chat(chat_data):
274
- raise ValueError("Failed to save chat message")
275
-
276
- return chat_data
277
 
278
  def web_search(self, query: str) -> Dict[str, Any]:
279
- return self.process_chat(query=query)
 
 
 
 
9
  from app.services.prompts import *
10
  from app.services.vector_database_search import VectorDatabaseSearch
11
  import re
 
12
  vectordb = VectorDatabaseSearch()
13
 
14
  class ChatProcessor:
 
22
  city = self.user_city if self.user_city else ''
23
  self.environment_data = EnvironmentalData(city)
24
  self.web_searcher = WebSearch(num_results=num_results, max_images=num_images)
25
+ self.web_search_required = True
26
 
27
  def extract_keywords_yake(self, text: str, language: str, max_ngram_size: int = 2, num_keywords: int = 4) -> list:
28
  lang_code = "en"
 
58
  name = profile['name']
59
  age = profile['age']
60
  self.chat_session.load_chat_history()
61
+ self.chat_session.update_title(self.session_id,query)
62
  history = self.chat_session.format_history()
63
 
64
+ history_based_prompt = HISTORY_BASED_PROMPT.format(history=history,query= query)
65
+
66
  enhanced_query = Model().send_message_openrouter(history_based_prompt)
67
 
68
  self.session_id = self.ensure_valid_session(title=enhanced_query)
69
  permission = self.chat_session.get_user_preferences()
70
+ websearch_enabled = permission.get('websearch', False)
71
  env_recommendations = permission.get('environmental_recommendations', False)
72
  personalized_recommendations = permission.get('personalized_recommendations', False)
73
  keywords_permission = permission.get('keywords', False)
74
  reference_permission = permission.get('references', False)
75
  language = self.chat_session.get_language().lower()
76
 
77
+
78
+ language_prompt = LANGUAGE_RESPONSE_PROMPT.format(language = language)
79
+
80
+ if websearch_enabled :
81
+ with ThreadPoolExecutor(max_workers=2) as executor:
82
+ future_web = executor.submit(self.web_searcher.search, enhanced_query)
83
+ future_images = executor.submit(self.web_searcher.search_images, enhanced_query)
84
+ web_results = future_web.result()
85
+ image_results = future_images.result()
86
+
87
+ context_parts = []
88
+ references = []
89
+
90
+ for idx, result in enumerate(web_results, 1):
91
+ if result['text']:
92
+ context_parts.append(f"From Source {idx}: {result['text']}\n")
93
+ references.append(result['link'])
94
+
95
+ context = "\n".join(context_parts)
96
+
97
+ if env_recommendations and personalized_recommendations:
98
+ prompt = ENVIRONMENTAL_PERSONALIZED_PROMPT.format(
99
+ user_name=name,
100
+ user_age=age,
101
+ history=history,
102
+ user_details=self.chat_session.get_personalized_recommendation(),
103
+ environmental_condition=self.environment_data.get_environmental_data(),
104
+ previous_history=history,
105
+ context=context,
106
+ current_query=enhanced_query
107
+ )
108
+ elif personalized_recommendations:
109
+ prompt = PERSONALIZED_PROMPT.format(
110
+ user_name=name,
111
+ user_age=age,
112
+ user_details=self.chat_session.get_personalized_recommendation(),
113
+ previous_history=history,
114
+ context=context,
115
+ current_query=enhanced_query
116
+ )
117
+ elif env_recommendations :
118
+ prompt = ENVIRONMENTAL_PROMPT.format(
119
+ user_name=name,
120
+ user_age=age,
121
+ environmental_condition=self.environment_data.get_environmental_data(),
122
+ previous_history=history,
123
+ context=context,
124
+ current_query=enhanced_query
125
+ )
126
+ else:
127
+ prompt = DEFAULT_PROMPT.format(
128
+ previous_history=history,
129
+ context=context,
130
+ current_query=enhanced_query
131
+ )
132
+
133
+ prompt = prompt + language_prompt
134
+
135
+ response = Model().llm(prompt,enhanced_query)
136
+
137
+ keywords = ""
138
+
139
+ if (keywords_permission):
140
+ keywords = self.extract_keywords_yake(response, language=language)
141
+ if (not reference_permission):
142
+ references = ""
143
+
144
+ chat_data = {
145
+ "query": enhanced_query,
146
+ "response": response,
147
+ "references": references,
148
+ "page_no": "",
149
+ "keywords": keywords,
150
+ "images": image_results,
151
+ "context": context,
152
+ "timestamp": datetime.now(timezone.utc).isoformat(),
153
+ "session_id": self.chat_session.session_id
154
+ }
155
+
156
+ if not self.chat_session.save_chat(chat_data):
157
+ raise ValueError("Failed to save chat message")
158
+ return chat_data
159
 
 
 
 
 
160
  else:
161
+ attach_image = False
162
+
163
+ with ThreadPoolExecutor(max_workers=2) as executor:
164
+ future_images = executor.submit(self.web_searcher.search_images, enhanced_query)
165
+ image_results = future_images.result()
166
+
167
+ start_time = datetime.now(timezone.utc)
168
+
169
+ results = vectordb.search( query=enhanced_query, top_k=3)
170
+
171
+ context_parts = []
172
+ references = []
173
+ seen_pages = set()
174
+
175
+ for result in results:
176
+ confidence = result['confidence']
177
+ if confidence > 60:
178
+ context_parts.append(f"Content: {result['content']}")
179
+ page = result['page']
180
+ if page not in seen_pages: # Only append if page is not seen
181
+ references.append(f"Source: {result['source']}, Page: {page}")
182
+ seen_pages.add(page)
183
+ attach_image = True
184
+
185
+ context = "\n".join(context_parts)
186
+
187
+ if not context or len(context) < 10:
188
+ context = "There is no context found unfortunately"
189
+
190
+ if env_recommendations and personalized_recommendations:
191
+ prompt = ENVIRONMENTAL_PERSONALIZED_PROMPT.format(
192
+ user_name=name,
193
+ user_age = age,
194
+ history=history,
195
+ user_details=self.chat_session.get_personalized_recommendation(),
196
+ environmental_condition=self.environment_data.get_environmental_data(),
197
+ previous_history=history,
198
+ context=context,
199
+ current_query=enhanced_query
200
+ )
201
+ elif personalized_recommendations:
202
+ prompt = PERSONALIZED_PROMPT.format(
203
+ user_name=name,
204
+ user_age=age,
205
+ user_details=self.chat_session.get_personalized_recommendation(),
206
+ previous_history=history,
207
+ context=context,
208
+ current_query=enhanced_query
209
+ )
210
+ elif env_recommendations :
211
+ prompt = ENVIRONMENTAL_PROMPT.format(
212
+ user_name=name,
213
+ user_age=age,
214
+ environmental_condition=self.environment_data.get_environmental_data(),
215
+ previous_history=history,
216
+ context=context,
217
+ current_query=enhanced_query
218
+ )
219
+ else:
220
+ prompt = DEFAULT_PROMPT.format(
221
+ previous_history=history,
222
+ context=context,
223
+ current_query=enhanced_query
224
+ )
225
+
226
+ prompt = prompt + language_prompt
227
+
228
+ response = Model().response = Model().llm(prompt,query)
229
+
230
+ end_time = datetime.now(timezone.utc)
231
+
232
+ keywords = ""
233
+
234
+ if (keywords_permission):
235
+ keywords = self.extract_keywords_yake(response, language=language)
236
+
237
+ if (not reference_permission):
238
+ references = ""
239
+
240
+ if not attach_image:
241
+ image_results = ""
242
+ keywords = ""
243
+
244
+ chat_data = {
245
+ "query": enhanced_query,
246
+ "response": response,
247
+ "references": references,
248
+ "page_no": "",
249
+ "keywords": keywords,
250
+ "images": image_results,
251
+ "context": context,
252
+ "timestamp": datetime.now(timezone.utc).isoformat(),
253
+ "session_id": self.chat_session.session_id
254
+ }
255
+ match = re.search(r'(## Personal Recommendations|## Environmental Considerations)', response)
256
+ if match:
257
+ truncated_response = response[:match.start()].strip()
258
+ else:
259
+ truncated_response = response
260
+ if not self.chat_session.save_details(session_id=self.session_id , context= context , query= enhanced_query , response=truncated_response , rag_start_time=start_time , rag_end_time=end_time ):
261
+ raise ValueError("Failed to save the RAG details")
262
+ if not self.chat_session.save_chat(chat_data):
263
+ raise ValueError("Failed to save chat message")
264
+ return chat_data
265
 
266
  except Exception as e:
267
  return {
 
270
  "response": "Sorry, there was an error processing your request.",
271
  "timestamp": datetime.now(timezone.utc).isoformat()
272
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
 
274
  def web_search(self, query: str) -> Dict[str, Any]:
275
+ if self.session_id and len(self.session_id) > 5:
276
+ return self.process_chat(query=query)
277
+ else:
278
+ return self.process_chat(query=query)