Skip to content

Commit

Permalink
feat: convert latex to plain text
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ldelossa committed Jul 30, 2024
1 parent b1985ca commit cee74a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion chatblade/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rich.rule import Rule

from chatblade import utils
from pylatexenc.latex2text import LatexNodes2Text


console = Console()
Expand Down Expand Up @@ -60,6 +61,7 @@ def print_messages(messages, args):

def print_message(message, args):
printable = message.content

if not args.raw:
printable = detect_and_format_message(
message.content, cutoff=1000 if message.role == "user" else None, theme=args.theme
Expand Down Expand Up @@ -87,6 +89,10 @@ def extract_messages(messages, args):


def detect_and_format_message(msg, cutoff=None, theme=None):
# if message contains any latex, convert it to ascii.
converter = LatexNodes2Text()
msg = converter.latex_to_text(msg)

if cutoff and len(msg) > cutoff:
msg = "... **text shortened** ... " + msg[-cutoff:]
return msg
Expand Down Expand Up @@ -123,7 +129,6 @@ def extract_block(str):
except IndexError:
return None


def looks_like_markdown(str):
"""very rudimentary, but avoids making things markdown that shouldn't be"""
md_links = len(re.findall(r"\[[^]]+\]\(https?:\/\/\S+\)", str))
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ platformdirs==4.2.2
pydantic==2.8.2
pydantic_core==2.20.1
Pygments==2.18.0
pylatexenc==2.10
PyYAML==6.0.1
regex==2024.5.15
requests==2.32.3
Expand Down

0 comments on commit cee74a9

Please sign in to comment.