Stream internal phases and final answer using SSE (text/event-stream).
(self)
| 302 | self.send_json_response({"error": f"Server error: {str(e)}"}, status_code=500) |
| 303 | |
| 304 | def handle_chat_stream(self): |
| 305 | """Stream internal phases and final answer using SSE (text/event-stream).""" |
| 306 | try: |
| 307 | content_length = int(self.headers['Content-Length']) |
| 308 | post_data = self.rfile.read(content_length) |
| 309 | data = json.loads(post_data.decode('utf-8')) |
| 310 | |
| 311 | query = data.get('query') |
| 312 | session_id = data.get('session_id') |
| 313 | compose_flag = data.get('compose_sub_answers') |
| 314 | decomp_flag = data.get('query_decompose') |
| 315 | ai_rerank_flag = data.get('ai_rerank') |
| 316 | ctx_expand_flag = data.get('context_expand') |
| 317 | verify_flag = data.get('verify') |
| 318 | |
| 319 | # ✨ NEW RETRIEVAL PARAMETERS |
| 320 | retrieval_k = data.get('retrieval_k', 20) |
| 321 | context_window_size = data.get('context_window_size', 1) |
| 322 | reranker_top_k = data.get('reranker_top_k', 10) |
| 323 | search_type = data.get('search_type', 'hybrid') |
| 324 | dense_weight = data.get('dense_weight', 0.7) |
| 325 | |
| 326 | # 🚩 NEW: Force RAG override from frontend |
| 327 | force_rag = bool(data.get('force_rag', False)) |
| 328 | |
| 329 | # 🌿 Provence sentence pruning |
| 330 | provence_prune = data.get('provence_prune') |
| 331 | provence_threshold = data.get('provence_threshold') |
| 332 | |
| 333 | # User-selected generation model |
| 334 | requested_model = data.get('model') |
| 335 | if isinstance(requested_model,str) and requested_model: |
| 336 | RAG_AGENT.ollama_config['generation_model']=requested_model |
| 337 | |
| 338 | if not query: |
| 339 | self.send_json_response({"error": "Query is required"}, status_code=400) |
| 340 | return |
| 341 | |
| 342 | # 🔄 UPDATE SESSION TITLE: If this is the first message in the session, update the title |
| 343 | if session_id: |
| 344 | try: |
| 345 | # Check if this is the first message by calling the backend server |
| 346 | backend_url = f"http://localhost:8000/sessions/{session_id}" |
| 347 | session_resp = requests.get(backend_url) |
| 348 | if session_resp.status_code == 200: |
| 349 | session_data = session_resp.json() |
| 350 | session = session_data.get('session', {}) |
| 351 | # If message_count is 0, this is the first message |
| 352 | if session.get('message_count', 0) == 0: |
| 353 | # Generate a title from the first message |
| 354 | title = generate_session_title(query) |
| 355 | # Update the session title via backend API |
| 356 | # We'll need to add this endpoint to the backend, for now let's make a direct database call |
| 357 | # This is a temporary solution until we add a proper API endpoint |
| 358 | db.update_session_title(session_id, title) |
| 359 | print(f"📝 Updated session title to: {title}") |
| 360 | |
| 361 | # 💾 STORE USER MESSAGE: Add the user message to the database |
no test coverage detected