huggingface / chat-ui

Open source codebase powering the HuggingChat app
https://huggingface.co/chat
Apache License 2.0
7.61k stars 1.12k forks source link

Katex fails to render math expressions from ChatGPT4. #1097

Open haje01 opened 6 months ago

haje01 commented 6 months ago

I am using Chat UI version 0.8.3 and ChatGPT version gpt-4-turbo-2024-04-09.

ChatGPT is outputting formula delimiters as \[, \], \(, \) and katex in the current version of ChatUI is not rendering them correctly. Based on my experiments, katex renders only formulas with $ delimiters correctly.

I did a quick test with the following prompts

echo following text as is: \[ D_i \] <- Fail to render

echo following text as is: $ D_i $ <- Successful

Thank you in advance.

nsarrazin commented 6 months ago

Thanks for bringing this up!

I can reproduce the issue, but I'm not sure how we can tackle it. We use marked but I can't see an option for delimiters there. 👀

Would be open to ideas on how to fix this

haje01 commented 4 months ago

I also couldn't find a Katex setting to change the delimiter, so I worked around it by appending the following text in the ChatGPT prompt

Use $ as a delimiter at the beginning and end of the formula.

If the answer from the ChatGPT is in non-Latin characters, such as CJK, for example, the following prompt would be useful.

Use $ as a delimiter at the beginning and end of the formula. Make sure to put a single space after the delimiter $ whenever it's followed by a non-Latin character, such as CJK 

I hope this helps.

thorvaldur-arnar commented 1 month ago

There seems to be an extension for configuring delimiters: https://katex.org/docs/autorender

haje01 commented 1 week ago

Specifying the following system prompt for the ChatGPT model seems to resolve most issues with equation rendering:

Indicate the start and end of inline equations with '$ ' and ' $', respectively. Use $$ to delimit the start and end of block equations.