Skip to content

Commit f0c87c1

Browse files
committed
feat: convert latex to plain text
Newest GPT models love to provide latex equations. Unfortunately this makes things pretty impossible to read for larger questions. Add a conversion to change latex markup to plain text. Signed-off-by: ldelossa <[email protected]>
1 parent b1985ca commit f0c87c1

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

chatblade/printer.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from rich.rule import Rule
1010

1111
from chatblade import utils
12+
from pylatexenc.latex2text import LatexNodes2Text
1213

1314

1415
console = Console()
@@ -87,6 +88,10 @@ def extract_messages(messages, args):
8788

8889

8990
def detect_and_format_message(msg, cutoff=None, theme=None):
91+
# if message contains any latex, convert it to ascii.
92+
converter = LatexNodes2Text()
93+
msg = converter.latex_to_text(msg)
94+
9095
if cutoff and len(msg) > cutoff:
9196
msg = "... **text shortened** ... " + msg[-cutoff:]
9297
return msg
@@ -123,7 +128,6 @@ def extract_block(str):
123128
except IndexError:
124129
return None
125130

126-
127131
def looks_like_markdown(str):
128132
"""very rudimentary, but avoids making things markdown that shouldn't be"""
129133
md_links = len(re.findall(r"\[[^]]+\]\(https?:\/\/\S+\)", str))

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ platformdirs==4.2.2
1414
pydantic==2.8.2
1515
pydantic_core==2.20.1
1616
Pygments==2.18.0
17+
pylatexenc==2.10
1718
PyYAML==6.0.1
1819
regex==2024.5.15
1920
requests==2.32.3

0 commit comments

Comments
 (0)