-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (33 loc) · 1.16 KB
/
utils.py
File metadata and controls
44 lines (33 loc) · 1.16 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
"""Utility functions for DeckSmith."""
from typing import Any, Dict, Union
def clean_response(response: Union[str, Dict[str, Any]]) -> str:
"""Clean and format a chatbot response for display.
Args:
response: The raw response, either a string or dictionary with 'response' key.
Returns:
A cleaned and formatted response string.
"""
if isinstance(response, dict):
response = response.get('response', '')
if not isinstance(response, str):
response = str(response)
response = response.replace("{", "").replace("}", "").replace("\"", "")
response = response.replace("\n\n", "\n")
response = response.replace(" ", " ")
replacements = {
"\u2022": "-",
"\u27a2": "-",
"\u2023": "-",
"\u25aa": "-",
"\u25ab": "-",
}
for old, new in replacements.items():
response = response.replace(old, new)
lines = response.split("\n")
formatted_lines = []
for line in lines:
if line.startswith("-"):
formatted_lines.append(f"- {line[1:].strip()}")
else:
formatted_lines.append(line)
return "\n\n".join(formatted_lines)