ChAbhishek28 commited on
Commit
fe9e63c
Β·
1 Parent(s): df5eb6f

Add text bot support to groq websocket handler - route text clients to structured responses instead of audio

Browse files
Files changed (1) hide show
  1. groq_websocket_handler.py +95 -10
groq_websocket_handler.py CHANGED
@@ -87,6 +87,10 @@ class GroqWebSocketHandler:
87
  await self._process_audio_stream(websocket, session_id, message)
88
  elif message_type == "text_query":
89
  await self._process_text_query(websocket, session_id, message)
 
 
 
 
90
  elif message_type == "conversation_state":
91
  await self._handle_conversation_state(websocket, session_id, message)
92
  elif message_type == "voice_settings":
@@ -177,7 +181,7 @@ class GroqWebSocketHandler:
177
  })
178
 
179
  # Process the transcribed query
180
- await self._process_transcribed_query(websocket, session_id, transcribed_text, user_language)
181
 
182
  except Exception as e:
183
  logger.error(f"❌ Audio processing error: {e}")
@@ -187,7 +191,71 @@ class GroqWebSocketHandler:
187
  "timestamp": time.time()
188
  })
189
 
190
- async def _process_transcribed_query(self, websocket: WebSocket, session_id: str, query: str, language: str = "en"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  """Process transcribed query and generate response"""
192
  try:
193
  # Update session activity
@@ -256,11 +324,31 @@ class GroqWebSocketHandler:
256
  "timestamp": time.time()
257
  })
258
 
259
- # Also send friend's format
260
- await self.send_message(session_id, {
261
- "type": "llm_response",
262
- "text": response_text
263
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  # Update conversation history
266
  if session_id in self.user_sessions:
@@ -271,9 +359,6 @@ class GroqWebSocketHandler:
271
  "timestamp": time.time()
272
  })
273
 
274
- # Generate TTS audio response (like friend's backend)
275
- await self._generate_audio_response(session_id, response_text)
276
-
277
  except Exception as e:
278
  logger.error(f"❌ Query processing error: {e}")
279
  await self.send_message(session_id, {
 
87
  await self._process_audio_stream(websocket, session_id, message)
88
  elif message_type == "text_query":
89
  await self._process_text_query(websocket, session_id, message)
90
+ elif message_type == "voice_message":
91
+ await self._process_voice_message(websocket, session_id, message)
92
+ elif message_type == "connection":
93
+ await self._handle_connection_message(websocket, session_id, message)
94
  elif message_type == "conversation_state":
95
  await self._handle_conversation_state(websocket, session_id, message)
96
  elif message_type == "voice_settings":
 
181
  })
182
 
183
  # Process the transcribed query
184
+ await self._process_transcribed_query(websocket, session_id, transcribed_text, user_language, client_type="voice")
185
 
186
  except Exception as e:
187
  logger.error(f"❌ Audio processing error: {e}")
 
191
  "timestamp": time.time()
192
  })
193
 
194
+ async def _process_voice_message(self, websocket: WebSocket, session_id: str, message: Dict[str, Any]):
195
+ """Process voice message with direct transcription (for text clients)"""
196
+ try:
197
+ transcription = message.get("transcription", "")
198
+ client_type = message.get("client_type", "voice")
199
+ language = message.get("lang", "english")
200
+
201
+ # Check session data for client type override
202
+ if session_id in self.user_sessions:
203
+ stored_client_type = self.user_sessions[session_id].get("client_type")
204
+ if stored_client_type:
205
+ client_type = stored_client_type
206
+
207
+ if not transcription:
208
+ await self.send_message(session_id, {
209
+ "type": "error",
210
+ "message": "No transcription provided",
211
+ "timestamp": time.time()
212
+ })
213
+ return
214
+
215
+ logger.info(f"πŸ’¬ Processing voice message from {client_type} client: {transcription}")
216
+
217
+ # Process the query (same as transcribed query)
218
+ await self._process_transcribed_query(websocket, session_id, transcription,
219
+ language, client_type=client_type)
220
+
221
+ except Exception as e:
222
+ logger.error(f"❌ Voice message processing error: {e}")
223
+ await self.send_message(session_id, {
224
+ "type": "error",
225
+ "message": f"Voice message processing failed: {str(e)}",
226
+ "timestamp": time.time()
227
+ })
228
+
229
+ async def _handle_connection_message(self, websocket: WebSocket, session_id: str, message: Dict[str, Any]):
230
+ """Handle connection message to store client preferences"""
231
+ try:
232
+ client_type = message.get("client_type", "voice")
233
+ knowledge_base = message.get("knowledge_base", "government_docs")
234
+
235
+ # Update session data with client type
236
+ if session_id in self.user_sessions:
237
+ self.user_sessions[session_id]["client_type"] = client_type
238
+ self.user_sessions[session_id]["knowledge_base"] = knowledge_base
239
+
240
+ logger.info(f"πŸ”— Client type set to: {client_type} for session {session_id}")
241
+
242
+ # Send confirmation
243
+ await self.send_message(session_id, {
244
+ "type": "connection_confirmed",
245
+ "client_type": client_type,
246
+ "knowledge_base": knowledge_base,
247
+ "timestamp": time.time()
248
+ })
249
+
250
+ except Exception as e:
251
+ logger.error(f"❌ Connection message handling error: {e}")
252
+ await self.send_message(session_id, {
253
+ "type": "error",
254
+ "message": f"Connection setup failed: {str(e)}",
255
+ "timestamp": time.time()
256
+ })
257
+
258
+ async def _process_transcribed_query(self, websocket: WebSocket, session_id: str, query: str, language: str = "en", client_type: str = "voice"):
259
  """Process transcribed query and generate response"""
260
  try:
261
  # Update session activity
 
324
  "timestamp": time.time()
325
  })
326
 
327
+ # Send different response formats based on client type
328
+ if client_type == "text":
329
+ # For text clients, send structured response
330
+ await self.send_message(session_id, {
331
+ "type": "streaming_response",
332
+ "clause_text": response_text,
333
+ "summary": response_text[:200] + "..." if len(response_text) > 200 else response_text,
334
+ "role_checklist": [],
335
+ "source_title": "Government Document Assistant",
336
+ "clause_id": f"response_{int(time.time())}",
337
+ "date": time.strftime("%Y-%m-%d"),
338
+ "url": "",
339
+ "score": 1.0,
340
+ "scenario_analysis": None,
341
+ "charts": []
342
+ })
343
+ else:
344
+ # For voice clients, send friend's format
345
+ await self.send_message(session_id, {
346
+ "type": "llm_response",
347
+ "text": response_text
348
+ })
349
+
350
+ # Generate TTS audio response (like friend's backend)
351
+ await self._generate_audio_response(session_id, response_text)
352
 
353
  # Update conversation history
354
  if session_id in self.user_sessions:
 
359
  "timestamp": time.time()
360
  })
361
 
 
 
 
362
  except Exception as e:
363
  logger.error(f"❌ Query processing error: {e}")
364
  await self.send_message(session_id, {