zenaight commited on
Commit Β·
0b6571b
1
Parent(s): 5367f7b
Enhance WhatsApp formatting correction in fix_whatsapp_formatting function
Browse files- Improved the fix_whatsapp_formatting function to handle multiple patterns for bold formatting, ensuring comprehensive conversion of markdown to WhatsApp-specific formatting.
- Added debug statements to track the presence of double asterisks before and after processing, aiding in troubleshooting and validation of formatting corrections.
- Updated the system message in chat_with_session_memory to include specific examples of correct and incorrect formatting, reinforcing user understanding of WhatsApp formatting rules.
- ai_chat.py +44 -3
ai_chat.py
CHANGED
|
@@ -9,11 +9,27 @@ def fix_whatsapp_formatting(text: str) -> str:
|
|
| 9 |
"""
|
| 10 |
Convert markdown formatting to WhatsApp formatting
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Fix double underscores to single underscores for italic
|
| 16 |
-
text = re.sub(r'__(
|
|
|
|
| 17 |
|
| 18 |
# Fix markdown links [text](url) to WhatsApp format
|
| 19 |
text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'\1: \2', text)
|
|
@@ -21,6 +37,16 @@ def fix_whatsapp_formatting(text: str) -> str:
|
|
| 21 |
# Remove any remaining square brackets
|
| 22 |
text = re.sub(r'\[([^\]]+)\]', r'\1', text)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return text
|
| 25 |
|
| 26 |
def chat_with_session_memory(state):
|
|
@@ -82,6 +108,13 @@ def chat_with_session_memory(state):
|
|
| 82 |
"π° Price: *RXXX,XXX*\n"
|
| 83 |
"π Features: Feature1, Feature2, Feature3\n"
|
| 84 |
"π View listing: https://url.com\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"\nπ¨ REMEMBER: WhatsApp is NOT markdown. Use *single asterisks* and emojis ONLY.\n"
|
| 86 |
)
|
| 87 |
|
|
@@ -208,9 +241,17 @@ def chat_with_session_memory(state):
|
|
| 208 |
response = llm.invoke(messages)
|
| 209 |
ai_response = response.content
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# Post-process to fix any markdown formatting that slipped through
|
| 212 |
ai_response = fix_whatsapp_formatting(ai_response)
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# Save messages to database (this will be handled by the async wrapper)
|
| 215 |
return {
|
| 216 |
"response": ai_response,
|
|
|
|
| 9 |
"""
|
| 10 |
Convert markdown formatting to WhatsApp formatting
|
| 11 |
"""
|
| 12 |
+
print(f"DEBUG - Original text has ** count: {text.count('**')}")
|
| 13 |
+
|
| 14 |
+
# Multiple passes to catch all patterns
|
| 15 |
+
for i in range(3): # Up to 3 passes to catch nested patterns
|
| 16 |
+
original_text = text
|
| 17 |
+
|
| 18 |
+
# Fix double asterisks to single asterisks for bold (multiple patterns)
|
| 19 |
+
text = re.sub(r'\*\*([^*]+)\*\*', r'*\1*', text)
|
| 20 |
+
text = re.sub(r'\*\*(\w+)\*\*', r'*\1*', text)
|
| 21 |
+
text = re.sub(r'\*\*([^*\n]+?)\*\*', r'*\1*', text)
|
| 22 |
+
text = re.sub(r'\*\*([^*]{1,50}?)\*\*', r'*\1*', text)
|
| 23 |
+
|
| 24 |
+
# Brute force: Replace any remaining ** with *
|
| 25 |
+
text = text.replace('**', '*')
|
| 26 |
+
|
| 27 |
+
if text == original_text:
|
| 28 |
+
break # No more changes
|
| 29 |
|
| 30 |
# Fix double underscores to single underscores for italic
|
| 31 |
+
text = re.sub(r'__([^_]+)__', r'_\1_', text)
|
| 32 |
+
text = text.replace('__', '_')
|
| 33 |
|
| 34 |
# Fix markdown links [text](url) to WhatsApp format
|
| 35 |
text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'\1: \2', text)
|
|
|
|
| 37 |
# Remove any remaining square brackets
|
| 38 |
text = re.sub(r'\[([^\]]+)\]', r'\1', text)
|
| 39 |
|
| 40 |
+
# Final safety check
|
| 41 |
+
remaining_double = text.count('**')
|
| 42 |
+
print(f"DEBUG - After processing ** count: {remaining_double}")
|
| 43 |
+
|
| 44 |
+
if '**' in text:
|
| 45 |
+
print(f"DEBUG - Markdown still found after processing: {text}")
|
| 46 |
+
# Nuclear option: replace ALL ** with *
|
| 47 |
+
text = text.replace('**', '*')
|
| 48 |
+
print(f"DEBUG - After nuclear option: {text}")
|
| 49 |
+
|
| 50 |
return text
|
| 51 |
|
| 52 |
def chat_with_session_memory(state):
|
|
|
|
| 108 |
"π° Price: *RXXX,XXX*\n"
|
| 109 |
"π Features: Feature1, Feature2, Feature3\n"
|
| 110 |
"π View listing: https://url.com\n"
|
| 111 |
+
"\nπ¨ SPECIFIC EXAMPLES OF WHAT NOT TO DO IN TERMS OF ADDING BOLD TO LETTERS:\n"
|
| 112 |
+
"β WRONG: π **Features**:\n"
|
| 113 |
+
"β
CORRECT: π *Features*:\n"
|
| 114 |
+
"β WRONG: π **Tags**:\n"
|
| 115 |
+
"β
CORRECT: π *Tags*:\n"
|
| 116 |
+
"β WRONG: **Large Warehouse in Randburg**\n"
|
| 117 |
+
"β
CORRECT: *Large Warehouse in Randburg*\n"
|
| 118 |
"\nπ¨ REMEMBER: WhatsApp is NOT markdown. Use *single asterisks* and emojis ONLY.\n"
|
| 119 |
)
|
| 120 |
|
|
|
|
| 241 |
response = llm.invoke(messages)
|
| 242 |
ai_response = response.content
|
| 243 |
|
| 244 |
+
# Debug: Check original response
|
| 245 |
+
if '**' in ai_response:
|
| 246 |
+
print(f"DEBUG - BEFORE processing: Found ** in response: {ai_response[:200]}...")
|
| 247 |
+
|
| 248 |
# Post-process to fix any markdown formatting that slipped through
|
| 249 |
ai_response = fix_whatsapp_formatting(ai_response)
|
| 250 |
|
| 251 |
+
# Debug: Check after processing
|
| 252 |
+
if '**' in ai_response:
|
| 253 |
+
print(f"DEBUG - AFTER processing: Still found ** in response: {ai_response[:200]}...")
|
| 254 |
+
|
| 255 |
# Save messages to database (this will be handled by the async wrapper)
|
| 256 |
return {
|
| 257 |
"response": ai_response,
|