huggingface / chat-ui

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

Katex fails to render math expressions from ChatGPT4. #1097

Open haje01 opened 2 months ago

haje01 commented 2 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 2 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 3 weeks 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.