/ fix_hybrid.py
fix_hybrid.py
 1  #!/usr/bin/env python3
 2  """
 3  Quick fix for hybrid orchestrator response handling
 4  """
 5  
 6  import json
 7  import re
 8  
 9  def clean_response(text: str) -> str:
10      """Clean up API response formatting"""
11      if not text:
12          return "No response generated."
13      
14      # Remove common formatting tags
15      text = re.sub(r'<s>|</s>|\[OUT\]|\[/OUT\]|\[/s\]', '', text)
16      
17      # Remove multiple spaces and newlines
18      text = re.sub(r'\s+', ' ', text).strip()
19      
20      # If text is very short or just whitespace, return meaningful message
21      if len(text) < 10 or text.isspace():
22          return "Response was too short or empty. Please try again with a different query."
23      
24      return text
25  
26  # Test with your saved files
27  print("=== CLEANING SAVED RESPONSES ===")
28  
29  files = [
30      "hybrid_result_20251211_082454.json",
31      "hybrid_result_20251211_082500.json",
32      "cloud_result_20251211_082458.json"
33  ]
34  
35  for filename in files:
36      try:
37          with open(filename, 'r') as f:
38              data = json.load(f)
39          
40          print(f"\nšŸ“„ {filename}:")
41          if 'response' in data:
42              original = data['response']
43              cleaned = clean_response(original)
44              print(f"  Original: {original[:100]}...")
45              print(f"  Cleaned:  {cleaned[:100]}...")
46              
47              # Update the file
48              data['response'] = cleaned
49              with open(filename, 'w') as f:
50                  json.dump(data, f, indent=2)
51              print("  āœ… Updated")
52      except FileNotFoundError:
53          print(f"  āš ļø  File not found: {filename}")