codeBOKER commited on
Commit
3e8eb5c
·
1 Parent(s): 5b59120

Fix: strip markdown and HTML tags from AI responses for Telegram

Browse files
Files changed (1) hide show
  1. ai_service.py +14 -2
ai_service.py CHANGED
@@ -9,8 +9,20 @@ MODEL_NAME = HF_MODEL
9
 
10
  def clean_ai_response(text: str):
11
  if not text: return ""
12
- cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
13
- return cleaned_text.strip()
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  async def search_bank_knowledge(query: str):
16
  query_embedding = pc.inference.embed(
 
9
 
10
  def clean_ai_response(text: str):
11
  if not text: return ""
12
+ text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
13
+ # Remove markdown tables
14
+ text = re.sub(r'\|.*?\|', '', text)
15
+ text = re.sub(r'[-|]{3,}', '', text)
16
+ # Remove HTML tags
17
+ text = re.sub(r'<br\s*/?>', '\n', text)
18
+ text = re.sub(r'<[^>]+>', '', text)
19
+ # Remove markdown bold/italic
20
+ text = re.sub(r'[*_]{1,3}(.*?)[*_]{1,3}', r'\1', text)
21
+ # Remove markdown headers
22
+ text = re.sub(r'^#{1,6}\s*', '', text, flags=re.MULTILINE)
23
+ # Clean up extra blank lines
24
+ text = re.sub(r'\n{3,}', '\n\n', text)
25
+ return text.strip()
26
 
27
  async def search_bank_knowledge(query: str):
28
  query_embedding = pc.inference.embed(