Spaces:
Runtime error
Runtime error
Commit ·
ba2a182
1
Parent(s): 4b2359f
Fix summary truncation: Add intelligent summary generation for impact analysis and policy queries - no more cut-off summaries
Browse files- groq_websocket_handler.py +64 -1
groq_websocket_handler.py
CHANGED
|
@@ -365,11 +365,14 @@ class GroqWebSocketHandler:
|
|
| 365 |
# Generate charts for impact analysis queries
|
| 366 |
charts = await self._generate_charts_if_needed(query, response_text)
|
| 367 |
|
|
|
|
|
|
|
|
|
|
| 368 |
# For text clients, send structured response
|
| 369 |
await self.send_message(session_id, {
|
| 370 |
"type": "streaming_response",
|
| 371 |
"clause_text": response_text,
|
| 372 |
-
"summary":
|
| 373 |
"role_checklist": [],
|
| 374 |
"source_title": "Government Document Assistant",
|
| 375 |
"clause_id": f"response_{int(time.time())}",
|
|
@@ -653,5 +656,65 @@ class GroqWebSocketHandler:
|
|
| 653 |
logger.error(f"❌ Chart generation error: {e}")
|
| 654 |
return []
|
| 655 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
# Global instance
|
| 657 |
groq_websocket_handler = GroqWebSocketHandler()
|
|
|
|
| 365 |
# Generate charts for impact analysis queries
|
| 366 |
charts = await self._generate_charts_if_needed(query, response_text)
|
| 367 |
|
| 368 |
+
# Create intelligent summary based on query type
|
| 369 |
+
summary = self._create_intelligent_summary(query, response_text)
|
| 370 |
+
|
| 371 |
# For text clients, send structured response
|
| 372 |
await self.send_message(session_id, {
|
| 373 |
"type": "streaming_response",
|
| 374 |
"clause_text": response_text,
|
| 375 |
+
"summary": summary,
|
| 376 |
"role_checklist": [],
|
| 377 |
"source_title": "Government Document Assistant",
|
| 378 |
"clause_id": f"response_{int(time.time())}",
|
|
|
|
| 656 |
logger.error(f"❌ Chart generation error: {e}")
|
| 657 |
return []
|
| 658 |
|
| 659 |
+
def _create_intelligent_summary(self, query: str, response_text: str) -> str:
|
| 660 |
+
"""Create intelligent summary based on query type and content"""
|
| 661 |
+
try:
|
| 662 |
+
query_lower = query.lower()
|
| 663 |
+
|
| 664 |
+
# For impact analysis queries, create detailed summary
|
| 665 |
+
if any(keyword in query_lower for keyword in ['impact', 'effect', 'analyze', 'comparison', 'scenario']):
|
| 666 |
+
# Extract key points from response for impact analysis
|
| 667 |
+
lines = response_text.split('\n')
|
| 668 |
+
key_points = []
|
| 669 |
+
|
| 670 |
+
for line in lines:
|
| 671 |
+
line = line.strip()
|
| 672 |
+
if any(indicator in line.lower() for indicator in ['conclusion', 'comparison', 'impact', 'analysis', 'result']):
|
| 673 |
+
if len(line) > 20 and not line.startswith('|'): # Avoid table rows
|
| 674 |
+
key_points.append(line)
|
| 675 |
+
if len(key_points) >= 3: # Limit to top 3 key points
|
| 676 |
+
break
|
| 677 |
+
|
| 678 |
+
if key_points:
|
| 679 |
+
summary = ' '.join(key_points)
|
| 680 |
+
# Ensure summary is not too long but comprehensive
|
| 681 |
+
if len(summary) > 500:
|
| 682 |
+
summary = summary[:500] + "..."
|
| 683 |
+
return summary
|
| 684 |
+
|
| 685 |
+
# For policy overview queries, extract the main policy information
|
| 686 |
+
elif any(keyword in query_lower for keyword in ['policies', 'rules', 'schemes', 'overview']):
|
| 687 |
+
# Look for policy definitions and key features
|
| 688 |
+
lines = response_text.split('\n')
|
| 689 |
+
policy_info = []
|
| 690 |
+
|
| 691 |
+
for line in lines:
|
| 692 |
+
line = line.strip()
|
| 693 |
+
if (line.startswith('**') or 'policy' in line.lower() or 'scheme' in line.lower()) and len(line) > 20:
|
| 694 |
+
policy_info.append(line.replace('**', '').strip())
|
| 695 |
+
if len(policy_info) >= 2:
|
| 696 |
+
break
|
| 697 |
+
|
| 698 |
+
if policy_info:
|
| 699 |
+
summary = ' '.join(policy_info)
|
| 700 |
+
if len(summary) > 400:
|
| 701 |
+
summary = summary[:400] + "..."
|
| 702 |
+
return summary
|
| 703 |
+
|
| 704 |
+
# Default: Use first paragraph or first 300 characters
|
| 705 |
+
paragraphs = response_text.split('\n\n')
|
| 706 |
+
if len(paragraphs) > 0:
|
| 707 |
+
first_paragraph = paragraphs[0].strip()
|
| 708 |
+
if len(first_paragraph) > 300:
|
| 709 |
+
return first_paragraph[:300] + "..."
|
| 710 |
+
return first_paragraph
|
| 711 |
+
|
| 712 |
+
# Fallback to character limit
|
| 713 |
+
return response_text[:300] + "..." if len(response_text) > 300 else response_text
|
| 714 |
+
|
| 715 |
+
except Exception as e:
|
| 716 |
+
logger.error(f"❌ Summary generation error: {e}")
|
| 717 |
+
return response_text[:200] + "..." if len(response_text) > 200 else response_text
|
| 718 |
+
|
| 719 |
# Global instance
|
| 720 |
groq_websocket_handler = GroqWebSocketHandler()
|