-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_cot_filtering.py
More file actions
80 lines (65 loc) · 2.36 KB
/
Copy pathpatch_cot_filtering.py
File metadata and controls
80 lines (65 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""
Patch the LLM service to improve Chain of Thought filtering
"""
import os
def patch_llm_service():
"""Patch the LLM service with enhanced CoT filtering"""
# Path to the LLM service file
llm_service_path = 'Ultimate_Voice_Bridge/backend/services/llm_service.py'
if not os.path.exists(llm_service_path):
print(f"LLM service file not found at: {llm_service_path}")
return False
# Read the current content with proper encoding
try:
with open(llm_service_path, 'r', encoding='utf-8') as f:
original_content = f.read()
except UnicodeDecodeError:
# Fallback to latin-1 if utf-8 fails
with open(llm_service_path, 'r', encoding='latin-1') as f:
original_content = f.read()
# Enhanced CoT filtering patterns
enhanced_patterns = '''
# Enhanced Chain of Thought filtering patterns
COT_FILTER_PATTERNS = [
r"Let's think step by step.*?\\n",
r"Step \\d+: .*?\\n",
r"First, .*?\\n",
r"Then, .*?\\n",
r"Therefore, .*?\\n",
r"Because .*?\\n",
r"So, .*?\\n",
r"This means .*?\\n",
r"Let me break this down.*?\\n",
r"Here's my thought process.*?\\n"
]
'''
# Check if patterns already exist
if "COT_FILTER_PATTERNS" in original_content:
print("✅ CoT filtering patterns already exist")
return True
# Add enhanced patterns to the file
# Find a good insertion point (after imports)
lines = original_content.split('\\n')
insert_index = 0
# Find the end of imports section
for i, line in enumerate(lines):
if line.startswith('import ') or line.startswith('from '):
insert_index = i + 1
elif line.strip() == '' and insert_index > 0:
insert_index = i + 1
break
# Insert the enhanced patterns
lines.insert(insert_index, enhanced_patterns.strip())
# Write back the modified content
modified_content = '\\n'.join(lines)
try:
with open(llm_service_path, 'w', encoding='utf-8') as f:
f.write(modified_content)
print("✅ Enhanced CoT filtering patterns added to LLM service")
return True
except (OSError, IOError) as e:
print(f"❌ Failed to write enhanced patterns: {e}")
return False
if __name__ == "__main__":
patch_llm_service()